6

I have install glowroot (java application monitoring) to my JVM. When my application idles, I get this kind graph formation of memory heap usage. The pattern seems almost uniform. Could someone please explain and point me to whatever blog post on why does the graph looks like that? I am curious.

enter image description here

snso
  • 309
  • 7
  • 15
  • 1
    This usually occurs when the JVM enters full garbage collection (full GC). The Java runtime is always trying to free memory that is no longer being used, but sometimes it can't clear memory faster than it is being used. At a certain point the JVM runs out of memory and needs to clear some space. It enters full GC. This is a stop the world process which attempts to free all un-referenced data. It can cause some unusual errors if you aren't careful, and the more objects you have the longer full GC can take. – flakes Dec 12 '17 at 07:08
  • This question should help you too [why a sawtooth shaped graph?](https://stackoverflow.com/questions/7219532/why-a-sawtooth-shaped-graph) – flakes Dec 12 '17 at 07:22

3 Answers3

7

The large-scale sawtooth pattern represents the memory utilization between GC cycles. The application is allocating objects steadily (the upsloping line) until the heap gets full enough for the VM to decide to run the GC (the point). Then the GC reclaims a large amount of garbage (the steep drop) and the process starts again.

The short spikes up and down are harder to understand. It is possible that the upward spikes represent anomalous "large" allocations (of short life-time objects) that are triggering a young generation cycle. The downward spikes might represent cached objects being freed in response to "memory pressure".

If you want a better understanding of the spikes, you need to look at the GC log messages, and try to correlate them with the graphs.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
4

It looks like that because (at the least) you're observing it. If your application did absolutely nothing, and there were no threads doing any allocations, you'd get a horizontal line for the heap.

However since you're observing it, there are things being done in the JVM to get that data back to you. That's why you get the ubiquitous sawtooth pattern seen in many, many profiling questions. A few examples below.

java memory leak, visualvm showing wrong data

visualvm monitors memory usage

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • 1
    "because you're observing it" Is an interesting insight. Not 100% sure that it's true, but it seems an important consideration! – markspace Feb 02 '19 at 22:31
1

This is the java garbage collection at work. Each drop is the garbage collector freeing memory by removing unused references. The reason why it grows again is simply because your application keeps creating new references.

You can also have a look at this post for a more in-depth explaination: Why a sawtooth shaped graph?

Stian
  • 685
  • 4
  • 12