[Insights series] JAVA (1) Understanding JAVA garbage collection

Source: Internet
Author: User

As a saying, I firmly believe that I understand garbage collection can be a better JAVA programmer. If a person is interested in the garbage collection process, it indicates that he has a considerable degree of experience in the application development field. If a person is thinking about how to choose the right garbage collection algorithm, it means he has a full understanding of the features of the application. Of course, it cannot be generalized. However, few believe that understanding garbage collection is a prerequisite for a good JAVA development.

This is the first article in the series "Become a JAVA garbage collection expert. This article focuses on the overview of garbage collection. In the next article, we will discuss the garbage collection status analysis and optimization.

This article aims to give a brief introduction to garbage collection. I hope this article will be useful.

Before learning about garbage collection, you need to know the concept. This concept is: stop-the-world ). Any garbage collection algorithm will stop the world. Stopping the world means that the JVM will suspend the running of the program when running garbage collection. When the world is stopped, all processes except garbage collection are paused. When the garbage collection is completed, these paused tasks resume running.

Garbage collection generation

In JAVA, it is not used to specify a specific segment of memory and clear it in program code. Some people can clear the memory by setting the object to null or using the System. gc () method. Setting the object to null does not have much impact. However, calling the System. gc () method will significantly affect the System performance. Therefore, this method should not be called. (Fortunately, no NHN developer is found to call this method .)

In JAVA, developers do not specify to clear a certain segment of memory in program code. The garbage collector will find and clear unwanted objects. The garbage collector has the following two assumptions: (it may be a preset or the premise is better .)

  • Most objects will soon be inaccessible.
  • It is uncommon for old objects to reference new objects.

These assumptions are called weak generational assumptions. To ensure the advantages of this assumption, objects are generally physically divided into two generations in the HotSpot VM:New Generation, AndOld Age

New Generation: Most newly created objects are located in this place. Because most objects are not accessible soon, many objects created in the new generation will disappear. When the object disappears from this region, we say it happened once.Secondary garbage collection.

Old Age: In the new generation, objects that do not become inaccessible and survive will be copied to this region. This region is generally larger than the new generation. Because of its larger size, the garbage collection here is less frequent than the new generation. When the object disappears from the old age, we say it happened once.Major garbage collection(OrFull garbage collection).

Let's look at this process in the figure:

InEternal generationIt is called the method area. It stores classes or detained strings. This region does not store objects that survive in the old age. Garbage collection may occur in this area. Garbage collection in this area is also the main garbage collection.

Some may ask:

What happens if objects in the old age need to reference objects in the new generation?

To handle this situation, there is a 512-byte block in the old age,Card table). When objects of the old generation need to reference objects of the new generation, Add corresponding records to the card table. When garbage collection is required in the new generation, the table is scanned to determine whether objects can be recycled. This avoids scanning objects throughout the old age. Card tableWrite barrier)Management. Writing barriers can speed up secondary garbage collection. Although it consumes some resources, the overall garbage collection time is reduced.

 

Figure 2: card table structure

New generation components

Understanding the new generation is the basis for understanding garbage collection. Objects in the new generation are created for the first time. The new generation is divided into three regions.

  • 1 edian Space
  • 2 survivor Space

There are a total of three spaces, two of which are the survival space. The execution process of each space is:

In the above process, there is always a space for survival that must be empty. If both survivors have data, or both survivors have no data. That meansSystem Problems.

The process of accumulating data in the old age during secondary garbage collection is described as follows:

Note that there are two technologies for fast memory allocation in the HotSpot VM. ABump-the-pointer, The other isTLABs (locally allocated cache by thread).

Bump-the-pointerThe last object that the technical tracing is allocated to the edian space. This object will be allocated to the top of the edian space. If another object is created after this, it only needs to check whether the object size is suitable for the Indian space. If appropriate, the object will be placed in the Indian Space and allocated to the top. Therefore, when a new object is created, only the last added object needs to be checked. This makes the memory allocation more efficient. However, it is another thing in a multi-threaded environment. If the thread-safe method is used to save the objects used by multiple threads in the Indian space, the performance will be greatly reduced due to the use of locks.TLABsIs a solution to solve this problem in the HotSpot VM. This allows each thread to have a small portion of the Indian space separately. Each thread can only access their own TLAB, so that the Bump-the-pointer technology can neither use locks.

This is a quick overview of garbage collection in the new generation. You do not have to remember these two technologies. They don't have to go to jail either. However, remember that after an object is created in the Indian space for the first time, the objects that survive for a long time will be moved to the old age through the living space.

Garbage collection in old ages

In the old age, garbage collection is usually performed when the data is full. The execution process varies with the garbage collection type. Understanding the various types of garbage collection will help you understand the execution process.

According to JDK 7, there are 5 types of garbage collection:

In this case, sequential garbage collection should not be used on servers. This type of garbage collection applies only to single-core workstations. Using this sort of garbage collection will greatly reduce application performance.

Now we will introduce various types of garbage collection:

Sequential garbage collection (-XX: + UseSerialGC)

In the previous section, we have introduced garbage collection in the new generation. In the old age, the garbage collection algorithm was called "mark-clear-compression ".

Sequential garbage collection is suitable for machines with small memory and few CPU cores.

 

Parallel garbage collection (-XX: + UseParallelGC)

Figure 4: differences between sequential garbage collection and parallel garbage collection

Figure 4 shows the differences between sequential garbage collection and parallel garbage collection. When sequential garbage collection only uses one thread for garbage collection. Parallel garbage collection uses multiple threads to process one garbage collection. Parallel garbage collection applies to machines with large memory and multi-core CPUs. Therefore, it is also called throughput garbage collection.

Parallel garbage collection (-XX: + UseParallelOldGC)

JDK5 began to support parallel old garbage collection. The only difference from parallel garbage collection is that this is an old garbage collection algorithm. It has three steps: Mark-Summary-compression. In the summary phase, only the surviving objects in the previously processed regions are marked for garbage collection. This is a difference from parallel garbage collection. It will take more complex steps.

Synchronous mark clearing (CMS) garbage collection (-XX: + UseConcMarkSweepGC)

Figure 5: sequential garbage collection and CMS garbage collection

As shown in figure 5, CMS garbage collection is more complex than the aforementioned garbage collection types. Its initial marking stage is very simple. The surviving objects in the objects closest to classloader will be searched. In this way, the pause time will be very short. During the synchronization marking stage, the objects referenced by the surviving objects will be tracked and checked. The difference in this step is that it is executed simultaneously with other threads. During the remarking phase, newly added objects or objects that are no longer referenced in the synchronization marking phase will be checked. Finally, the garbage collection process starts in the synchronization clearing stage. Garbage collection is synchronized with other threads. Because the garbage collection type is executed in this way, the pause time is very short. CMG garbage collection is also called the recycling press with low delay.It is suitable for environments with high response time requirements..

This type of garbage collection has an advantage in the low stop world time. It also has the following weaknesses:

  • It consumes more memory and CPU time than other garbage collection types.
  • The compression phase is not provided by default.

Using this type requires careful evaluation. If a compression task needs to be executed because of too many memory fragments, it will take longer to stop the world than other garbage collection types. Check the execution time and frequency of the compression task.

Garbage collection priority (G1)

Finally, let's learn about the garbage first (G1) GC.

Finally, we will introduce the priority (G1) for garbage collection.

Figure 6: G1 garbage collection layout

If you need to understand G1 garbage collection, you must first forget the new generation and old generation. As shown in figure 6, an object is allocated with a grid and garbage collection is executed. Then, after a region is fully loaded, the object will be allocated to another region and garbage collection will be performed. The data migration from the three spaces of the new generation to the old generation does not exist in the G1 garbage collection type. This type was used to replace CMS garbage collection. For a long time, this caused many problems and complaints.

The biggest advantage of G1. It is faster than the preceding garbage collection methods. However, in JDK 6, this is not a formal feature. JDK 7 officially provides this feature. In JDK 6, the JVM crashes due to the application of G1. Wait until it is more stable.

In this article, it is just an overview of JAVA garbage collection. This article describes how to check the JAVA garbage collection status and optimize garbage collection.

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.