Reducing Java heap size

I have an application that uses a lot of memory by distinguishing the contents of two potentially huge (100k +) directories. It makes sense to me that such an operation would use a lot of memory, but once my operation is done, the heap will stay the same size.

Basically I have code that instantiates a class to store the file name, file size, path and modified date for each source and target file. I store additions, deletions and updates in other arrays. I then have clear()

my source and destination arrays (which may be 100K each by now), leaving the relatively small additions, deletions, and updated arrays to the left.

After I have clear()

my target and source arrays, the memory usage (as seen through VirtualVM and Windows Task Manager) does not decrease. I'm not experienced enough with VirtualVM (or any profiler) to figure out what all this memory is taking up. The VirtualVM dump heap lists the best objects with a saved size of several megabytes.

Anything to help me point in the right direction?

+3


source to share


3 answers


If the used heap crashes after a garbage collection, it probably works as expected. Java grows its heap when it needs more memory, but it doesn't free it - it prefers to store it if the application uses more memory again. See Is there a way to lower the Java heap when not in use? See this section for more information on why the heap doesn't shrink after the used heap is reduced.



+2


source


VM grows or shrinks the heap based on command line options -XX:MinHeapFreeRatio

and -XX:MaxHeapFreeRatio

. This will shrink the heap when the free percentage reaches -XX:MaxHeapFreeRatio

, by default, 70.



This is summarized in Oracle bug # 6498735 .

+2


source


Depending on your code, you may be generating memory leaks and the garbage collector simply cannot deal with them.

I would suggest measuring your code to find potential memory leaks. Once this is eliminated or fixed, I will start looking at the code itself for possible improvements.

Note that, for example, if you are using a try / catch / finally block. The finally block may not be called at all (or at least not immediately). If you do some freeing of resources in the finally block, this might be the answer.

However, read this thread for example here: http://www.toptal.com/java/hunting-memory-leaks-in-java

+1


source







All Articles