Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't the allocated heap memory shrinking when usage is below MaxHeapFreeRatio?

I have a java server task, which is hogging memory. For one I doubt it ever exceeded MinHeapFreeRatio, but that's speculation. It is more interesting that GC reduces the mature generation to roughly 2%, yet never reduces the allocated memory for the heap.

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 3221225472 (3072.0MB)

   NewSize          = 268435456 (256.0MB)
   MaxNewSize       = 268435456 (256.0MB)
   OldSize          = 805306368 (768.0MB)
   NewRatio         = 7
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 176160768 (168.0MB)

Heap Usage:
New Generation (Eden + 1 Survivor Space):
   capacity = 241631232 (230.4375MB)
   used     = 71657320 (68.3377456665039MB)
   free     = 169973912 (162.0997543334961MB)
   29.65565312351675% used
Eden Space:
   capacity = 214827008 (204.875MB)
   used     = 47322984 (45.130714416503906MB)
   free     = 167504024 (159.7442855834961MB)
   22.028414602320392% used
From Space:
   capacity = 26804224 (25.5625MB)
   used     = 24334336 (23.20703125MB)
   free     = 2469888 (2.35546875MB)
   90.78545232273838% used
To Space:
   capacity = 26804224 (25.5625MB)
   used     = 0 (0.0MB)
   free     = 26804224 (25.5625MB)
   0.0% used
concurrent mark-sweep generation:
   capacity = 2952790016 (2816.0MB)
   used     = 66930392 (63.829795837402344MB)
   free     = 2885859624 (2752.1702041625977MB)
   2.2666830908168447% used
Perm Generation:
   capacity = 45752320 (43.6328125MB)
   used     = 27404664 (26.13512420654297MB)
   free     = 18347656 (17.49768829345703MB)
   59.89786747426142% used
like image 721
Mantriur Avatar asked Aug 27 '12 22:08

Mantriur


People also ask

How do I reduce heap memory usage?

Set the Heap Size The heap must be at least large enough for all objects that are alive at the same time. Preferably the heap should be at least twice the size of the total amount of live objects, or large enough so that the JVM spends less time garbage collecting the heap than running Java code.

What affects heap size?

The heap size value is determined by the amount of memory available in the computer.

Why does heap size increase?

To improve the performance of common Delegated Administrator functions such as displaying pages and performing searches, you can increase the Java Virtual Machine (JVM) heap size used by the Web container to which Delegated Administrator is deployed.


3 Answers

There are apparently various factors that can cause MaxHeapFreeRatio to not be honoured:

  • The minimum heap size (-Xms) overrides this (AFAIK).
  • The -XX:ParallelGC switch inhibits heap shrinking - https://forums.oracle.com/forums/thread.jspa?messageID=6438432
  • The shrinkage only happens after a full GC - https://forums.oracle.com/forums/thread.jspa?messageID=9557861
like image 66
Stephen C Avatar answered Oct 27 '22 01:10

Stephen C


The amount of memory reserved from the operating system for the heap is determined by min heap and max heap, the parameters -Xms and -Xmx on the java command line. The various garbage collector ratios and other configurations are all internal to that and don't affect how much total memory JVM uses, just how it arranges things in that memory.

Commonly when people set up servers they set it so that -Xms and -Xmx are the same value, to avoid additional performance cost of resizing the heap and having to create contiguous memory space while the server is running if the heap needs to grow. This means that the amount of memory reserved from the operating system for heap will never shrink as a result of garbage collection, it just gets freed up to have new JVM data put in.

like image 21
Affe Avatar answered Oct 26 '22 23:10

Affe


In JRE 1.7 you can use -XX:+UseG1GC -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15 . However, to get memory shrinked you still need to invoke GC explicitly by calling System.gc().

like image 41
Piotr Tomiak Avatar answered Oct 26 '22 23:10

Piotr Tomiak