Java also talks about GC

Source: Internet
Author: User

I have always wanted to write a blog, but not a blog. Today, I made up my mind to cultivate my blog writing habits. The first blog won't be written too deeply. This article focuses on Java's knowledge about GC. For more information.

This article focuses on hot spot virtual machines. Other virtual machines may be different. please differentiate them by yourself. Because we usually use hotspot. Hey, open source, free of charge!

As we all know, Java recycles memory automatically by GC. This has to say which memory needs to be recycled in Java, so you have to check which memory areas are available in Java.

Memory region during Java runtime:

1. Java heap, which is the most widely used memory area in Java programs. The memory of a new object is allocated here.

2. Method area. This is the code area of the Java program. Class, method, and variable information all exists in this region. Of course, the constant pool is also in this region.

3. Virtual Machine stacks. Java programs support multi-threaded programs and are also stack-based interpretation programs. So the pressure stack of the program call method refers to this virtual machine stack. The VM stack mainly allocates object references and basic types.

4. Local method stack. Java programs support native methods. The native method stack space will be allocated in this part of memory.

5. Program counters. The memory used is very small and you do not need to consider them in many cases. Of course, this is bound to a thread. The location where the program command is executed.

6. Direct Memory. It should be emphasized that this is not in the memory space of the JVM process, but the memory of the operating system. The reason for writing it here is to remind everyone not to forget this memory. This is also prone to oom. This is especially true for NiO. We will discuss it in detail below.

We can see that the memory areas 1, 2, and 6 are globally shared, that is, all threads share the memory space. 3, 4, and 5 are bound by threads. That is to say, the lifecycle of these Memory Spaces is closely related to threads. The release of Memory 3, 4 and 5 is very simple. For example, the VM stack, with the pressure stack memory allocation, the out stack memory is released. The so-called stack pressure means method call and method exit. The same is true for local method stacks. Program counters are recycled along with the collection of threads. Next we will focus on memory recovery for 1, 2, and 6.

Java heap memory collection:

There is no doubt that this memory is the most active memory area in Java. All Java new objects must be allocated memory here. Oom occurs when GC does not allow allocation of objects. Java. Lang. outofmemoryerror:... java heap space. This is usually the most likely memory overflow. If such OOM occurs, consider whether the heap memory is set too small, or whether the program is always allocating memory and holding a strong reference, so that these objects are always highly accessible? Okay. Let's go to the question and explain how the memory is recycled. To know how to recycle, you must first know the problems that need to be solved by any GC.

1. What objects are in the memory, which objects can be recycled, and which objects cannot be recycled?

2. How to recycle and remove the objects to be recycled from the memory.

3. When does it need to be recycled.

4. How to Solve the program delay during garbage collection and memory allocation requests when GC occurs. Especially multi-threaded programs, when GC occurs, there must be a lot of multithreading in the request heap memory allocation. How to handle these requests.

I will answer these questions one by one. Remember the question number. The question number will be mentioned below.

1st questions-the root search algorithm used to find objects in Java (not reference count ). How can we determine the root in Java. The root in Java mainly exists in these places: instance variables, static variables, reference on the method stack, and reference on the local method stack. That is to say, Java can only find the objects currently in use by the program, but cannot recycle them. So how can we find unnecessary objects. Hey, of course, it is to mark all the variables in the memory and mark all the objects to be used. The marked objects are used and should not be recycled. A series of questions are generated here. These questions are also used to answer question 4. Java memory can be allocated with n gb. How long will it take to mark so many objects each time. Besides, some objects in Java are in the heap until the program starts and ends. Mark it every time, and then do not clear it. This is a waste! So the GC power of Java is shown.

Question 2: How to recycle -- Java uses generational recycle. The heap memory in Java is divided into two generations: the new generation and the old generation.

The so-called new generation: can be understood literally, that is, those who are still relatively young objects. Is the allocated object. The new generation will enter the old age based on certain promotion strategies. Promotion Strategies (possibly incomplete ):

1. The new object is large enough (more than half of the new generation memory by default, which can be configured). Then, the new object enters the old age.

2. When an object is alive for a certain number of times in the new generation (15 by default, this number is still a little too many. You can configure it as needed), it will enter the old age.

3. In the case of GC in the new generation, "insufficient memory" (this is related to the memory reclaim policy of the new generation, which will be explained later, and why is it insufficient) is required to provide guarantees in the old generation. The new generation of more objects directly entered the old age.

The new generation recovery strategy: the new generation is also divided into two areas: the Eden area and the lotto area. The new generation will first allocate objects in this area. The other part is composed of two peer-to-peer regions, called vor. Why is it composed of two parts. This has to begin with the characteristics of the new generation. There is no doubt that the method stack is the place where the new generation requests allocate the most memory. Generally, with method rollback, most objects are not root accessible objects. Therefore, most objects here can be recycled. Then, when these objects are marked and recycled, the recovery rate is very high. Which tag recycling policy is used? There are three mark collection policies:

1. Mark replication-mark available objects and copy them to another memory area without memory fragments.

2. Mark clearing-mark available objects, and then clear unavailable objects directly. Memory fragments are generated here.

3. Mark the available objects, move the available objects to a segment, record the available object area, and clear the objects at the other end of the available objects. No memory fragments.

Obviously, most objects of the new generation can be recycled soon. The strategy is the most suitable because there are few objects to be copied or moved without generating memory fragments. In the new generation, GC usually uses the mark replication algorithm. Based on the assumption that most objects can be recycled, the size of the two copied regions is not equal, that is, the sizes of Eden and survivor are not equal. The default configuration is. Because the same VOR has two equal. Therefore, the new generation of Eden accounts for 4/5 by default. When GC occurs, the Eden zone and one of the primary vor are copied to the other primary vor. At this time, the other vor may not be able to hold the Eden and the other vor objects. If this happens, it requires the old age to guarantee and directly enter the old age. In this way, the new generation replicates back and forth to achieve GC recovery. During the recovery process, you need to pause the memory allocation. So here is a program pause point. Normally, the recovery of the young generation will be very fast, so the pause here will not be too long. However, the recovery frequency of the young generation is usually high. Of course, the garbage collection of the young generation can also be configured with a single thread, multiple threads, and a recycler that can control the delay.

Old recovery strategies: here we mainly talk about the CMS recovery policies. The recycling policies of earlier ages of G1 or jdk1.5 are rarely used. CMS (concurrent-mark-sweep) is a parallel recycler in the old age. However, this does not mean that all GC processes in the old age can be parallel with memory allocation. The recycling process in the old age is divided into the following processes:

1. initialize the tag

2. Concurrent mark

3. remark

4. Concurrent cleanup

Memory Allocation requests are paused during the process, and the process can be performed simultaneously with memory allocation requests. In the old age, the mark clearing algorithm is used, so memory fragments are generated. This allows you to enable memory fragmentation and perform memory fragmentation after GC (this can be configured ). This sorting process is full GC that makes the application pause.

About the 3rd questions-when to recycle them. For the new generation, GC usually occurs when objects cannot be allocated. GC occurs when the memory usage reaches a certain level in the old age. By default, the usage rate is 68%, which is also a configuration item. Full GC may occur after system. GC () is called. Of course, some parameters can be used to disable the display and call GC. If the disableexplicitgc parameter is set to true, the GC call cannot be displayed. Of course, there are other GC cases, such as whether the security guarantee is configured or not.

About 4th issues-generational collection is the key to improving Java performance. Parallel collection of CMS meets the requirement of simultaneous GC and application, reducing the pause time. This is also the reason why Java and virtual machines are becoming more mature and widely used. Of course, we are looking forward to using the G1 recycler. This parallel operation can control the pause time and mark the collector of the old age. It must be the trend of Java virtual machines. It is also a more brilliant expectation for the Java language.

Memory recovery in the method zone:

Generally, the memory allocation and recovery in the method area are not as frequent as those in the Java heap. But this is not a permanent generation in the true sense. memory will be recycled here. In general, the recycling of A Class must meet the following conditions before it can be recycled (possibly incomplete)

1. All instances of this class have been recycled.

2. The class loader has been recycled.

3. The class corresponding to the class is not used anywhere, including reflection.

These conditions are actually harsh. It may not be encountered in general applications. However, if cglib is frequently used to generate classes, dynamically load classes, hot deployment of JSP and other applications, it will still happen.

For the collection of constants, it is necessary to satisfy the constant without any strong reference.

Recovery of Direct Memory:

Where the Direct Memory is used. Generally, it is in NIO. For example, netty uses direct memory allocation by default. After 2.0, Mina is no longer the default Direct Memory. We have already stressed that direct memory is not the memory of the JVM process, but the memory of the operating system. This part of memory can also be configured in JVM startup parameters. Maxdirectmemorysize. The recovery of this memory usually takes effect only when full GC is used. Previously, the company was using netty (hbase communication) Where OOM occurred, because the recovery frequency in the old age was too low, and there was no recovery in the old age when the direct memory overflow occurred. Of course, disableexplicitgc can be disabled here. However, this is also a dangerous operation, which means that you have allowed the application to call system. GC (), so it is best not to call this method in the application. When disableexplicitgc is disabled, the corresponding parallel recovery parameters should also be enabled. Otherwise, this is a true full
GC, which means the program is paused. For specific parameters, you can view the JVM parameter configuration. For direct memory recovery, you can also display the call. For details about the cleaner clean method of directbuffer, refer to another blog: http://blog.csdn.net/xieyuooo/article/details/7547435. This section describes in detail.

Java GC is briefly introduced here. Of course, Java GC has many complicated applications. One of the core and advantages of Java is its powerful GC. You can usually observe the Java GC situation, which is very helpful for understanding the performance of the application. The overall JVM tuning should be adjusted based on GC conditions.

If you have any questions in this article, please correct them.

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.