zoukankan      html  css  js  c++  java
  • Programmatically dumping heap from Java applications 规格严格

    http://blogs.oracle.com/sundararajan/?page=7

    In the troubleshooting BOF, we demonstrated how to use the jmap -dump option to dump heap dump of a running application and showed how to browse/analyze the resulting binary heap dump using the jhat tool.

    One of the questions was how to programmatically dump the heap from applications. For example, you may want to dump multiple heap snapshots from your application at various points in time and analyze those off line using jhat. Yes, you can dump heap from your application -- but you have to do a bit of programming as shown below:

    
    import javax.management.MBeanServer;
    import java.lang.management.ManagementFactory;
    import com.sun.management.HotSpotDiagnosticMXBean;
    
    public class HeapDumper {
        // 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
         \*/
        static void dumpHeap(String fileName, boolean live) {
            // initialize hotspot diagnostic MBean
            initHotspotMBean();
            try {
                hotspotMBean.dumpHeap(fileName, live);
            } catch (RuntimeException re) {
                throw re;
            } catch (Exception exp) {
                throw new RuntimeException(exp);
            }
        }
    
        // initialize the hotspot diagnostic MBean field
        private static void initHotspotMBean() {
            if (hotspotMBean == null) {
                synchronized (HeapDumper.class) {
                    if (hotspotMBean == null) {
                        hotspotMBean = getHotspotMBean();
                    }
                }
            }
        }
    
        // get the hotspot diagnostic MBean from the
        // platform MBean server
        private static HotSpotDiagnosticMXBean getHotspotMBean() {
            try {
                MBeanServer server = ManagementFactory.getPlatformMBeanServer();
                HotSpotDiagnosticMXBean bean = 
                    ManagementFactory.newPlatformMXBeanProxy(server,
                    HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
                return bean;
            } catch (RuntimeException re) {
                throw re;
            } catch (Exception exp) {
                throw new RuntimeException(exp);
            }
        }
    
        public static void main(String[] args) {
            // default heap dump file name
            String fileName = "heap.bin";
            // by default dump only the live objects
            boolean live = true;
    
            // simple command line options
            switch (args.length) {
                case 2:
                    live = args[1].equals("true");
                case 1:
                    fileName = args[0];
            }
    
            // dump the heap
            dumpHeap(fileName, live);
        }
    }
    
    
    By including the above class in your application, you can call HeapDumper.dumpHeap whenever you want to dump the heap as shown in the sample main.

    Important note: while you can dump multiple heap snapshots from your application, you can not correlate the objects from multiple dumps. jmap tool uses object addresses as object identifiers - which vary between garbage collections [recall that GC may move objects which changes the object addresses]. But, you can correlate by aggregate stat such as histogram etc.

    Comments:

    [Trackback] A few weeks ago I blogged about how to programmatically access the JVM Monitoring information . Here is a small Java application that prints all the attributes of all the JVM Management & Monitoring MBeans . ...

    Posted by JMX, SNMP, Java, etc... on May 16, 2007 at 05:16 PM IST #

    Any chance the video of the JavaOne2007 BOF session (on analysing and browsing the heap dump) be made publicly available ? It would be very helpful to a lot of developers. Thanks in advance.

    Posted by Hanson Char on May 18, 2007 at 09:10 AM IST #

    I would find it more useful to be able to programmatically get the heap histogram (ideally in a thread that doesn't impact anything)

    Posted by Jack Shirazi on May 22, 2007 at 06:57 PM IST #

    Hi Jack Shirazi: You can start the VM with the option <code>-XX:+PrintClassHistogram</code>. With that option, heap histogram is printed whenever SIGQUIT signal is sent to the process. If you are okay with sending signals to the current process (say, by native code), you can send SIGQUIT programatically.

    Posted by A. Sundararajan on May 22, 2007 at 07:41 PM IST #

  • 相关阅读:
    10分钟教你用Python玩转微信之抓取好友个性签名制作词云
    1. 配置win7下odbc数据源找不到数据库驱动的问题
    1. 加签和会签的区别
    4. mysql 1449 : The user specified as a definer ('test'@'%') does not exist 解决方法
    1. 在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快
    37. sqlplus工具连接服务端或其他电脑的oracle方式
    36. Oracle查询数据库中所有表的记录数
    4. mysql 查看数据库中所有表的记录数
    35. Oracle监听器启动出错:本地计算机上的OracleOraDb11g_home1TNSListener服务启动后又停止了解决方案
    4. powerdesigner 生成sql脚本步骤
  • 原文地址:https://www.cnblogs.com/diyunpeng/p/2249009.html
Copyright © 2011-2022 走看看