Java garbage collection mechanism and java garbage collection

Source: Internet
Author: User

Java garbage collection mechanism and java garbage collection

When it comes to Garbage Collection (GC), many people naturally associate it with Java. In Java, programmers do not need to worry about the problem of dynamic memory allocation and garbage collection. All this is handled by JVM.

As the name implies, garbage collection is to release the space occupied by garbage. in Java, what types of objects are considered as "garbage "? After some objects are identified as spam, what policies are used to recycle (release space )? What are the typical spam collectors in current commercial virtual machines? Next we will discuss these issues one by one. The following is the directory outline of this article:

How to determine whether an object is "junk "?
Typical garbage collection algorithms
Typical Garbage Collector

1. How to determine if an object is "junk "?

In this section, we first understand the most basic question: if an object is determined to be "junk "? Since the task of the garbage collector is to recycle the space occupied by the garbage objects for use by new objects, how does the Garbage Collector determine that an object is "garbage "? That is, the method used to determine whether an object can be recycled.

In java, references are used to associate objects. That is to say, to operate on objects, references must be used. Obviously, a simple method is to judge whether an object can be recycled by reference counting. Without losing its universality, if an object is not associated with any reference, it means that the object is unlikely to be used elsewhere, and the object becomes a recyclable object. This method becomes the reference notation.

This method is easy to implement and highly efficient, but it cannot solve the problem of circular reference. Therefore, this method is not used in Java (Python uses reference Notation ). See the following code:

public class Main {  public static void main(String[] args) {    MyObject object1 = new MyObject();    MyObject object2 = new MyObject();        object1.object = object2;    object2.object = object1;        object1 = null;    object2 = null;  }}class MyObject{  public Object object = null;}

In the last two sentences, the values of object1 and object2 are null. That is to say, the objects pointed to by object1 and object2 cannot be accessed again, but because they reference each other, as a result, their reference count is not 0, so the Garbage Collector will never recycle them.

To solve this problem, the Accessibility analysis method is adopted in Java. The basic idea of this method is to search by a series of "GC Roots" objects as the starting point. If there is no reachable path between "GC Roots" and an object, this object is not reachable, but it should be noted that the object to be judged as inaccessible may not necessarily become a recyclable object. Objects that are determined to be inaccessible must go through at least two marking processes to become recyclable objects. If the two marking processes still do not escape the possibility of becoming recyclable objects, it is basically a recyclable object.

As for how the Accessibility Analysis Method operates, I have not yet understood it clearly. If any of you know it clearly, please kindly advise.

Here is an example:

Object aobj = new Object ( ) ;Object bobj = new Object ( ) ;Object cobj = new Object ( ) ;aobj = bobj;aobj = cobj;cobj = null;aobj = null;

Which row may make an object a recyclable object? 7th lines of code may cause objects to become recyclable objects. The reason is left to the reader's own consideration.

Let's look at another example:

String str = new String("hello");SoftReference<String> sr = new SoftReference<String>(new String("java"));WeakReference<String> wr = new WeakReference<String>(new String("world"));

Which of the three statements will make the String object a recyclable object? In the case of insufficient memory, 2nd sentences and 3rd sentences determine the String object as a recycle object, and 2nd sentences will be regarded as a recycle object under any circumstances.

Finally, we will summarize the common cases of determining objects as recyclable objects:

1) explicitly assign a reference to null or point the reference that has already pointed to an object to a new object, such as the following code:

Object obj = new Object();obj = null;Object obj1 = new Object();Object obj2 = new Object();obj1 = obj2;

2) local reference refers to the object, such as the following code:

void fun() {.....  for(int i=0;i<10;i++) {    Object obj = new Object();    System.out.println(obj.getClass());  }  }

After each loop is executed, the generated Object becomes a recyclable Object.

3) only objects associated with weak references, such:

WeakReference<String> wr = new WeakReference<String>(new String("world"));

Ii. Typical garbage collection algorithms

After determining which garbage can be recycled, the garbage collector starts to recycle the garbage. However, there is a problem: how to efficiently recycle the garbage. Java Virtual Machine specifications do not clearly define how to implement the Garbage Collector. Therefore, virtual machines of various manufacturers can implement the Garbage Collector in different ways, so here we will only discuss the core ideas of several common garbage collection algorithms.

1. Mark-Sweep (Mark-clear) Algorithm

This is the most basic garbage collection algorithm. The most fundamental reason is that it is the easiest to implement and the simplest to think about. The tag-clearing algorithm is divided into two phases: the tag phase and the clear phase. The task in the mark phase is to mark all objects to be recycled, and the clear phase is to recycle the space occupied by the marked objects. The specific process is shown in:

It is easy to see that the tag-clearing algorithm is easier to implement, but a serious problem is that memory fragments are easily generated, too many fragments may cause insufficient space to be found when large objects need to be allocated in the subsequent process, and a new garbage collection action will be triggered in advance.

2. Copying Algorithm

To solve the Mark-Sweep algorithm defect, the Copying algorithm is proposed. It divides the available memory into two equal-size blocks by capacity and uses only one of them at a time. When the memory of this block is used up, copy the still living objects to the other block, and then clear the used memory space, in this way, memory fragmentation is not prone. The specific process is shown in:

Although this algorithm is easy to implement and runs efficiently, it is not easy to generate memory fragments, but it imposes a high cost on the use of memory space, because the memory that can be used is halved.

Obviously, the efficiency of the Copying algorithm is significantly related to the number of surviving objects. If there are many surviving objects, the efficiency of the Copying algorithm will be greatly reduced.

3. Mark-Compact Algorithm

To solve the Copying algorithm defect and make full use of the memory space, the Mark-Compact algorithm is proposed. The algorithm Mark phase is the same as Mark-Sweep, but after the Mark is completed, it does not directly clear the recyclable object, but moves the surviving object to one end, then, the memory outside the end boundary is cleared. The specific process is shown in:

4. Generational Collection (Generational Collection) Algorithm

The generational collection algorithm is currently used by most JVM garbage collectors. Its core idea is to divide the memory into several different regions based on the lifecycle of the object. Generally, heap zoning is divided into Tenured Generation and Young Generation. In the old age, only a few objects need to be recycled during each garbage collection, the new generation is characterized by the fact that a large number of objects need to be recycled during each garbage collection, the most suitable collection algorithm can be adopted based on the characteristics of different generations.

At present, most garbage collectors adopt the Copying Algorithm for the new generation, because most objects need to be recycled in each garbage collection in the new generation, that is, the number of replication operations is small, however, in reality, the ratio of is not used to divide the space of the new generation. Generally, the new generation is divided into a large Eden space and two small elastic vor spaces. Each time an Eden space is used and one of the elastic vor spaces is recycled, copy the surviving objects in Eden and movie vor to another movie vor space, and then clear the Eden and the movie vor space you just used.

In the old age, only a small number of objects are recycled each time. Generally, the Mark-Compact algorithm is used.

Note that there is also a permanent Generation (Permanet Generation) outside the heap, which is used to store class classes, constants, and method descriptions. The recovery of permanent generation mainly involves two parts: discard constants and useless classes.

Iii. Typical Garbage Collector

The garbage collection algorithm is the theoretical basis of memory collection, and the garbage collector is the specific implementation of memory collection. The following describes several garbage collectors provided by the HotSpot (JDK 7) virtual machine. You can combine the collectors used in various years based on your needs.

1. Serial/Serial Old

The Serial/Serial Old collector is the most basic and oldest collector. It is a single-thread collector and must suspend all user threads when collecting garbage. The Serial collector is designed for the new generation of collectors and adopts the Copying algorithm. The Serial Old collector is designed for the collectors of the Old generation and uses the Mark-Compact algorithm. Its advantage is its simplicity and efficiency, but its disadvantage is that it will bring a pause to users.

2. ParNew

The ParNew collector is a multi-threaded version of the Serial collector. It uses multiple threads for garbage collection.

3. Parallel Scavenge

The Parallel Scavenge collector is a new generation of multi-thread collectors (Parallel collectors). It does not need to suspend other user threads during collection. It uses the Copying algorithm, the collector differs from the first two collectors to achieve a controllable throughput.

4. Parallel Old

Parallel Old is the Old version of the Parallel Scavenge collector (Parallel collector), using multithreading and the Mark-Compact algorithm.

5. CMS

The CMS (Current Mark Sweep) collector is a collector designed to obtain the minimum recovery pause time. It is a concurrent collector that uses the Mark-Sweep algorithm.

6. G1

The G1 collector is the most cutting-edge achievement in the development of collector technology. It is a collector for server applications and can fully utilize multi-CPU and multi-core environments. Therefore, it is a parallel and concurrent collector, and it can establish a predictable pause time model.

Next we will add something about memory allocation:

In general, the object memory is allocated on the stack. objects are mainly allocated in the new generation of Eden Space and From Space. In a few cases, they are directly allocated in the old age. If the Space of the new generation of Eden Space and From Space is insufficient, a GC is initiated. If GC is performed, eden Space and From Space can hold this object in Eden Space and From Space.

During GC, the surviving objects in Eden Space and From Space are moved To Space, and Eden Space and From Space are cleaned up. If the To Space cannot store an object during the cleaning process, the object will be moved To the old age. After GC, the Eden space and To Space are used. The next GC will copy the surviving object To the From Space, so that the loops are repeated. If an object evades GC once in the same vor area, the object age is increased by 1. By default, if the object age reaches 15 years old, it will be moved to the old age.

In general, large objects will be directly allocated to the old age. The so-called large objects refer to objects that require a large number of consecutive buckets. The most common large object is a large array, for example:

byte[] data = new byte[4*1024*1024]

In general, the storage space is directly allocated in the old age.

Of course, the allocation rules are not fixed, which depends on the current Garbage Collector combination and JVM-related parameters.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.