2

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.

4
  • 1
    Use JVisualVM - it comes bundled with the JDK. Although 2GB for a webserver isn't really that much... Commented Jun 5, 2016 at 10:57
  • @BoristheSpider, I agree its not much, but it does crash. I've found 'Java Native Memory Tracking' are you familiar with it? Commented Jun 5, 2016 at 11:01
  • It's heap, you have a very odd java program when you run out of stack. Besides visualvm: docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/… comes with java 8. Commented Jun 5, 2016 at 11:01
  • @zapl, In order to record my server, should I add the JVM parameters only? or add a new JVM connection to the Java Missions Control? Commented Jun 5, 2016 at 11:45

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is it preferable to use over 'Java Mission Control'? and recording the 'flight'?
I have never used the flight recorder. My understanding is that it is a commercial JVM feature and requires a licence, and the free tools I have mentioned in my answer worked well for me.
Will I be able to export the monitoring to a log file?
0

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 (e.g 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.