Java memory usage increases when the application is in use, but does not decrease when not in use

I have a Java application that uses a lot of memory when used, but when the program is not in use, memory usage is not reduced.

Is there a way to get Java to free this memory? Since this memory was not needed at that time, I can understand that it reserves a small amount of memory, but Java just reserves all the memory that it has ever used. It also reuses this memory later, but there should be a way to get Java to release it when it is not needed.

System.gc does not work.

+10
java memory-management memory
Jun 11 '10 at 11:48
source share
5 answers

As stated in the comments, he is not sure that although the garbage collector has objects, it will return memory to the system.

Perhaps the Tuning Garbage Collection Outline provides a solution to your problem:

By default, the JVM increases or compresses the heap on each GC to maintain the ratio of free space to living objects in each collection within the specified range.

  • -XX:MinHeapFreeRatio - when the percentage of free space in a generation falls below this value, the generation will be expanded to meet this percentage. Default 40
  • -XX:MaxHeapFreeRatio - when the percentage of free space in the gene exceeds this value, the generation will decrease to correspond to this value. Default 70



Otherwise, if you suspect that you have a link leak, you can find out how, what and where the objects have leaked - keep track of the heap in JVisualVM (a tool that comes with the standard SDK). You can use this program to perform a bunch of dumps and get a histogram over the memory consumption of the object:

enter image description here

+7
Jun 11 '10 at 11:50
source share

What kind of memory do you mean? If this is RAM (as opposed to the amount of heap space used by the Java virtual machine itself), this may be normal. This is a relatively expensive memory allocation operation, so as soon as the JVM receives it, it is reluctant to return it, even if it is not needed at that time.

+2
Jun 11 '10 at 11:54 on
source share

Do you consider using a memory profiler? If you don’t have access to one, you can start by writing a jmap -histo <pid> bundle and a script entry to see the differences.

+1
Jun 11 '10 at
source share

System.gc has no guarantee whether it should free memory at startup. See Why is it bad practice to call System.gc ()?

Try setting up the Xmx JVM if it is set to a large value, and look in the JConsole to see what happens with memory usage and GC activity. Usually you will see a saw blade pattern.

You can also use the profiler to find out where the memory is used and to identify any leaks.

0
Jun 11 '10 at 11:51 on
source share

One of two things happens:

1) Links are distributed in your application. Are you sure you don't hang on objects when you no longer need them? If you do, Java must keep them in memory.

2) Java is working fine. You do not get any benefit from memory that you are not using.

0
Jun 11 '10 at 11:51 on
source share



All Articles