Vinoth Kumar C M
Vinoth Kumar C M

Reputation: 10618

Question on Life cycle of an object

In Java the following is the life cycle of an object

 Created
 In use (strongly reachable)
 Invisible
 Unreachable
 Collected
 Finalized
 Deallocated

My question is, where does garbage collection fit in here? When do we say an object is "garbage collected"? Is it after the last stage in the cycle? Please clarify.

Upvotes: 0

Views: 228

Answers (2)

Tapas Bose
Tapas Bose

Reputation: 29816

I want to little extend the answer given by JB Nizet:

An object enters an unreachable state when no more strong references to it exist. When an object is unreachable, it is a candidate for collection.

An object is in the collected state when the garbage collector has recognized an object as unreachable and readies it for final processing as a precursor to deallocation. If the object has a finalize method, then it is marked for finalization.

An object is in the finalized state if it is still unreachable after its finalize method, if any, has been run. A finalized object is awaiting deallocation.

The deallocated state is the final step in garbage collection. If an object is still unreachable after all the above work has occurred, then it is a candidate for deallocation.

Look here for more information.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 692231

I would say that the garbage collection starts when the object goes from Unreachable to Collected, and stops when the object is deallocated. You normally don't have to care much, unless you use finalizers, which you shouldn't in 99.999% of the cases.

Upvotes: 3

Related Questions