Tortured soul from the JVM: What is your trash?

EDITORIAL

Mordor began to refuse, some people foolishly tell how rubbish should be classified, Java is really great language (playfully face) would have to achieve waste classification recovered! Also I do not understand out of!

what? You say reality of garbage classification graphic description, see to understand? In Java garbage collection algorithm is written, you can not read? Come on! If see it to understand, how it will spread "pig eat wet garbage, do not eat pig is hazardous waste ......"

But the Java world recycling division is very simple, just minutes recyclable or non-recyclable ~

Therefore, the computer world is actually easier.

GC Java developers need to be familiar with it?

Although the students do not like the Java Development C and C ++ students apply their own memory, to be thinking about when not to release out, to apply on the matter, but do not care does not mean it is not important, some unseen does not mean that will not affect you.

Which objects recovered - To be or not to be

On the road very common trash is divided into: dry garbage and recyclable waste, such as cardboard, metal, plastic bottles, are of recyclable.

0-1 in the world's so-called "garbage collection", meaning a withdrawal of the memory space occupied by the object can no longer be used in any way, the release of these memory objects may need to use.

Then the JVM is how to score? Or what objects that need to be recycled?

Which objects are recyclable?

There are two main algorithms to determine: reference counting and reachability analysis.

algorithm thought advantage Shortcoming
Reference counting Adding a reference to an object in the counter whenever a local reference to the object, the counter value of +1; when referring to the failure, the counter value -1. High efficiency determination The case is difficult to solve mutual references between objects; larger overhead, and a large number of frequently cited changes, bring a lot of extra computing
Reachability analysis Through a series of objects called "GC Roots" as a starting point, the search down from these nodes, when GC Roots to an object is not reachable, this object is recyclable. More accurate and rigorous, where the cycle can analyze the data structure refer to each other To achieve more complex; need to analyze large amounts of data, time-consuming

Reference counting, the biggest problem is difficult to solve the case of mutual references between objects.

  • 1

This is another case where two object references, in addition, these two objects no longer referenced, but because their reference count is not 0, the reference counter can not be notified GC collector recovery thereof.

  • 2

This is the case of a circular reference, no external references to them, but their reference count is not 0, it can not be recovered.

Therefore, the mainstream jvm do not use a reference counting method for memory management, instead of using the following reachability analysis, it is a schematic representation of the basic idea.

Seen from FIG, object5, object6 object7 and no reference to the object to GCRoots chain, they will be recovered.

Can be used as GC Roots contains the following:

Four kinds of reference types

Types of Object Description Usage
Strong references StrongReference Similar "Object obj = new Object ()" kind of quote If an object has a strong reference, and that the garbage collector will never recover it.
Soft references SoftReference Also useful to describe the object, but not necessary When the memory space is not enough (before throwing OutOfMemoryError), will be garbage collection
Weak references WeakReference Non-essential objects Before only survive until the next garbage collection, regardless of whether sufficient memory (only live for the next episode)
Phantom reference PhantomReference Ghost phantom reference or references The sole purpose of the system is able to receive a notification when the object is recovered

Several types of inheritance:

// All

How recycling?

Collection algorithm

ref

  1. Graphical Java garbage collection
  2. Description of Java garbage collection
  3. [JVM] GC-depth understanding of
  4. Java garbage collection (GC) mechanism Detailed

Guess you like

Origin juejin.im/post/5d25aa94f265da1b8f1aea57