TransWikia.com

How to reduce committed memory usage in Java

Stack Overflow Asked by Simon Hu on January 9, 2021

I use Java Swing to create a simple GUI software. There exists a button on the panel that will trigger the generation of an ArrayList with a huge size. Each time I click this button the memory usage of my java program will increase significantly on my task manager, therefore I doubt that whether there exists the memory leakage problem.

After, I investigate my program by Jprofile and found (pictures below):

enter image description here
enter image description here

As the pictures show, after the huge ArrayList is useless, the GC mechanism seems to work properly (really?). However, I want my program to reduce the committed size (instead of more free size) so that the total memory usage would be decreased when my program is idle.

I do not understand the Java memory usage, can anyone explain to me? How can I achieve my goal or the situation of my pictures is inevitable?

One Answer

However, I want my program to reduce the committed size (instead of more free size) so that the total memory usage would be decreased when my program is idle.

The easy way to limit committed memory usage for a JVM is to to use -Xmx to set the maximum heap size. The problem is that if you limit the heap size too severely you are liable to trigger OutOfMemoryErrors.

Getting the JVM to give back memory is another matter. A JVM typically will give some memory back to the OS if it thinks that the heap has gotten too large. However, the default GC settings for this mean that the JVM / GC takes multiple GC cycles before it will give memory back. (A typical Java application's memory demand fluctuates, and it is inefficient for the GC keep resizing the heap larger and smaller again.)

This Q&A describes the process in more detail:

It is worth noting that the resize decision making (typically) happens after a Full GC. But a program that is idle won't be generating much garbage, and probably won't trigger the GC often, if at all. It might be necessary to manually run the GC by calling System.gc() to "encourage" it to give back memory. But there are downsides in doing that too:

Overall, my advice would be to set a reasonable value for -Xmx and stop worrying. If system performance gets too bad (e.g. due to Java using too much memory), buy more memory or a "new" machine that can take more memory. (2 minutes with Google found me a refurbished low-end desktop PC with 8GB RAM for AU$ 199.)

Answered by Stephen C on January 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP