| | @@ -42,67 +41,75 @@ |
| 42 | 41 | */ |
| 43 | 42 | public class Memory implements Runnable |
| 44 | 43 | { |
| 45 | | - private Writer m_writer; |
| 44 | + private static Memory c_theInstance; |
| 45 | + |
| 46 | 46 | private Thread m_runner; |
| 47 | 47 | |
| 48 | 48 | /*--------------------------------------------------------------- |
| 49 | + * Constructor |
| 50 | + *-------------------------------------------------------------*/ |
| 51 | + private Memory() |
| 52 | + { |
| 53 | + // Start the runner |
| 54 | + m_runner = new Thread( this, "runner" ); |
| 55 | + m_runner.start(); |
| 56 | + } |
| 57 | + |
| 58 | + /*--------------------------------------------------------------- |
| 49 | 59 | * Runnable Method |
| 50 | 60 | *-------------------------------------------------------------*/ |
| 51 | 61 | public void run() |
| 52 | 62 | { |
| 53 | | - if ( m_runner == null ) |
| 54 | | - { |
| 55 | | - // This is the runner |
| 56 | | - m_runner = Thread.currentThread(); |
| 57 | | - } |
| 58 | | - else |
| 59 | | - { |
| 60 | | - System.out.println(Main.getRes().getString( "Stopping..." ) ); |
| 61 | | - // This is the shutdown hook. Sloppy code, but simple :-) |
| 62 | | - m_runner = null; |
| 63 | | - return; |
| 64 | | - } |
| 65 | | - |
| 66 | 63 | long startTime = System.currentTimeMillis(); |
| 67 | 64 | long lastTest = startTime; |
| 68 | 65 | try |
| 69 | 66 | { |
| 70 | | - m_writer.write( Main.getRes().getString( "--> Starting Memory Log\n" ) ); |
| 71 | | - m_writer.flush(); |
| 72 | | - |
| 73 | | - while( m_runner != null ) |
| 67 | + File file = new File( "../logs/memory.log" ); |
| 68 | + System.out.println( Main.getRes().getString( "Writing memory Log to: {0}", file ) ); |
| 69 | + |
| 70 | + Writer writer = new FileWriter( file ); |
| 71 | + try |
| 74 | 72 | { |
| 75 | | - long now = System.currentTimeMillis(); |
| 76 | | - System.out.println( Main.getRes().getString( "Running for {0}ms...", new Long( now - startTime ) ) ); |
| 73 | + writer.write( Main.getRes().getString( "--> Starting Memory Log\n" ) ); |
| 74 | + writer.flush(); |
| 77 | 75 | |
| 78 | | - if ( now - lastTest > 15000 ) |
| 76 | + while( m_runner != null ) |
| 79 | 77 | { |
| 80 | | - Runtime rt = Runtime.getRuntime(); |
| 81 | | - System.gc(); |
| 82 | | - long totalMemory = rt.totalMemory(); |
| 83 | | - long freeMemory = rt.freeMemory(); |
| 84 | | - long usedMemory = totalMemory - freeMemory; |
| 78 | + long now = System.currentTimeMillis(); |
| 79 | + System.out.println( Main.getRes().getString( "Running for {0}ms...", new Long( now - startTime ) ) ); |
| 85 | 80 | |
| 86 | | - m_writer.write( Main.getRes().getString( "total memory=" ) + pad( totalMemory, 10 ) |
| 87 | | - + Main.getRes().getString( ", used=" ) + pad( usedMemory, 10 ) |
| 88 | | - + Main.getRes().getString( ", free=" ) + pad( freeMemory, 10 ) + "\n" ); |
| 89 | | - m_writer.flush(); |
| 81 | + if ( now - lastTest > 15000 ) |
| 82 | + { |
| 83 | + Runtime rt = Runtime.getRuntime(); |
| 84 | + System.gc(); |
| 85 | + long totalMemory = rt.totalMemory(); |
| 86 | + long freeMemory = rt.freeMemory(); |
| 87 | + long usedMemory = totalMemory - freeMemory; |
| 88 | + |
| 89 | + writer.write( Main.getRes().getString( "total memory=" ) + pad( totalMemory, 10 ) |
| 90 | + + Main.getRes().getString( ", used=" ) + pad( usedMemory, 10 ) |
| 91 | + + Main.getRes().getString( ", free=" ) + pad( freeMemory, 10 ) + "\n" ); |
| 92 | + writer.flush(); |
| 93 | + |
| 94 | + lastTest = now; |
| 95 | + } |
| 90 | 96 | |
| 91 | | - lastTest = now; |
| 97 | + try |
| 98 | + { |
| 99 | + Thread.sleep( 250 ); |
| 100 | + } |
| 101 | + catch ( InterruptedException e ) |
| 102 | + { |
| 103 | + } |
| 92 | 104 | } |
| 93 | 105 | |
| 94 | | - try |
| 95 | | - { |
| 96 | | - Thread.sleep( 250 ); |
| 97 | | - } |
| 98 | | - catch ( InterruptedException e ) |
| 99 | | - { |
| 100 | | - } |
| 106 | + writer.write( Main.getRes().getString( "<-- Stopping Memory Log\n" ) ); |
| 107 | + writer.flush(); |
| 101 | 108 | } |
| 102 | | - |
| 103 | | - m_writer.write( Main.getRes().getString( "<-- Stopping Memory Log\n" ) ); |
| 104 | | - m_writer.flush(); |
| 105 | | - m_writer.close(); |
| 109 | + finally |
| 110 | + { |
| 111 | + writer.close(); |
| 112 | + } |
| 106 | 113 | } |
| 107 | 114 | catch ( IOException e ) |
| 108 | 115 | { |
| | @@ -131,44 +139,31 @@ |
| 131 | 139 | { |
| 132 | 140 | System.out.println( Main.getRes().getString( "Memory Tester Running...") ); |
| 133 | 141 | |
| 134 | | - // Locate the add and remove shutdown hook methods using reflection so |
| 135 | | - // that this class can be compiled on 1.2.x versions of java. |
| 136 | | - Method addShutdownHookMethod; |
| 137 | | - try { |
| 138 | | - addShutdownHookMethod = |
| 139 | | - Runtime.class.getMethod("addShutdownHook", new Class[] {Thread.class}); |
| 140 | | - } catch (NoSuchMethodException e) { |
| 141 | | - System.out.println( Main.getRes().getString( "Shutdown hooks not supported by current JVM.") ); |
| 142 | | - addShutdownHookMethod = null; |
| 143 | | - } |
| 142 | + c_theInstance = new Memory(); |
| 144 | 143 | |
| 145 | | - Memory app = new Memory(); |
| 146 | | - |
| 147 | | - // Create a Writer for the memory output |
| 148 | | - try |
| 149 | | - { |
| 150 | | - app.m_writer = new FileWriter( "memory.log" ); |
| 151 | | - } |
| 152 | | - catch ( IOException e ) |
| 153 | | - { |
| 154 | | - e.printStackTrace(); |
| 155 | | - return; |
| 156 | | - } |
| 157 | | - |
| 158 | | - // Register a shutdown hook using reflection. |
| 159 | | - if (addShutdownHookMethod != null) { |
| 160 | | - Runtime runtime = Runtime.getRuntime(); |
| 161 | | - Thread hook = new Thread( app, "shutdown-hook" ); |
| 162 | | - try { |
| 163 | | - addShutdownHookMethod.invoke(runtime, new Object[] {hook}); |
| 164 | | - } catch (IllegalAccessException e) { |
| 165 | | - System.out.println( Main.getRes().getString( "Unable to register shutdown hook: {0}", e.getMessage() ) ); |
| 166 | | - } catch (InvocationTargetException e) { |
| 167 | | - System.out.println( Main.getRes().getString( "Unable to register shutdown hook: {0}", e.getMessage() ) ); |
| 168 | | - } |
| 169 | | - } |
| 170 | | - |
| 171 | | - // Start the runner |
| 172 | | - new Thread( app, "runner" ).start(); |
| 144 | + // Register a shutdown hook. |
| 145 | + Runtime.getRuntime().addShutdownHook( new Thread( "shutdown-hook" ) |
| 146 | + { |
| 147 | + public void run() |
| 148 | + { |
| 149 | + System.out.println(Main.getRes().getString( "Stopping..." ) ); |
| 150 | + |
| 151 | + Thread runner = c_theInstance.m_runner; |
| 152 | + |
| 153 | + // Tell the main thread to stop. |
| 154 | + c_theInstance.m_runner = null; |
| 155 | + |
| 156 | + // Wait for the thread to actually stop cleanly. |
| 157 | + try |
| 158 | + { |
| 159 | + runner.join(); |
| 160 | + } |
| 161 | + catch ( InterruptedException e ) |
| 162 | + { |
| 163 | + } |
| 164 | + |
| 165 | + System.out.println(Main.getRes().getString( "Stopped." ) ); |
| 166 | + } |
| 167 | + } ); |
| 173 | 168 | } |
| 174 | 169 | } |
| | \ No newline at end of file |