Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically promoting an object to Old Generation

An object can be promoted from Young Generation to Old Generation when it reaches the Tenuring Threshold or when the "TO" Survival Space is full when it is being transferred.

Therefore, my question is: In order to improve performance, if I know my object will be frequently used (referenced), is it possible to automatically/manually declare an object in Old/Permanent Generation so that not declaring it in the Eden would delay the necessity of Minor Garbage Collection, thus delaying the "Stop The World" event and improving the applications performance?

like image 503
Samuel Hošovský Avatar asked Sep 26 '22 07:09

Samuel Hošovský


1 Answers

Generally:

No - not for a specific single object.

In more detail:

An allocation rougly looks the following:

  1. Use thread local allocation buffer (TLAB), if tlab_top + size <= tlab_end. This is the fastest path. Allocation is just the tlab_top pointer increment.
  2. If TLAB is almost full, create a new TLAB in the Eden space and retry in a fresh TLAB.
  3. If TLAB remaining space is not enough but is still to big to discard, try to allocate an object directly in the Eden space. Allocation in the Eden space needs to be done using an atomic operation, since Eden is shared between all threads.
  4. If allocation in the Eden space fails (eden_top + size > eden_end), typically a minor collection occurs.
  5. If there is not enough space in the Eden space even after a Young GC, an attempt to allocate directly in the old generation is made.

"Hack":

The following parameter:

XX:PretenureSizeThreshold=size

This parameter is default set to 0, so it is deactivated. If set, it defines a size threshold for objects to be automatically allocated into the old generation.

BUT:

You should use this with care: Setting this parameter wrong may change your GCs behaviour drastically. And: Only a few percent of objects survive their first GC, so most objects don't have to be copied during the young GC.

Therefore, the young GC is very fast and you should not really need to "optimize" it by forcing object allocation to old generation.

Java parameters:

If you want to get an overview over possible Java paramters, run the following:

java -XX:+PrintVMOptions -XX:+AggressiveOpts -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+PrintFlagsFinal  -version

This will print all flags you can set.

Different garbage collectors:

Also keep in mind that there are different garbage collectors out there, and that it is planned that Java 9 should use the garbage first (G1) GC as default garbage collector, which again may handle big objects differently (by allocating them into humangous regions).

Additional source:

Stack overflow question: Size of Huge Objects directly allocated to Old Generation

like image 155
Markus Weninger Avatar answered Sep 28 '22 23:09

Markus Weninger