Garbage Collection in Old Generation part in Java

From Oracle docs:

The Old Generation is used to store long surviving objects.

Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation.

Eventually the old generation needs to be collected. This event is called a major garbage collection.

Often a major collection is much slower because it involves all live objects. So for Responsive applications, major garbage collections should be minimized.

As the name suggests Garbage Collection, Garbage here refers to unreferenced objects (objects no longer needed).

For this line : 'Often a major collection is much slower because it involves all live objects.'

Live objects are those whose reference is still there. So, why a major collection include live objects? (Major collection must involve only dead objects).

Upvotes: 2

Views: 153

Answers (1)

maaartinus
maaartinus

Reputation: 46492

Unlike real-live garbage collection, Java GC deals mostly with living objects. It's more like going throw a landfill and picking up everything what's yet needed.

The reason is that there's no direct way how to recognize garbage. All what's known is that finding a living reference to an object means that it's no garbage.

when is the garbage going to be touched

Actually never: A sweeping collector works like this:

  • it finds all the life objects
  • moves them elsewhere
  • update all references to them

What remains is free memory. The garbage simply stays there and nobody cares.

This is the key to the efficiency on minor collections. Most objects die young and by touching only the survivors, the overhead is small.

Dead objects get never touched, with the exception of objects with finalize or reference queues. Don't use finalize unless you really know what you're doing. You can never depend on it.

Upvotes: 3

Related Questions