Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are ratios between spaces/generations in the Java Heap constant?

I have read this article about virtual machine garbage collection tuning to understand the java garbage collector better. Each space has a Virtual heap space area which it can grow into as the heap space needed gets closer to the max heap size. This can be seen in this picture: Java GC arrangement of generations
(source: oracle.com)

You can set the ratio between Young Generation and Old (Tenured) Generation with the NewRatio parameter, and the ratio between Eden Space and Survivor Space with the SurvivorRatio parameter.

Recently this question was asked about finding out the default ratios for the heap spaces. It says you should use the PrintGCDetails parameter and calculate the ratios manually.

My question is: Does the size of the different heap spaces increase by the same ratio, thus keeping the ratio set between them at startup constant during the whole runtime of the application? For example, if Young Generation and Old/Tenured Generation has a default NewRatio of 3, and the initial reserved heap space is 100MB for Young, and 300MB for Old. If more memory needs to be reserved for the Old Space, lets say 300MB more making it 600MB total. Does memory reserved for Young Space also double to 200MB keeping the ratio intact?

like image 585
ghdalum Avatar asked Mar 15 '13 08:03

ghdalum


People also ask

How many generations is the heap divided into?

Actually, the memory heap is divided into 3 smaller parts or generations. The reason behind this division is to increase the performance of the object allocation process.

How is virtual heap space divided in Java?

The heap space is divided into the old and the new generation. The new generation includes the new object space (eden), and two survivor spaces. The JVM allocates new objects in the eden space, and moves longer lived objects from the new generation to the old generation.

What is new ratio in JVM?

The default NewRatio for the Server JVM is 2: the old generation occupies 2/3 of the heap while the new generation occupies 1/3. The larger new generation can accommodate many more short-lived objects, decreasing the need for slow major collections.

What is Survivor ratio in JVM?

The SurvivorRatio parameter controls the size of the two survivor spaces. For example, -XX:SurvivorRatio=6 sets the ratio between each survivor space and eden to be 1:6, each survivor space will be one eighth of the young generation.


2 Answers

I think you are referring to GC Ergonomics and the Adaptive Size Policy

  • a feature of the Hotspost GC that automatically adapts the sizes of the generations at runtime based on the current allocation behavior of the running application.
  • This feature is ON by default and controls/adapts the size of the generations at runtime.
  • in fact, some of the GC parameters will be ignored if you do not disable Adaptive Size policy, eg. -XX:SurvivorRatio=.

You can disable the Adaptive Size Policy by using the -XX:-UseAdaptiveSizePolicy. Once you disabled the AdaptiveSizePolicy, the GC will respect the initial size of the generations as specified by your startup parameters (e.g. -Xms, -Xmx, -XX:MaxNewSize=,-XX:NewSize=, -XX:SurvivorRatio=) and they will remain constant.

You can find more on Adaptive Size policy in UseAdaptiveSizePolicy and other jvm opts.

like image 130
Aleš Avatar answered Sep 17 '22 20:09

Aleš


PermGen vs Young generation ratio is partially maintained by JVM, but to say if ratio is maintained, the answer is No.

JVM7 has become sufficiently advanced and complicated where we shouldn't predict the allocation of generations inside heap.

In every GC cycle that JVM runs, it also does metric analysis to learn of current applications in-memory storage behavior. - If more objects are surviving the multiple cycles of GC, then PermGen is reduced and some of the space is allocated to YoungGeneration. Basically No. of objects x number of GC cycles they skip plays a criteria in dynamically adjusting the generation ratio.

I hope it helps, thanks.

like image 36
Vishal Verma Avatar answered Sep 20 '22 20:09

Vishal Verma