How to Profile JVM memory in the code?

后端 未结 5 1779
既然无缘
既然无缘 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: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;

提交回复
热议问题