简体   繁体   中英

Monitoring Java internal objects & memory usage

I have a Java web server running as a Windows service. I use Tomcat 8 with Java 1.8.* For a few months now, I've detected that the memory usage is increasing quite rapidly. I cannot make up for sure if it's heap or stack. The process starts with ~200MB and after a week or so, it can reach up to 2GB. Shortly after it will generate OutOfMemory exception (the memory usage will be 2GB - 2.5GB). This has repeated multiple times on multiple environments.

I would like to know if there's a way to monitor the process and view it's internal memory usage, even to the level of viewing which objects are using the most amount of memory. Can 'Java Native Memory Tracking' be used for this? This will help me to detect any memory leaks that might cause this.

Thanks in advance.

To monitor the memory usage of a Java process, I'd use a JMX client such as JVisualVM, which is bundled with the Oracle JDK:

https://visualvm.java.net/jmx_connections.html

To identify the cause of a memory leak, I'd instruct the JVM to take a heap dump when it runs out of memory (on the Oracle JVM, this can be accomplished by specifying -XX:-HeapDumpOnOutOfMemoryError when starting your Java program), and then analyze that heap dump using a tool such as Eclipse MAT .

quoting:

the process starts with ~200MB and after a week or so, it can reach up to 2GB. Shortly after it will generate OutOfMemory exception (the memory usage will be 2GB - 2.5GB).

The problem might not be as simple as seeing what java objects you have got in JVisualVM (eg millions of strings) What you need to do is identify the code that leaks. One way you could do that is to force the execution of particular code and then monitor the memory. The easiest way to force the execution of code inside classes/objects is to use a tool like https://github.com/lorenzoongithub/nudge4j (particularly since you are on java 8)

alternatively you could just wire up nashorn to a command line or run your progam via jjs https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/shell.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM