-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add heap-dump to MemoryLeakVerifier functionality
Adjust handling of InterruptedException to return quickly
- Loading branch information
Showing
4 changed files
with
156 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.dstadler.commons.testing; | ||
|
||
import com.sun.management.HotSpotDiagnosticMXBean; | ||
|
||
import java.io.IOException; | ||
import java.lang.management.ManagementFactory; | ||
|
||
public class HeapDump { | ||
// This is the name of the HotSpot Diagnostic MBean | ||
private static final String HOTSPOT_BEAN_NAME = | ||
"com.sun.management:type=HotSpotDiagnostic"; | ||
|
||
// field to store the hotspot diagnostic MBean | ||
private static volatile HotSpotDiagnosticMXBean hotspotMBean; | ||
|
||
/** | ||
* Call this method from your application whenever you | ||
* want to dump the heap snapshot into a file. | ||
* | ||
* @param fileName name of the heap dump file | ||
* @param live flag that tells whether to dump | ||
* only the live objects | ||
*/ | ||
public static void dumpHeap(String fileName, boolean live) throws IOException { | ||
// initialize hotspot diagnostic MBean | ||
initHotspotMBean(); | ||
hotspotMBean.dumpHeap(fileName, live); | ||
} | ||
|
||
// initialize the hotspot diagnostic MBean field | ||
private static void initHotspotMBean() throws IOException { | ||
if (hotspotMBean == null) { | ||
synchronized (HeapDump.class) { | ||
if (hotspotMBean == null) { | ||
hotspotMBean = getHotspotMBean(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// get the hotspot diagnostic MBean from the | ||
// platform MBean server | ||
private static HotSpotDiagnosticMXBean getHotspotMBean() throws IOException { | ||
return ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), | ||
HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/org/dstadler/commons/testing/HeapDumpTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.dstadler.commons.testing; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class HeapDumpTest { | ||
@Test | ||
public void testDumpHeap() throws IOException { | ||
File file = File.createTempFile("HeapDumpTest", ".hprof"); | ||
assertTrue(file.delete()); | ||
assertFalse(file.exists()); | ||
|
||
try { | ||
HeapDump.dumpHeap(file.getAbsolutePath(), true); | ||
} finally { | ||
assertTrue(file.exists()); | ||
assertTrue(file.delete()); | ||
} | ||
|
||
// a second time to cover singleton | ||
try { | ||
HeapDump.dumpHeap(file.getAbsolutePath(), true); | ||
} finally { | ||
assertTrue(file.exists()); | ||
assertTrue(file.delete()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDumpHeapFailsOnWrongFilename() throws IOException { | ||
File file = File.createTempFile("HeapDumpTest", ".hprof"); | ||
assertTrue(file.delete()); | ||
assertFalse(file.exists()); | ||
assertTrue(file.mkdirs()); | ||
|
||
try { | ||
HeapDump.dumpHeap(file.getAbsolutePath(), true); | ||
fail("Should fail to write the dump in this case, tried for " + file.getAbsolutePath()); | ||
} catch (IOException e) { | ||
// expected in this case | ||
} finally { | ||
assertTrue(file.exists()); | ||
assertTrue(file.delete()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters