DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Memory Dump From Scala

08.02.2008
| 3280 views |
  • submit to reddit
        
// http://blogs.sun.com/sundararajan/entry/programmatically_dumping_heap_from_java
// see also:
// http://blogs.sun.com/sundararajan/entry/what_s_in_my_java
// http://blogs.sun.com/sundararajan/entry/querying_java_heap_with_oql
//
// The dump can be opened in the convenient NetBeans Profiler, menu "Profile / Load Heap Dump ...".

/**
 * @param fileName name of the heap dump file
 * @param live flag that tells whether to dump
 *             only the live objects
 */
def heapDump (fileName: String, live: Boolean): Unit = {
  import javax.management.MBeanServer;
  import java.lang.management.ManagementFactory;
  import com.sun.management.HotSpotDiagnosticMXBean;

  // This is the name of the HotSpot Diagnostic MBean
  val HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";

  // get the hotspot diagnostic MBean from the platform MBean server
  val server: MBeanServer = ManagementFactory.getPlatformMBeanServer ();
  val bean = ManagementFactory.newPlatformMXBeanProxy (server,
    HOTSPOT_BEAN_NAME, classOf[HotSpotDiagnosticMXBean]) .asInstanceOf[HotSpotDiagnosticMXBean];

  bean.dumpHeap (fileName, live);
}