Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do heap memory usage and number of loaded classes keep increasing?

I am using JVM Explorer - link to JVM Explorer , to profile my Spring application. I have following questions about it.

  • Why 'Used Heap Memory' keeps increasing even after the application has started up and have not received any requests yet? (Image 1)

  • Why even after garbage collection and before receiving any requests 'Used Heap Memory' keeps increasing? (Image2)

  • Why after garbage collection, by sending some requests to the application number of loaded classes is increasing? Is not the application supposed to use previous classes? why is it just increasing almost everything (heap, number of loaded classes)? (Image3)

    After application starts up - enlarge image enter image description here

    After clicking on 'Run Garbage Collector' button. - enlarge image

enter image description here

After sending some requests to the application following completion of Garbage Collection Procedure - enlarge image

enter image description here

like image 984
Daniel Newtown Avatar asked Jun 29 '15 06:06

Daniel Newtown


People also ask

What causes high heap memory usage?

High heap usage occurs when the garbage collection process cannot keep up. An indicator of high heap usage is when the garbage collection is incapable of reducing the heap usage to around 30%.

How do you fix heap memory problems?

There are several ways to eliminate a heap memory issue: Increase the maximum amount of heap available to the VM using the -Xmx VM argument. Use partitioning to distribute the data over additional machines. Overflow or expire the region data to reduce the heap memory footprint of the regions.

Why does my Java process consume more memory than XMX?

What you have specified via the -Xmx switches is limiting the memory consumed by your application heap. But besides the memory consumed by your application, the JVM itself also needs some elbow room. The need for it derives from several different reasons: Garbage collection.

What happens when heap memory is full?

When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects. Note that the JVM uses more memory than just the heap.


1 Answers

Why 'Used Heap Memory' keeps increasing even after the application has started up and have not received any requests yet? (Image 1)

Something in your JVM is creating objects. You would need a memory profiler to see what is doing this. It could be part of Swing, or yoru application or another library.

BTW Most profiling tools use JMX which processes a lot of garbage. e.g. when I run FlightRecorder or VisualVM on some of my applications it shows the JMX monitoring is creating most of the garbage.

Why even after garbage collection and before receiving any requests 'Used Heap Memory' keeps increasing? (Image2)

Whatever was creating objects is still creating objects.

Why after garbage collection, by sending some requests to the application number of loaded classes is increasing?

Classes are lazily loaded. Some classes are not needed until you do something.

Is not the application supposed to use previous classes?

Yes, but this doesn't mean it won't need more classes.

why is it just increasing almost everything (heap, number of loaded classes)? (Image3)

Your application is doing more work.

If you wan't to know what work the application is doing, I suggest using a memory profiler like VisualVM or Flight Recorder. I use YourKit for these sort of questions.

Note: it takes hard work to tune a Java program so that it doesn't produce garbage and I would say most libraries only try to reduce garbage if it causes a known performance problem.

like image 77
Peter Lawrey Avatar answered Nov 06 '22 15:11

Peter Lawrey