Stop Coding!

The Unofficial Flex Compiler Blog

More On The Incredible Shrinking JVM Heap…

with 4 comments

In the previous post, I talked about the unusual phenomenon of seeing JVM shrink its heap and returning unused memory back to the operating system when running HFCD. But I didn’t talk much about how it happened. This post is to follow up on that…

First of all, the two key JVM parameters responsible for resizing JVM heap are:

  • -XX:MinHeapFreeRatio (default is 40)
  • -XX:MaxHeapFreeRatio (default is 70)

According to Sun (now Oracle),

By default, the virtual machine grows or shrinks the heap at each collection to try to keep the proportion of free space to live objects at each collection within a specific range. This target range is set as a percentage by the parameters -XX:MinHeapFreeRatio=<minimum> and -XX:MaxHeapFreeRatio=<maximum>, and the total size is bounded below by -Xms and above by -Xmx .

It goes on to say:

With these parameters if the percent of free space in a generation falls below 40%, the size of the generation will be expanded so as to have 40% of the space free, assuming the size of the generation has not already reached its limit. Similarly, if the percent of free space exceeds 70%, the size of the generation will be shrunk so as to have only 70% of the space free as long as shrinking the generation does not decrease it below the minimum size of the generation.

Both Flash Builder and HFCD use the default -XX:MinHeapFreeRatio and -XX:MaxHeapFreeRatio setting. As a matter of fact, by default, HFCD only sets -Xmx1024m and lets JVM tune itself.

Now take a look at the heap usage of Flash Builder when running the compiler internally. It looks obvious that the requirement for -XX:MaxHeapFreeRatio is not met. Therefore, no shrinking.

But take a look a the heap usage of HFCD. The used heap (blue line) drops so much (i.e. free space well exceeds 70% at the end) that JVM is able to resize the heap downward based on the default -XX:MaxHeapFreeRatio setting.

I also tried -server (HotSpot Server VM) and -client (HotSpot Client VM) on HFCD and manage to get the downward heap resizing to kick in.

However, there are two caveats you need to know:

  1. By default, JVM uses serial GC. If you start JVM by using the parallel GC (i.e. -XX:+UseParallelGC), you will NOT see this kind of huge downward resizing. I could be wrong but I think it’s because the parallel GC uses the adaptive size policy to control the size of the generations and ignore the -XX:MaxHeapFreeRatio setting.
  2. The charts above were generated by running Java 5/6 on Windows. The JVM on Mac OSX does adjust heap size downward from time to time but they are all very minor downward adjustments and it does not do that major downward resizing after finishing a full build.

There you go.

Advertisements

Written by Clement Wong

April 12, 2010 at 11:21 am

4 Responses

Subscribe to comments with RSS.

  1. asking. how do i can resizing up heap size on my mobphone (cross pd 100t. heap: 3027000 b). its causing limit on installing apps. plz reply to my email. thk for your help. my regard.

    andi alamsyah

    September 4, 2011 at 1:15 pm

  2. […] ise kullanılmayan alanın geri verilmesi sağlanabilir. Örneğin ilk açılışta çok bellek lazımdır ve üst sınır yüksek tutulmalıdır. Ancak […]

    JVM Havuzları

    March 21, 2014 at 6:20 am

  3. […] The first link’s answer is actually wrong/out-dated, but 2 comments link to articles found here and here which seem promising. The thing is, I’ve tried all of the different Garbage […]

  4. […] above. The first link's answer is actually wrong/out-dated, but 2 comments link to articles found here and here which seem promising. The thing is, I've tried all of the different Garbage Collectors […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: