3. Mark-compact algorithm for the JVM cleanup Phase

https://blog.csdn.net/fsjwin/article/details/111342195

Are objects marked unreachable bound to be reclaimed? This is a good question and the general answer to this question is no!! Methods in Object.java:

 protected void finalize(a) throws Throwable {}Copy the code

1. The necessity of finalization mechanism

Finalization mechanisms are simply a free pass, or an edict in front of the sand.

  • The Java language provides an object finalization mechanism that allows developers to provide custom processing logic for objects before they are destroyed.
  • When the garbage collector finds that there is no reference to an object, that is, before garbage collection of the object, the Finalize () method of the object is always called.
  • The Finalize () method can be overridden in subclasses to release resources when objects are recycled (you can also do random things like not let them be recycled and let them come back to life). This method usually does some resource freeing and cleaning, such as closing files, sockets, and database connections.

2. Never actively call a Finalize () method of an object

  • Never actively call a Finalize () method of an object. Instead, let the garbage collector call it. There are three reasons:
  • Objects may result when finalize()The resurrection.
  • The execution time of finalize() method is not guaranteed. It is completely determined by GC thread. In extreme cases, if GC does not occur,The Finalize () method will never be executed.

    A bad Finalize () willSeriously affecting GC performance.

3. To be or not to be

  • If an object is unreachable from all root nodes, it is no longer in use. In general, this object needs to be reclaimed. But in fact, they are not necessarily dead. They are on probation. An unreachable object may “resurrect” itself under certain conditions, and if so, it is unreasonable to reclaim it. To this end, define three possible states for an object in a virtual machine. As follows:

    • palpable: This object can be reached from the root node (GCRoot).
    • Can the resurrection: All references to the object are released, but the object may be resurrected in.
    • untouchableWhen the finalize() of: object is called and is not resurrected, it will enter the untouchable state. Untouchable objects cannot be resurrected, because Finalize () will only be called once.

The above three states are distinguished by finalize() method. Objects can only be reclaimed if they are not reachable.

4. A dedicated thread collects garbage

There is a lower priority thread that calls the Finalize () method

5. Process

  • Determine an object. Whether obJ is recyclable or not is at least experiencedTwo markersProcess:
  1. If the object obj to GC Roots does not have a chain of references, proceedFirst mark.
  2. Filter to determine whether the object isWhether it is necessary toExecute the Finalize () method.

    (1) If object obj does not rewrite Finalize () method, or Finalize () method has been called by virtual machine, then virtual machine considers it “unnecessary to execute”. Obj is judged to beuntouchable.

    (2) If object obj overwrites finalize() method, andNot carried out, obj will be inserted intoF-QueueIn the queue, a low-priority Finalizer thread automatically created by the VIRTUAL machine triggers the finalize() method to execute.

    (3) Finalize () methods are objectsLast chance to escape deathLater, the GC will mark the objects in the f-queue a second time: if obj connects with any object in the reference chain in finalize(), then obj will be removed from the collection of “about to reclaim F-Queue” on the second mark.
  3. After that, the object will appear againNo reference exists. In this case, the Finalize () method will not be called again, and the object will become directlyDo not touchIn other words,The Finalize () method of an object will only be called once.

6. Resurrection demo

Key point: Finalize () can be used to avoid death by associating objects in GC Roots with Finalize () method!



Next up: 010 – the JVM – Java compiler to execute process from https://blog.csdn.net/fsjwin/article/details/111404949