Question

This question is limited in scope to HotSpot generations. Is there any way to programmatically find out in which generation a particular instance lives. Data such as:

  • Young or old generation?
  • If young, which survivor space?
  • Inside TLAB? Which thread?

Any technique (ex., BTrace, JVMTI) works so long as I can do something like this:

Object x = new Object();
HotSpotGenerationInfo info = HotSpotGenerationUtil.getInfo(x);

Beggars can't be choosers but ideally I could also learn when the instance of interest was being moved from one generation to another at the moment it happens (i.e., event callback based -- not interested in the delay & overhead implicit in polling.)

Not interested in answers that just say "no" without justification :)

Was it helpful?

Solution

As far as I know, you can not directly query which memory pool an object currently lives in. However, objects are promoted to a different memory pool by a garbage collection run, and you can query the number of major/minor gc runs since VM start using JMX. If you additionally take note of these counters when the object is created, you can reconstruct whether there was a GC since and from that which pool the object lives in.

OTHER TIPS

There's an additional complication to the "count the number of GCs since the object was created" approach - it doesn't take into account premature object promotion.

If the survivor spaces are basically too small, and memory pressure from Eden (ie the rate of objects surviving at least once) is high, then objects will be promoted to tenured before they hit the full tenuring threshold.

In real examples, healthy applications will typically have non-zero percentages of premature promotion. In fact, a 0% premature promotion rate is a really bad sign - it says that your survivor spaces are much, much too big and you're wasting a lot of memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top