Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MaxTenuringThreshold - how exactly it works?

We know that there's few main memory domains: Young, Tenured (Old gen) and PermGen.

  • Young domain is divided into Eden and Survivor (with two).
  • OldGen is for surviving objects.

MaxTenuringThreshold keeps objects from being finally copied to the OldGen space too early. It's pretty clear and understandable.

But how does it work? How is the garbage collector dealing with these objects which are still surviving till MaxTenuringThreshold and in what way? Where are they located?

Objects are being copied back to Survivor spaces for garbage collection.. or does it happen somehow else?

like image 730
VirtualVoid Avatar asked Nov 24 '12 17:11

VirtualVoid


People also ask

What is MaxTenuringThreshold?

The Java command line parameter -XX:MaxTenuringThreshold specifies for how many minor GC cycles an object will stay in the survivor spaces until it finally gets tenured into the old space.

What is InitiatingHeapOccupancyPercent?

Sets the number of parallel marking threads. Sets n to approximately 1/4 of the number of parallel garbage collection threads ( ParallelGCThreads ). -XX:InitiatingHeapOccupancyPercent=45. Sets the Java heap occupancy threshold that triggers a marking cycle. The default occupancy is 45 percent of the entire Java heap.

What is SurvivorRatio?

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.


1 Answers

Each object in Java heap has a header which is used by Garbage Collection (GC) algorithm. The young space collector (which is responsible for object promotion) uses a few bit(s) from this header to track the number of collections object that have survived (32-bit JVM use 4 bits for this, 64-bit probably some more).

During young space collection, every single object is copied. The Object may be copied to one of survival spaces (one which is empty before young GC) or to the old space. For each object being copied, GC algorithm increases it's age (number of collection survived) and if the age is above the current tenuring threshold it would be copied (promoted) to old space. The Object could also be copied to the old space directly if the survival space gets full (overflow).

The journey of Object has the following pattern:

  • allocated in eden
  • copied from eden to survival space due to young GC
  • copied from survival to (other) survival space due to young GC (this could happen few times)
  • promoted from survival (or possible eden) to old space due to young GC (or full GC)

the actual tenuring threshold is dynamically adjusted by JVM, but MaxTenuringThreshold sets an upper limit on it.

If you set MaxTenuringThreshold=0, all objects will be promoted immediately.

I have few articles about java garbage collection, there you can find more details.

like image 190
Alexey Ragozin Avatar answered Sep 24 '22 05:09

Alexey Ragozin