preface

  • How do you determine if a Java object is alive or not important for garbage collection, preventing memory leaks, and so on
  • This article will give a comprehensive explanation of judgmentJavaThe way objects live, I hope you’ll like it

In the coming days, I will publish a series of articles on the JVM, as follows; Interested in sustainable follow Carson_Ho’s Android development notes


Schematic diagram


directory


Schematic diagram


1. Judgment method

  • Garbage collector pairJavaCriteria for determining whether objects in the heap are recycled:JavaThe object is live or die

The collection is performed only if the object is dead

  • inJavaThere are two methods to determine whether an object exists on a VM:
    1. Reference counting method
    2. Chain of reference method (accessibility analysis)

More on this below.


2. Reference counting

2.1 Mode Description

  • toJavaObject adds a reference counter
  • Whenever a place references it, the counter +1; If reference fails, -1;

2.2 Judging the survival criteria of objects

If the counter is not 0, the object is judged to be alive. Otherwise, death is judged (counter = 0).

2.3 the advantages

  • Implement a simple
  • Determine efficient

2.4 disadvantages

  • Cannot solve the problem of objects referring to each other circularly

That is, the algorithm has the flaw of judgment logic

  • A detailed description
<-- background --> // The objects objA and objB have fields named // The two objects reference each other. ObjB. Name = objA; <-- problem --> // In fact, these two objects are no longer accessible and should be collected by the garbage collector. // But because they reference each other, the counter is not zero, which causes the reference counting algorithm to fail to tell the garbage collector to reclaim these two objectsCopy the code

It is because the algorithm has judgment logic loopholes, soJavaThe VM does not use this algorithmJavaWhether they survive.


3. Chain of reference method (accessibility analysis)

  • Many mainstream business languages (e.gJava,C#) use theReference chain methodjudgeJavaWhether the object is alive.
  • There are three steps:
    1. Accessibility analysis
    2. First mark & filter
    3. Second tag & filter

3.1 Accessibility analysis

A. Mode description

Take a series of GC Roots objects as a starting point from which to search down.

  • Can be used asGC RootThe objects are:

    1.JavaObject referenced in the virtual machine stack (the local variation table of stack frames)

    2. Local method stackJNIReference object

    3. Objects referenced by constant and class static properties in the method area
  • Search down the path = chain of references

The diagram below:


Schematic diagram

B. Check whether the object meets the standard

When an object is not connected to GC Roots by any reference chain, the object is judged unreachable

No chain of references connected = GC Root to object unreachable = object unavailable


Schematic diagram

Pay special attention to

  • Reachability analysis only determines whether an object is reachable, but it is not sufficient to determine whether an object is alive/dead
  • When judging unreachable objects in an accessibility analysis, only “condemned” = not really dead

Unreachable objects are placed in the collection “to be collected”.

  • To determine that an object is truly dead, there are two more stages:
    1. First mark & filter
    2. Second tag & filter

3.2 The first mark & filter

  • After an object is judged to be unreachable in a reachability analysis, it is marked for filtering for the first time

A. Do not filter: continue to stay in the collection of “to be recycled”, waiting for recycling; B. Filter: Remove from the collection of “to be collected”

  • Criteria for filtering: whether the object is necessary to executefinalize()methods
    1. If it is necessary to perform (manually set), then filter out and proceed to the next stage (second mark & filter);
    2. If not, the object is dead, do not filter and wait for collection

When objects have no Finalize () method or Finalize () has been called by a virtual machine, it is regarded as “not necessary to execute”.


3.3 Second mark & filter

When an object is marked & filtered the first time, it is marked & ready to be filtered the second time

A. Mode description

The object will be put into an F-queue, and finalize() will be executed by the Finalizer thread of low priority automatically set up by the VM.

  1. finalize()It will only be executed once
  2. But it does not promise to waitfinalize()The operation is complete. This is to preventfinalize()Slow execution/stop makingF-QueueQueue Other objects wait permanently.

B. Screening criteria

When finalize() is implemented, if the object is still not directly or indirectly associated with GC Roots on the reference chain (that is, the object associated with GC Roots), then the object will be judged dead, not filtered (left in the collection of “Collection to be collected”) and waiting for collection


3.4 summarize

3 Procedure + The following process




Schematic diagram


4. To summarize

  • This article is a comprehensive guide to determining the survival of Java objects
  • In the coming days, I’ll be presenting a series of presentationsJVMArticle, as follows; Interested in sustainable concernsCarson_Ho android Development Notes


Schematic diagram


Thumb up, please! Because your encouragement is the biggest power that I write!

The Android event distribution mechanism is the most comprehensive and easy to understand solution for Android screen adaptation. It is the most comprehensive and easy to understand solution for Android screen adaptation. Android development: JSON introduction and the most comprehensive analysis method! BroadcastReceiver Is the most comprehensive version of Android’s BroadcastReceiver


Welcome to attentionCarson_HoJane books!

Share the dry things about Android development from time to time, the pursuit of short, flat, fast, but there is no lack of depth.