How to print out Java jvm memory use for debugging purposes?

后端 未结 2 1565
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 15:22

I have a large Java app that uses massive amounts of memory at times and I would like to monitor this at intervals to see if heap or permgen limits are reached or have gotten ne

相关标签:
2条回答
  • 2021-02-05 15:54

    You can try visualVM, a Java profiler that goes with every JDK. You will find it in the "bin" folder.

    0 讨论(0)
  • 2021-02-05 16:01

    Here's a snippet from a piece of code we have that periodically logs the memory usage of our app:

    import java.lang.management.GarbageCollectorMXBean
    import java.lang.management.ManagementFactory
    import java.lang.management.MemoryPoolMXBean
    import java.lang.management.MemoryUsage
    
    log("Heap", ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
    log("NonHeap", ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage());
    List<MemoryPoolMXBean> beans = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean bean: beans) {
        log(bean.getName(), bean.getUsage());
    }
    
    for (GarbageCollectorMXBean bean: ManagementFactory.getGarbageCollectorMXBeans()) {
        log(bean.getName(), bean.getCollectionCount(), bean.getCollectionTime());
    }
    
    0 讨论(0)
提交回复
热议问题