KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanukisoftware > wrapper > test > Memory


1 package org.tanukisoftware.wrapper.test;
2
3 /*
4  * Copyright (c) 1999, 2006 Tanuki Software Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of the Java Service Wrapper and associated
8  * documentation files (the "Software"), to deal in the Software
9  * without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sub-license,
11  * and/or sell copies of the Software, and to permit persons to
12  * whom the Software is furnished to do so, subject to the
13  * following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  *
28  * Portions of the Software have been derived from source code
29  * developed by Silver Egg Technology under the following license:
30  *
31  * Copyright (c) 2001 Silver Egg Technology
32  *
33  * Permission is hereby granted, free of charge, to any person
34  * obtaining a copy of this software and associated documentation
35  * files (the "Software"), to deal in the Software without
36  * restriction, including without limitation the rights to use,
37  * copy, modify, merge, publish, distribute, sub-license, and/or
38  * sell copies of the Software, and to permit persons to whom the
39  * Software is furnished to do so, subject to the following
40  * conditions:
41  *
42  * The above copyright notice and this permission notice shall be
43  * included in all copies or substantial portions of the Software.
44  */

45
46 import java.io.FileWriter JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.Writer JavaDoc;
49 import java.lang.reflect.InvocationTargetException JavaDoc;
50 import java.lang.reflect.Method JavaDoc;
51
52 /**
53  *
54  *
55  * @author Leif Mortenson <leif@tanukisoftware.com>
56  */

57 public class Memory implements Runnable JavaDoc
58 {
59     private Writer JavaDoc m_writer;
60     private Thread JavaDoc m_runner;
61     
62     /*---------------------------------------------------------------
63      * Runnable Method
64      *-------------------------------------------------------------*/

65     public void run()
66     {
67         if ( m_runner == null )
68         {
69             // This is the runner
70
m_runner = Thread.currentThread();
71         }
72         else
73         {
74             System.out.println("Stopping..." );
75             // This is the shutdown hook. Sloppy code, but simple :-)
76
m_runner = null;
77             return;
78         }
79         
80         long startTime = System.currentTimeMillis();
81         long lastTest = startTime;
82         try
83         {
84             m_writer.write( "--> Starting Memory Log\n" );
85             m_writer.flush();
86     
87             while( m_runner != null )
88             {
89                 long now = System.currentTimeMillis();
90                 System.out.println( "Running for " + ( now - startTime ) + "ms..." );
91                 
92                 if ( now - lastTest > 15000 )
93                 {
94                     Runtime JavaDoc rt = Runtime.getRuntime();
95                     System.gc();
96                     long totalMemory = rt.totalMemory();
97                     long freeMemory = rt.freeMemory();
98                     long usedMemory = totalMemory - freeMemory;
99                     
100                     m_writer.write( "total memory=" + pad( totalMemory, 10 )
101                         + ", used=" + pad( usedMemory, 10 )
102                         + ", free=" + pad( freeMemory, 10 ) + "\n" );
103                     m_writer.flush();
104                     
105                     lastTest = now;
106                 }
107                 
108                 try
109                 {
110                     Thread.sleep( 250 );
111                 }
112                 catch ( InterruptedException JavaDoc e )
113                 {
114                 }
115             }
116             
117             m_writer.write( "<-- Stopping Memory Log\n" );
118             m_writer.flush();
119             m_writer.close();
120         }
121         catch ( IOException JavaDoc e )
122         {
123             e.printStackTrace();
124         }
125     }
126     
127     /*---------------------------------------------------------------
128      * Methods
129      *-------------------------------------------------------------*/

130     private static final String JavaDoc PADDING = " ";
131     private String JavaDoc pad( long n, int len )
132     {
133         String JavaDoc s = Long.toString( n );
134         int sLen = s.length();
135         if ( sLen < len )
136         {
137             s = s + PADDING.substring( 0, len - sLen );
138         }
139         return s;
140     }
141     /*---------------------------------------------------------------
142      * Main Method
143      *-------------------------------------------------------------*/

144     public static void main(String JavaDoc[] args)
145     {
146         System.out.println("Memory Tester Running...");
147         
148         // Locate the add and remove shutdown hook methods using reflection so
149
// that this class can be compiled on 1.2.x versions of java.
150
Method JavaDoc addShutdownHookMethod;
151         try {
152             addShutdownHookMethod =
153                 Runtime JavaDoc.class.getMethod("addShutdownHook", new Class JavaDoc[] {Thread JavaDoc.class});
154         } catch (NoSuchMethodException JavaDoc e) {
155             System.out.println("Shutdown hooks not supported by current JVM.");
156             addShutdownHookMethod = null;
157         }
158         
159         Memory app = new Memory();
160         
161         // Create a Writer for the memory output
162
try
163         {
164             app.m_writer = new FileWriter JavaDoc( "memory.log" );
165         }
166         catch ( IOException JavaDoc e )
167         {
168             e.printStackTrace();
169             return;
170         }
171         
172         // Register a shutdown hook using reflection.
173
if (addShutdownHookMethod != null) {
174             Runtime JavaDoc runtime = Runtime.getRuntime();
175             Thread JavaDoc hook = new Thread JavaDoc( app, "shutdown-hook" );
176             try {
177                 addShutdownHookMethod.invoke(runtime, new Object JavaDoc[] {hook});
178             } catch (IllegalAccessException JavaDoc e) {
179                 System.out.println("Unable to register shutdown hook: " + e.getMessage());
180             } catch (InvocationTargetException JavaDoc e) {
181                 System.out.println("Unable to register shutdown hook: " + e.getMessage());
182             }
183         }
184         
185         // Start the runner
186
new Thread JavaDoc( app, "runner" ).start();
187     }
188 }
189
Popular Tags