001: package org.tanukisoftware.wrapper.test;
002:
003: /*
004: * Copyright (c) 1999, 2006 Tanuki Software Inc.
005: *
006: * Permission is hereby granted, free of charge, to any person
007: * obtaining a copy of the Java Service Wrapper and associated
008: * documentation files (the "Software"), to deal in the Software
009: * without restriction, including without limitation the rights
010: * to use, copy, modify, merge, publish, distribute, sub-license,
011: * and/or sell copies of the Software, and to permit persons to
012: * whom the Software is furnished to do so, subject to the
013: * following conditions:
014: *
015: * The above copyright notice and this permission notice shall be
016: * included in all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
020: * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
021: * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
022: * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
023: * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
024: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
025: * OTHER DEALINGS IN THE SOFTWARE.
026: *
027: *
028: * Portions of the Software have been derived from source code
029: * developed by Silver Egg Technology under the following license:
030: *
031: * Copyright (c) 2001 Silver Egg Technology
032: *
033: * Permission is hereby granted, free of charge, to any person
034: * obtaining a copy of this software and associated documentation
035: * files (the "Software"), to deal in the Software without
036: * restriction, including without limitation the rights to use,
037: * copy, modify, merge, publish, distribute, sub-license, and/or
038: * sell copies of the Software, and to permit persons to whom the
039: * Software is furnished to do so, subject to the following
040: * conditions:
041: *
042: * The above copyright notice and this permission notice shall be
043: * included in all copies or substantial portions of the Software.
044: */
045:
046: import java.io.FileWriter;
047: import java.io.IOException;
048: import java.io.Writer;
049: import java.lang.reflect.InvocationTargetException;
050: import java.lang.reflect.Method;
051:
052: /**
053: *
054: *
055: * @author Leif Mortenson <leif@tanukisoftware.com>
056: */
057: public class Memory implements Runnable {
058: private Writer m_writer;
059: private Thread m_runner;
060:
061: /*---------------------------------------------------------------
062: * Runnable Method
063: *-------------------------------------------------------------*/
064: public void run() {
065: if (m_runner == null) {
066: // This is the runner
067: m_runner = Thread.currentThread();
068: } else {
069: System.out.println("Stopping...");
070: // This is the shutdown hook. Sloppy code, but simple :-)
071: m_runner = null;
072: return;
073: }
074:
075: long startTime = System.currentTimeMillis();
076: long lastTest = startTime;
077: try {
078: m_writer.write("--> Starting Memory Log\n");
079: m_writer.flush();
080:
081: while (m_runner != null) {
082: long now = System.currentTimeMillis();
083: System.out.println("Running for " + (now - startTime)
084: + "ms...");
085:
086: if (now - lastTest > 15000) {
087: Runtime rt = Runtime.getRuntime();
088: System.gc();
089: long totalMemory = rt.totalMemory();
090: long freeMemory = rt.freeMemory();
091: long usedMemory = totalMemory - freeMemory;
092:
093: m_writer.write("total memory="
094: + pad(totalMemory, 10) + ", used="
095: + pad(usedMemory, 10) + ", free="
096: + pad(freeMemory, 10) + "\n");
097: m_writer.flush();
098:
099: lastTest = now;
100: }
101:
102: try {
103: Thread.sleep(250);
104: } catch (InterruptedException e) {
105: }
106: }
107:
108: m_writer.write("<-- Stopping Memory Log\n");
109: m_writer.flush();
110: m_writer.close();
111: } catch (IOException e) {
112: e.printStackTrace();
113: }
114: }
115:
116: /*---------------------------------------------------------------
117: * Methods
118: *-------------------------------------------------------------*/
119: private static final String PADDING = " ";
120:
121: private String pad(long n, int len) {
122: String s = Long.toString(n);
123: int sLen = s.length();
124: if (sLen < len) {
125: s = s + PADDING.substring(0, len - sLen);
126: }
127: return s;
128: }
129:
130: /*---------------------------------------------------------------
131: * Main Method
132: *-------------------------------------------------------------*/
133: public static void main(String[] args) {
134: System.out.println("Memory Tester Running...");
135:
136: // Locate the add and remove shutdown hook methods using reflection so
137: // that this class can be compiled on 1.2.x versions of java.
138: Method addShutdownHookMethod;
139: try {
140: addShutdownHookMethod = Runtime.class.getMethod(
141: "addShutdownHook", new Class[] { Thread.class });
142: } catch (NoSuchMethodException e) {
143: System.out
144: .println("Shutdown hooks not supported by current JVM.");
145: addShutdownHookMethod = null;
146: }
147:
148: Memory app = new Memory();
149:
150: // Create a Writer for the memory output
151: try {
152: app.m_writer = new FileWriter("memory.log");
153: } catch (IOException e) {
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,
164: new Object[] { hook });
165: } catch (IllegalAccessException e) {
166: System.out.println("Unable to register shutdown hook: "
167: + e.getMessage());
168: } catch (InvocationTargetException e) {
169: System.out.println("Unable to register shutdown hook: "
170: + e.getMessage());
171: }
172: }
173:
174: // Start the runner
175: new Thread(app, "runner").start();
176: }
177: }
|