Instances of Java objects are stored in the heap area of the JVM, and for GC threads, there are three states of these objects.
1. Accessible state: There is also a variable reference in the program, then this object is accessible.
2. The Resurrection State: when there are no variables in the program that refer to this object, the object is moved from the accessible state to the resurrected state. CG threads will be ready to call this object's Finalize method at a certain time (the Finalize method inherits or overrides the child object), and the code within the Finalize method may turn the object into a reachable state, otherwise the object is converted to a non-reachable state.
3. Unreachable state: The GC thread can reclaim the memory of this object only if the object is in a non-accessible state.
JVM Heap Area Object state transitions
In order for the GC to be able to properly dispose of objects, it is necessary to monitor the running state of each object, including the application, reference, reference, assignment, etc. of the object, and the GC needs to be monitored, so no matter what state the GC will be aware of in any of the states above.
As mentioned above, when a GC thread executes a finalize method of a resurrected state object at a certain time, when does it execute? Since different JVM implementations may use different algorithms to manage GC, at any time the developer cannot anticipate the timing of the GC thread's operations (including detecting object state, freeing object memory, and invoking the object's Finalize method). Although it is possible to alert GC threads to garbage collection operations as soon as possible through the System.GC () and RUNTIME.GC () functions, there is no guarantee that GC threads will be recycled immediately.