garbage collection issue in young generation [clos

2019-02-21 00:34发布

问题:

When Eden space is young generation is full, minor GC will be triggered. And in the minor GC process, non-free objects in Eden and one source Survivor space will be copied to another destination Survivor space.

My question is, if the destination Survivor space is full, how could minor GC handle?

回答1:

If it is not possible to do / complete a minor collection, then a major / full collection is performed. This is typically done using a mark-sweep-compact algorithm rather than copying algorithm ... which is one reason why full collection is expensive.

But ultimately (if you keep filling the heap) a full collection will not be able to reclaim enough space to continue and an OOME will be thrown. (Or if you are using -XX:+UseGCOverheadLimit, the OOME will be thrown when the percentage time spent in GC exceeds a designated threshold.)



回答2:

Young generation has 3 segments Eden Space, Survivor1 and Survivor2. These are just logical divisions of Young Generation. So objects get copied from Eden Space to Survivor1 and then to Survivor2.

So minor collection in general means that collection happens in Young generation. And if Young generation is full then object gets copied to Old generation.

Again, collection is minor or major depends on multiple factors one of them is space availability in young generation. So if there is enough space in Young generation for object allocation then it will be minor collection. But if there is not enough free space in YG then the same collection can turn into major.

Also JVM specification doesn't talk anything about garbage collection. So its left to JVM implementors to have their own strategy.