Author: sco...@google.com Date: Tue Mar 31 13:55:44 2009 New Revision: 5139
Modified: changes/scottb/memory/dev/core/src/com/google/gwt/dev/util/Memory.java Log: SQUASH into initial infrastructure; make this compile against Java 5 SDK. Suggested by: spoon Modified: changes/scottb/memory/dev/core/src/com/google/gwt/dev/util/Memory.java ============================================================================== --- changes/scottb/memory/dev/core/src/com/google/gwt/dev/util/Memory.java (original) +++ changes/scottb/memory/dev/core/src/com/google/gwt/dev/util/Memory.java Tue Mar 31 13:55:44 2009 @@ -15,19 +15,38 @@ */ package com.google.gwt.dev.util; -import com.sun.management.HotSpotDiagnosticMXBean; - import java.io.File; -import java.io.IOException; import java.lang.management.ManagementFactory; +import java.lang.reflect.Method; /** * Utility methods for dealing with VM memory. */ public class Memory { + /** + * Number of times to call System.gc() before measuring the memory + * usage/dumping the heap. This value was arrived at through trial-and-error + * on the Sun JVM; memory usage seems to stabilize after 3 to 4 runs. We think + * the reason it requires multiple runs is due to the generational aspect of + * garbage collection. + */ + private static final int NUM_GC_COLLECTIONS = 4; + + /** + * Set this system property to a filename suffix to dump heaps into. + */ private static final String PROPERTY_DUMP_HEAP = "gwt.memory.dumpHeap"; + + /** + * Set this system property to dump memory usage at various points. + */ private static final String PROPERTY_DUMP_MEMORY = "gwt.memory.usage"; + + /** + * Time to start measuring since the last memory measurement/dump, or + * application startup. + */ private static long startTime; public static void initialize() { @@ -50,7 +69,7 @@ public static void maybeDumpMemory(String info) { long elapsed = System.currentTimeMillis() - startTime; if (System.getProperty(PROPERTY_DUMP_MEMORY) != null) { - for (int i = 0; i < 10; ++i) { + for (int i = 0; i < NUM_GC_COLLECTIONS; ++i) { System.gc(); } long heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); @@ -65,14 +84,15 @@ dumpFile = info + "-" + dumpFile; new File(dumpFile).delete(); try { - HotSpotDiagnosticMXBean bean; - bean = ManagementFactory.newPlatformMXBeanProxy( + Class<?> beanClass = Class.forName("com.sun.management.HotSpotDiagnosticMXBean"); + Object bean = ManagementFactory.newPlatformMXBeanProxy( ManagementFactory.getPlatformMBeanServer(), - "com.sun.management:type=HotSpotDiagnostic", - HotSpotDiagnosticMXBean.class); - bean.dumpHeap(dumpFile, true); + "com.sun.management:type=HotSpotDiagnostic", beanClass); + Method dumpHeapMethod = beanClass.getDeclaredMethod("dumpHeap", + String.class, Boolean.TYPE); + dumpHeapMethod.invoke(bean, dumpFile, true); System.out.println("(" + info + ") dumped heap into: " + dumpFile); - } catch (IOException e) { + } catch (Throwable e) { System.err.println("Unable to dump heap"); e.printStackTrace(); } --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---