2

I am trying to use JConsole to see if I have a memory leak but do not know what to look for. If I had to guess, the memory usage should always go up, despite garbage collection, like this:

enter image description here

As you can see in my other SO question, I'm seeing a jagged edge, where the memory usage goes up—even if the browser is closed and no requests are hitting my local Tomcat server—and then go down.

enter image description here

What does a memory leak in Java "look like" in JConsole?

Community
  • 1
  • 1
jds
  • 7,910
  • 11
  • 63
  • 101
  • 1
    It really depends on a lot of things, especially which garbage collector you've chosen, but what I usually look for is the fact that over time, heap usage continues going up. So in the second doodle, the bottom-most point in the "valleys" gradually creeps upward. – Roddy of the Frozen Peas Sep 22 '15 at 19:14
  • @RoddyoftheFrozenPeas, and I guess how quickly it creeps upward is dependent upon the kind of leak. – jds Sep 22 '15 at 19:31
  • After each full collection, the amount used keeps going up. Though this could mean you just need more memory than your program as been given and if you gave it more it would faltten out. – Peter Lawrey Sep 22 '15 at 19:43

1 Answers1

2

I think the best way to learn what a memory leak looks like is to do an experiment. Try something like this to create a memory leak:

    Collection<Object> data = new LinkedList<>();
    while(true) {
        long[] canBeGarbageCollected = new long[10000];
        long[] canNotBeGarbageCollected = new long[100];
        data.add(Arrays.asList(canNotBeGarbageCollected));
    }

Besides JConsole you also have VisualVM that has a nice plugin called "Visual GC". Java Mission Control with Flight Recording can give you a lot of details. A very powerful tool exists on the command line as well:

jstat -gcutil -h20 $PID 1000

This will show that a memory leak will eventually lead to 100% Old space utilization (O) and the CPU will spend more and more time doing full garbage collections (FGCT)