How to Profile JVM memory in the code?

后端 未结 5 1732
既然无缘
既然无缘 2021-01-03 09:24

I am writing a servlet that I can\'t test in Eclipse, I need to run on server. I want to do memory profiling and pinpoint any leaks. So, I think I need write debug statement

相关标签:
5条回答
  • 2021-01-03 10:06

    Can't you use the built-in tool in the jdk jvisualvm?

    0 讨论(0)
  • 2021-01-03 10:10

    JConsole to rescue you!

    0 讨论(0)
  • 2021-01-03 10:12

    So, I think I need write debug statements that can tell me current memory usage.

    I think you are like me who prefers to profile in code instead of using an external profiling tool like JVisualVM or JConsole.

    You can use a tool called MemorySampler (written by me) from CoralBits that will tell you exactly in what line of your code memory is being allocated. Below an example:

    ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
    MemorySampler.start();
    for(int i = 0; i < strings.length; i++) queue.offer(strings[i]);
    for(int i = 0; i < strings.length; i++) queue.poll();
    MemorySampler.end();
    if (MemorySampler.wasMemoryAllocated()) MemorySampler.printSituation();
    

    Gives you the following output:

    Memory allocated on last pass: 24576
    Memory allocated total: 24576
    
    Stack Trace:
        java.util.concurrent.ConcurrentLinkedQueue.offer(ConcurrentLinkedQueue.java:327)
        TestGC.main(TestGC2.java:25)
    

    Now if you go to line 327 of the ConcurrentLinkedQueue source code, you will see that it allocates a Node instance there.

    Disclaimer: I am one of the developers of CoralBits.

    0 讨论(0)
  • 2021-01-03 10:13

    Profiling application memory and hunting down leaks would be a difficult task, not to mention misleading with simple debug statements. If you can't use a tool which remotely connects to your process, using hprof would be a good bet IMO. Also, have a look at the troubleshooting documentation here.

    But I still think it would be better if you tried to do the same locally (i.e. fixing leaks) if possible.

    0 讨论(0)
  • 2021-01-03 10:16

    So, I think I need write debug statements that can tell me current memory usage.

    That most likely won't help you much since the garbage collector makes it somewhat hard to find memory leaks by just looking at the counts (you don't really know when the gc runs and what it actually collects, it sometimes doesn't collect everything that is collectable). So you might need to make some memory snapshots and analyse them, i.e. see which objects (or which types of objects) are not collected and thus more and more instances are created.

    For this, take a look at the suggested tools (JVisualVM, JConsole).

    If you still want to get memory usage information from inside your program, try the classes in the java.lang.management package;

    0 讨论(0)
提交回复
热议问题