Original address: http://leihuang.net/2014/11/13/Object-Lifecycle/
What is an object
- Objects are collections of data and behavior
- An object is an instance of a class
- Object has state and behavior
Creating objects
Creating objects with the New keyword is one of the most common methods in Java.
Object life cycle
- Created
- In use
- Invisible
- Unreachable
- Collected
- Finalized
- De-allocated
Such as
Created
The following is the some actions performed when a object is created,new memory was allocated for an object.
Once The object has been created, assuming that it's assigned to some variable and then it directly moves to the on use s Tate.
In use
Objects that is held by at least one strong reference is considered to being "in use".
Invisible
An object was in the ' Invisible ' state when there was no longer any strong references that was accessible to the program, E Ven though there might still be references.
Unreachable
An object enters a "unreachable" state when the no more strong references to it exist. When an object was unreachable then it was a state for collection.
It is the important to note, just any strong reference would hold an object in memory. These must be references this chain from a garbage collection root.
Garbage collection roots is a special class of variable that includes,temporary variables on the stack
Collected
An object was in the ' collected ' state when the garbage collector have recognized an object as unreachable and readies it fo R final processing as a precursor to de-allocation. If the object has a Finalize method, then it's marked for finalization.
Finalized
An object was in the "finalized" state if it was still unreachable after it's Finalize method, if any, have been run. A finalized object is awaiting de-allocation. If you is considering using a finalizer to ensure that important resources is freed in a timely manner, you might want t O reconsider. To lengthening object lifetimes, finalize methods can increase object size.
De-allocated
The de-allocated state was the final step in garbage collection. If an object was still unreachable after all the above work has been done and then this is the state for de-allocation.
2014-11-13 17:53:46
Brave,happy,thanksgiving!
Life cycle of Java objects