0

I was told not to create too many long-lived objects to get a better gc performance. Because long-lived objects will be moved to old gen. And it's more expensive to collect objects in old gen.

But what does long-lived mean? Is 100 milliseconds too long? And what about 10 seconds?

My app takes 15G heap memory and uses G1 gc, I can't find some configuration like 'how long to move an object to old gen'

T.Tony
  • 495
  • 3
  • 15
  • Don't design an application around such a mantra. Keep objects as long as you need them. The only consequence of this recommendation should be that you should not keep objects longer than necessary, e.g. not to try to cache or reuse ordinary objects. – Holger Jun 12 '18 at 11:50

2 Answers2

2

The tenuring threshold is the number of times an object can survive a young gen collection before being promoted to the old gen. This can be configured with these options:

  • -XX:InitialTenuringThreshold: The initial tenuring threshold (default is 7).
  • -XX:MaxTenuringThreshold: Maximum tenuring threshold (default is 15 for the parallel collector and 4 for CMS).
  • -XX:+PrintTenuringDistribution: Print tenuring age information.
Hearen
  • 7,420
  • 4
  • 53
  • 63
jspcal
  • 50,847
  • 7
  • 72
  • 76
1

Long-lived objects are objects that survive enough minor collections and are 'moved' to old generation.

At first GC, live objects are moved to survivor space from new generation. Then, after a number of gcs, they are tenured to old space.

As shown here, survivor doc, the number of gc objects survive before being tenured depend on the survivor space size.

At each GC, the JVM determines the number of times an object can be copied before it is tenured, called the tenure threshold. This threshold is chosen to keep the survivor space half full.

If you want to have more control, you can use the JVM param XX:MaxTenuringThreshold

Fran Montero
  • 1,679
  • 12
  • 24