JVM Garbage Collection Mechanism (1)--What is Garbage Collection?

1. Garbage collection

The history of garbage collection is much longer than that of the Java language. MIT's LISP is the first language that really uses memory dynamic allocation and garbage collection technology (LISP name comes from the English abbreviation of LISt Processing), which is from MIT. A computer programming language based on the lambda calculus created in 1958 by John McCarthy, a pioneer in artificial intelligence research at the School.

When it comes to garbage collection (GC for short), it is necessary to talk about what garbage collection is collected, when to collect it, and how the virtual machine performs automatic garbage collection through the garbage collector.

1.1 What does garbage collection collect

According to the standard, garbage collection is a process of monitoring heap memory areas, identifying objects in use or not, and reclaiming unused objects . In the C language, garbage collection is done manually (requires us in the Program definition), and the Java language is automatically completed by the garbage collector. To put it bluntly, garbage collection is to recycle unreferenced objects. Since we want to recycle unreferenced objects, we have to talk about what is a reference object (object in use).

What is an object in use? An object in use is a reference to the object that still exists in the program.

When it comes to object references, we have to talk about what object references are.

1.1.1 Object references

Before JDK1.2, the definition of reference type in Java was very narrow, which means that if the data of reference type stores the starting address of another piece of memory, it means that this piece of memory represents a reference.

After JDK1.2, Java extended the concept of reference, divided into strong reference, soft reference, weak reference, virtual reference.

①Strong Reference: It is a reference such as Object obj = new Object() that is very common in our code, where obj points to the Object object allocated in the heap memory. As long as the strong reference exists, the garbage collector will The object will not be recycled.

②Soft Reference (Soft Reference): used to describe some useful but not necessary objects (like Cao Cao's chicken ribs, it is a pity that it is tasteless and discarded). After JDK1.2, java.lang.ref.SoftReference is provided class to implement soft references, such as

SoftReference<String> softRf = new SoftReference<String>(new String("helloworld"));

Use softRf.get() to get the referenced object. Here softRf is a soft reference. The object referenced by the soft reference will be recycled when the memory space is insufficient.

③Weak Reference: Weak references also describe non-essential objects, which are weaker than soft references. After JDK1.2, the java.lang.ref.WeakReference class is provided to implement weak references. Such as

WeakReference<String> weakRf = new WeakReference<String>(new String("helloworld"));     

Use weakRf.get() to get the referenced object. WeakRf is a weak reference. The object referenced by the weak reference will be recycled when the garbage collector collects work.

④Phantom Reference: Listening to this name, it is like a shadow, so it is also called a ghost reference, which is the weakest reference relationship. After JDK1.2, the java.lang.ref.PhantomReference class is provided Represents a virtual reference. But the usage is different from the above.

Phantom references must be used with reference queues (ReferenceQueue).

如:ReferenceQueue<String> rfQueue = new ReferenceQueue<String>();

  PhantomReference<String> phantomRf = new PhantomReference<String>(new String("helloworld"), rfQueue);

Use phantomRf.get() to get the referenced object. The virtual reference does not affect the life cycle of the object it refers to. The only effect of using a virtual reference is to tell the system "I'm going, bye bye" when the garbage collector is working ".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326176310&siteId=291194637