Java GC Mechanism

Source: Internet
Author: User

Usually interview if you say Java, this question is usually asked, if you can partition from the heap memory, answer to the garbage collector, and then to the GC monitoring, so it is more satisfactory

The JVM will stop the execution of the application while the GC is in progress, and other threads are waiting in addition to the GC thread, so the GC optimization is often minimized by minimizing the pause time.

Speaking of Java garbage collection, it is necessary to introduce the memory structure of Java, one:

program counter: thread private. is a small piece of memory. is the line number indicator of the bytecode that is being executed by the current thread. is the only zone in the Java Virtual Machine specification that does not have an oom.

stack: thread private. The life cycle is the same as the thread. Executing each method creates a stack that stores local variables and operands (object references). The amount of memory space required for a local variable is allocated during compilation. So the size of the stack does not change. There are two exceptions: if the thread request is greater than the depth of the stack, the Stachoverflowerror is thrown. If the stack is dynamically expanding, it cannot request enough memory to throw an oom.

java heap: all threads are shared. Created when the virtual machine is started. Holds object instances and arrays. The largest memory footprint. It is divided into the new generation (young area), the old age. Cenozoic Sub-Eden area, Servior District. Servior District is also divided into from space and to space area. The memory ratio for the Eden and Servior areas is 8:1. When the extended memory is larger than the available memory, the Oom is thrown.

method Area: all threads are shared. Used to store data such as class information, constants, static variables, and so on, that have been loaded by the virtual machine. Also known as non-heap (non–heap). The method area is also called "the Permanent generation". GC is seldom performed in this region, but does not mean that it is not recycled. This area recovery target is primarily for the recovery of constant pools and the unloading of types. When the memory request is greater than the actual available memory, the Oom is thrown.

Local method Stack: thread private. Similar to the Java stack, but not for Java methods (bytecode), but for local non-Java methods. will also throw Stackoverflowerror and Oom.

Java garbage collection mainly for heap memory and permanent generation (method area), note: JDK1.8 has no permanent generation of this said, why? See another post, GC FAQs

Java's heap memory is generational, mainly divided into the new generation (young), the elderly (old), the permanent generation (perm)

The permanent generation is also called the method area . He is used to save class constants and string constants. Therefore, this area is not used to permanently store objects that survived from the old age. This area can also occur in GC, and the GC that occurs in this region is the full GC.

The Cenozoic is also divided into Eden and two survivor zones:

The order of each space is as follows:

1. Each newly created object is in the Eden area most of the time, and very large objects go directly to the old age. (such as a large array);

2. When the Eden area is full (the new object request space fails), a GC is triggered to move the surviving objects in the Eden area to a survivor area, which is called survivor0, and then the surviving objects in the Eden region are moved to SURVIVOR0;

3. When the survivor0 is full, the surviving objects will be moved to Survivor1, and then the survivor0 will be emptied. This way, the surviving objects are moved to the old generation after several iterations.

Next is the old generation of GC, old GC also known as Fullgc,old generation GC trigger There are many kinds of situations:

1 ,theSystem.GC () method call

2 , the old generation of space is not enough (it is possible that the Cenozoic is too small, causing most of the objects to the old generation)

3 , permanent generation of space shortage

4 , statistically, the average size of the Minor GC promoted to the old generation is greater than the remaining space in the old age.

This is a more complex trigger situation, thehotspot in order to avoid the new generation of objects promoted to the old generation of space caused by the old generation of the phenomenon, in the Minor GC, made a judgment, if the

The average size of the Minor GC promoted to the old generation is greater than the remaining space of the old generation, and the full GC is triggered directly .

The new generation and the old era of garbage collection characteristics are not the same, in the Cenozoic, after the GC, can recover most of the objects, but in the old age, after the GC, most of the objects are still alive.

Therefore, the new generation and the old time GC algorithms are inconsistent, the former using a mark-clean method, the latter use of the labeling method.

Java Common garbage Collection algorithm introduction:

(1). Mark-Clear algorithm:

The most basic garbage collection algorithm, the algorithm is divided into "mark" and "clear" two stages: first mark out all the objects that need to be recycled, after the mark is complete, the unified collection of all tagged objects.

There are two drawbacks to the mark-and-purge algorithm: First, efficiency issues, marking and clearing efficiency are not high. Second, after the tag is cleared, there is a lot of discontinuous memory fragmentation, and too much space fragmentation can cause the program to be unable to find enough contiguous memory when it needs to allocate memory for a large object and has to trigger another garbage collection action in advance.

(2). Copy algorithm:

The available memory is divided into two blocks of equal size, one at a time, when the memory is used, the surviving objects are copied to another memory, and the used memory space is cleaned out once. This makes each time a piece of memory to be recycled, memory allocation without regard to memory fragmentation and other complex situations, only need to move the heap top pointer, in order to allocate memory, easy to implement, efficient operation.

The disadvantage of the replication algorithm is obvious, and the memory that can be used is reduced to half the original.

(3). Tag-Collation algorithm:

The tagging-finishing algorithm has been improved on the basis of the mark-and-sweep algorithm, which marks all the objects that need to be recycled, and does not clean up the recyclable objects directly after the tag is complete, but instead allows all surviving objects to move toward one end, clearing recyclable objects during the move, a process called grooming.

The advantage of the mark-and-sweep algorithm compared to the tag-purge algorithm is that memory fragmentation does not result in a large number of discontinuous memory fragments.

In the case of high object survival rate, the replication algorithm will perform more replication operations, and the efficiency can be reduced, and the efficiency of the marker-collation algorithm will be greatly improved with the high object survival rate.

(4). Generation of collection algorithms:

Depending on the lifetime of the objects in memory, dividing the memory into chunks, Java's virtual machines generally divide memory into cenozoic and older generations, and when new objects are created, they typically allocate memory space in the Cenozoic, and when the next generation garbage collector recovers several times, the surviving objects are moved to the old generation memory. When large objects cannot find enough contiguous memory in the Cenozoic, they are created directly in the old generation.

Garbage collector Introduction

1 , serial garbage collector

The serial garbage collector works by holding all the threads of the application. It is designed for single-threaded environments and uses only a single thread for garbage collection and works by freezing all application threads, so it may not be suitable for a server environment. It is best suited to a simple command-line program.

The serial garbage collector can be used with the JVM parameter-XX:+USESERIALGC.

2 , parallel garbage collector

The parallel garbage collector is also called throughput Collector. It is the default garbage collector for the JVM. Unlike the serial garbage collector, it uses multithreading for garbage collection. Similarly, it freezes all application threads when garbage collection is performed

3 , concurrent tag Scan garbage collector (CMS)

Concurrent tag garbage collection uses multithreaded scanning of heap memory, marking instances that need to be cleaned up, and cleaning up flagged instances. The Concurrency tag garbage collector only holds all of the application threads in the following two scenarios.

    1. When the tagged reference object is in the tenured area;
    2. In the garbage collection, the data of the heap memory is changed concurrently.

Compared to the parallel garbage collector, the concurrent tag scan garbage collector uses more CPUs to ensure program throughput. If we can allocate more CPUs for better program performance , then scanning the garbage collector on a concurrent tag is a better choice than the concurrent garbage collector.

Open the concurrency tag scan garbage collector with the JVM parameter XX:+USEPARNEWGC.

4 ,G1 garbage Collector

The G1 garbage collector is suitable for large heap memory situations where he splits heap memory into different zones and concurrently recycles them. G1 can also compress the remaining heap memory space after the memory is reclaimed. The concurrent scan flag garbage collector compresses memory in the case of STW. G1 Garbage Collection will prioritize the first block of trash

Using the G1 garbage collector through the JVM parameter –XX:+USEG1GC

Java GC Mechanism

Related Article

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.