DEV Community

Jashua
Jashua

Posted on

JVM memory leak, where to begin?

How do you find the root of the problem?

I just made a simple Spring Boot app to test an API, it began at 200mb, slowly but surely increasing, after 8 hours is nearly at 300mb

I haven't even touched it, it's just idle in the background

Top comments (12)

Collapse
 
waitamiwrong profile image
rabbie • Edited

You can use a profiler to inspect the JVM at run time.

  1. jmap is a free basic CLI tool you can use to inspect a JVM. You can dump the heap, monitor live heap memory and get a simple histogram of objects using this tool.

  2. jconsole is an upgrade to jmap with a GUI and more options.

  3. VisualVM is a great free tool with way more options.

All the above tools are shipped free with a JDK distribution. You can find all of them in <JDK>/bin directory.

  1. Also, you can use something heavyweight, like JProfiler as well.

You can use all these tools to connect to a running JVM and inspect the internals.

Collapse
 
jouo profile image
Jashua

I'm checking out those as we speak, thank you very much!

Collapse
 
josephtaylor profile image
J. Taylor O'Connor

Are you running it with any memory args like -Xmx ?

Collapse
 
jouo profile image
Jashua

No I actually didn't, would that be the problem?

I removed them because I wanted to see if the little app was stable, then I came across said memory leak

Collapse
 
josephtaylor profile image
J. Taylor O'Connor

I would try setting a max heap and see how that affects it. I can’t remember exactly what the calculation is but the JVM sets its default memory settings based on the overall system memory. If it’s assuming that it has a lot of memory available to it, it may allow the heap to grow without garbage collecting it. So, could be if you constrain the heap some more, the JVM will perform more garbage collection and keep the overall usage lower.

But, like other people are saying, your best window into what’s going on memory-wise is to take heap dumps and analyze them with a memory analyzer (MAT is a good free one that does leak suspect reports).

Thread Thread
 
smuschel profile image
smuschel

I think this would be your best option. Restrict max heap size an check if your app runs into an OutOfMemoryError. If that happens, use one of the mentioned tools to analyse your heap (take snapshots of your heap at different points in time, see what objects might accumulate).
But first, I'd also recommend to verify there's a memory issue

Thread Thread
 
jouo profile image
Jashua

I did it yesterday, and ran into a memory error (not sure which), logs said the JVM killed the child or something... I should've saved that log I'm sorry

But I do am working on it, already taking a course on JVM memory :)

Collapse
 
danielwoodsdeveloper profile image
Daniel Woods

You'll want to use a tool like JavaMelody or JProfiler to be able to work out what's going on inside the JVM. You may want to look at taking a Heap Dump and analysing it; JProfiler has a pretty good Heap Walker which will allow you to sort objects by size or the number of instances, and most likely your top object here is the leak.

Collapse
 
jouo profile image
Jashua

thank you very much :D

Collapse
 
sunitk profile image
Sunit Katkar

Share your code repo.

Collapse
 
jouo profile image
Jashua

I forgot to give an update!

I wasn't able to edit it because most of the code was from work, so I made a quick project from scratch with nothing but the API and that one was working fine, therefore I had nothing to show you :(

But I'm already working out the problem based on the suggestions given

Collapse
 
jouo profile image
Jashua

Will do tomorrow, I need to remove a couple things from work, I will reply to you again tomorrow