简体   繁体   中英

Does calling System.gc() reset the statistical data for generational collection?

In other words:

I need to know if after calling System.gc() object instances (that are not collected) are distributed between generations in the same way as before calling System.gc() .

Thanks,

After a Full GC, which a System.gc() may or may not trigger,

  • the Eden space will be empty, so anything in there will be moved out or cleaned up.
  • the Survivor spaces will swap objects from the one which has objects to the empty one, and
  • the Tenured space will have the same retained objects it had before but some may have been cleaned up and some may be new to that generation.

In short, the only time System.gc() won't change objects between generations is when

  • it doesn't do anything because it is ignored
  • no objects have been created or discarded since the last Full GC.

if this could be another reason why calling System.gc() is evil

Mostly because it hurts performance and you gain very little in return. Note: the RMI can trigger a full collection periodically to ensure distributed objects are cleaned up but it tries to keep this to a minimum.

First of all, System.gc() is only a hint to the JVM that you want a GC, it might not trigger one (if using -XX:+DisableExplicitGC with Hotspot for example), and you should not rely on it doing anything -- the JVM usually knows better anyway.

If it does trigger a GC, then it's just like any other GC, and objects might get promoted from the young to the old generation if they satisfy the criteria (enough generations spent in the survivor spaces for example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM