The life cycle of a Java object
In Java, the life cycle of an object consists of the following stages:
1. Creation phase (Created)
2. Application phase (in use)
3. Invisible Stage (Invisible)
4. Non-reach stage (unreachable)
5. Collection phase (collected)
6. Finalization phase (finalized)
7. Object Space Redistribution phase (de-allocated)
Figure 1. Javaobject Life Cycle
1. Creation phase (Created)
During the creation phase, the system accomplishes the creation of the object by following several steps
- L Allocate storage space for objects
- L Start Constructing objects
- Initialize a static member from a superclass to a subclass
- L Superclass member variables are initialized in sequence, and recursive invocation of superclass construction methods
- L Child class member variables are initialized in order, and subclass constructor method calls
Once the object is created and assigned to certain variables, the state of the object is switched to the application phase
2. Application phase (in use)
The object is held at least by a strong reference.
3. Invisible Stage (Invisible)
When an object is in an invisible stage, the description program itself no longer holds any strong references to that object, although those references still exist.
Simply put, the execution of the program has gone beyond the scope of the object.
For example, if the local variable count has exceeded its scope at 25 rows, then this is called count in the invisible phase. Of course, the compiler will be in the process of compiling a direct error.
Figure 2. Example of an invisible stage
4. Non-reach stage (unreachable)
An object in an unreachable stage means that the object is no longer held by any strong references.
In contrast to the "invisible phase", the "invisible stage" means that the program no longer holds any strong references to the object, in which case the object may still be held by certain loaded static variables or strong references such as threads or JNI under systems such as the JVM, which are referred to as "GC root". The presence of these GC roots can cause memory leaks in objects that cannot be recycled.
5. Collection phase (collected)
When the garbage collector discovers that the object is already in an unreachable stage and the garbage collector is ready to reallocate the object's memory space, the object enters the collection stage. If the object has overridden the Finalize () method, it will execute the terminal operation of the method.
Here's a special note: Don't overload the Finazlie () Method! There are two reasons:
- L will affect the JVM's object allocation and recovery speed
When the object is allocated, the JVM needs to register the object on the garbage collector so that it can execute the overloaded method when it is reclaimed, the CPU time is consumed when the method executes, and the recycle operation is performed after the method is executed, that is, at least two GC is required for the object by the garbage collector.
- L may cause the object to "revive" again
In the Finalize () method, if another strong reference holds the object again, it causes the state of the object to be changed from the collection phase to the application stage. This has destroyed the life cycle process of the Java object, and the "resurrected" object does not take advantage of subsequent code management.
6. Finalization phase
When an object is still in the unreachable state after the Finalize () method is executed, the object enters the finalization phase. At this stage, you wait for the garbage collector to reclaim the object space.
7. Object Space Redistribution Phase
When the garbage collector reclaims or allocates the memory space occupied by the object, the object is completely gone, called the object space redistribution phase.
Transferred from: http://blog.csdn.net/sodino/article/details/38387049
Life cycle of Java objects