Java Out of memory automatic heap dump file name

前端 未结 2 1291
温柔的废话
温柔的废话 2021-02-07 03:03

I have several Java processes and I am trying to manage the heap dumps created when OOM error occur. When I say manage I mean

  • name the heap dump differently, based
2条回答
  •  时光说笑
    2021-02-07 03:46

    HeapDumpPath is a manageable VM option. This means you can set it to whatever you want in runtime using JMX.

        String pid = ManagementFactory.getRuntimeMXBean().getName();
        pid = pid.substring(0, pid.indexOf('@'));
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String fileName = "/tmp/heap_" + pid + "_" + date + ".dump";
    
        HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(
                ManagementFactory.getPlatformMBeanServer(),
                "com.sun.management:type=HotSpotDiagnostic",
                HotSpotDiagnosticMXBean.class);
        bean.setVMOption("HeapDumpOnOutOfMemoryError", "true");
        bean.setVMOption("HeapDumpPath", fileName);
    

提交回复
热议问题