7

I am running from time to time in eclipse tasks that require very big amount of memory. So jvm while task is running swallows about 2-3gb of RAM, that is ok. But once jvm took that memory it does not release it and I have a situation when used memory in the heap is about 200mb with total heap size about 3gb and that is really unwanted as other programs are starving for memory.

I tried Max/MinHeapFreeRatio parameters to force jvm to reduce consumption of an unused memory. That is my eclipse config.ini file:

-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.100.v20110502
-vm
c:/Program Files/Java/jdk1.6.0_26/bin/javaw.exe
-showlocation
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vmargs
-Duser.name=Michael Nesterenko
-Dosgi.requiredJavaVersion=1.5
-Xms512m
-Xmx4096m
-XX:MinHeapFreeRatio=10
-XX:MaxHeapFreeRatio=30

But that does not help, I still have situations when there are lots of unused memory.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
michael nesterenko
  • 14,222
  • 25
  • 114
  • 182
  • Is it practical to have two different short-cuts/shell-scripts for starting Eclipse, with different sets of parameters for when you have to do the memory-intensive tasks? – matt freake Jun 21 '12 at 13:08

1 Answers1

4

Java allocates the maximum heap size when it starts as virtual memory. As it uses the memory it get allocated as real main memory. It never shrinks this size. The only way to release this memory is for the program to exit.

Java will compact its memory usage so many of the pages it has used before can be swapped to disk, however this can have a significant performance hit if it needs them again.

Trying
  • 14,004
  • 9
  • 70
  • 110
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 5
    I disagree. The memory can shrinks. From this doc (http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html): `-XX:MaxHeapFreeRatio=70 Maximum percentage of heap free after GC to avoid shrinking.` That means if more than 70% of heap is free the heap will shrink. Also according to this bug report (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6498735) : `The HotSpot JVM already will release memory back to the operating system if it is not needed for live Java objects.` – alain.janinm Jun 21 '12 at 13:13
  • 1
    The heap used will shrink, however the resident size of the application does not. `I don't know what the bug reviewer is talking about either. I've NEVER seen the Virtual Memory footprint shrink. ` – Peter Lawrey Jun 21 '12 at 13:24
  • Actually virtual memory is not a problem, it can be any size, as it is process private. I need to reduce process working set - that is physical RAM usage. – michael nesterenko Jun 21 '12 at 13:30
  • Read the 2nd comment by stolsvik on the linked bug. (I'm not saying it is true, but it might explain why you've never seen heap shrinking.) – Stephen C Jun 21 '12 at 13:30
  • 1
    I find there is often a descrepency between optimisations the JVM should do and what you really find. An example is escape analysis. In theory it can remove alot of object creation, but in reality its very hard for find an example where this happens. – Peter Lawrey Jun 21 '12 at 13:36
  • @PeterLawrey If the `heap used` is shrink then the commited size doesn't change, there is just more free memory. Whereas the note in Evaluation says `release memory back to the operating system` so it means that the commited heap size is reduce. I've a graph that show that made from a Dijkstra prog with MXBeans, I will take some time to post an answer with that. – alain.janinm Jun 21 '12 at 13:40
  • @PeterLawrey If you have the time take a look at this code : https://github.com/alainjm/dijkstra_memory/tree/master/src, run it with 2 parameters (like 1000 1000) and you will see the evolution of the commited heap, in this case (at least on my machine) the commited heap decrease sometimes. – alain.janinm Jun 21 '12 at 14:18
  • I see the committed memory reported is between 40 - 55 MB but the "private memory" in task manager is around 240 MB. Perhaps the OS only reclaims memory if it needs to ?? – Peter Lawrey Jun 21 '12 at 14:50
  • @PeterLawrey Ok you are on Windows or you have 1 core no? I'm using Ubuntu with 4 core and I use about 4 times more memory :). I made this for a report and I've also draw graph from the result I've got (with a Debian 4 core) you can see one on github. Your comment about the task manager is true I've never noticed that before... The memory value stay the same, I don't know if we can rely on that btw. Where goes the uncommited memory if the Java process doesn't release memory? I guess the answer probably depends on the JVM implementation and the OS. I lack of knowledges here sorry... – alain.janinm Jun 21 '12 at 17:17
  • 1
    @alain.janinm Its a Windows 7 box with 8 cores and 32 GB of memory (about 23 GB free) I imagine with a high free memory, the OS might not take back memory because it doesn't need to. – Peter Lawrey Jun 22 '12 at 07:35