Home  >  Article  >  Java  >  Hotspot--garbage collector (JVM) explained in detail

Hotspot--garbage collector (JVM) explained in detail

巴扎黑
巴扎黑Original
2017-07-17 11:51:081961browse

The first two articles "Introduction to JVM - Runtime Data Area" and "JVM Common Garbage Collection Algorithms" mentioned the actual JVM specifications and commonly used garbage collection algorithms, specificallyJVMThere is actually more than one implementation, there are JRockit, J9 waiting, of course The most famous one is HotSpot JVM. The following is the overall architecture diagram of HotSpot JVM. This article focuses on the garbage collector (Garbage in HotSpot Collector).

The existing HotSpot garbage collectors and their relationships and application scope are as shown below Shown:

Among them, G1 GC is very conspicuously located between the new generation and the old generation. , it can be guessed that this G1 GC can be used in both the new generation and the old generation. It can indeed be said that G1 is an epoch-making new conceptGC.

Before introducing the above garbage collector, we must first explain the JVM virtual machine’s Client mode and Server mode, JavaThe first thing that can be done is to be a client. Simply speaking, it isGUIDesktop application, second, can be used as server side. Two modes: Client mode starts quickly and has poor performance after startup; Server mode starts slowly and has higher performance after startup.

Serial GC(-XX:+UseSerialGC, copy algorithm, new generation)

## This is a relatively old garbage collector, I understand it as

Simple and crude, simple and crude methods can often deal with simple environments. In fact, Serial GCThis is exactly the case in Client mode. It is a serial garbage collector. Serial means that even a multi-core processor will not have multiple threads to collect in parallel. While serial, other normal working threads must also stop working, which is called “Stop the world”. This is actually easy to understand. When you are cleaning the garbage, you don’t want someone to throw away garbage at the same time. Of course, Serial GC is almost in today’s HotSpot JVMServer mode. Abandoned. Also, it works using garbage collection's "copy algorithm"works inJavaThe new generation of the heap.

ParNew GC-XX:+Use ParNewGC, Copy algorithm,New generation

 ParNew GC is actually a multi-threaded version of Serial GC. As mentioned above, Serial GC even in a multi-core CPU environment uses a single thread to recycle garbage memory. This garbage collector side can recycle garbage memory in a multi-threaded environment. This multi-thread is only a multi-thread of garbage collection, rather than executing concurrently with user threads. And only it can be used with the old generation garbage collector of CMS, and CMS is exactly the epoch-making garbage collector. , so when the old generation garbage collector of JVM is CMS, the new generation garbage collector is usually ParNew GC.

Parallel GC-XX:+UseParallelGC, replication algorithm, new generation)

It is somewhat similar to ParaNew GC. Judging from the name, it is also a parallel multi-threaded collector. We have mentioned before that during the process of GC#"Stop the world", the shorter the pause time, the better. , many garbage collectors (including the first two) focus on how to improve the pause time. And Parallel GC focuses on throughput. It focuses on the overall time-consuming of garbage collection. If the overall time-consuming of garbage collection is shorter, the throughput is high, and CPU can spend more time on it. The execution of the task ,(throughput = task running time / (task running time + Garbage collection time)).

Serial Old GC-XX:+UseSerialOldGC, tag -Compression algorithm, old generation)

It is the old generation version of Serial GC. It is also single-threaded and can also be used with Parallel GC is used in conjunction with its old generation GC.

Parallel Old GC-XX+UseParallelOldGC, tags -Compression algorithm, old generation)

In order to avoid the dilemma of choosing Parallel GC in the new generation and only choosing Serial Old GC in the old generation, ## appeared The old version of #Parallel GC——Parallel Old GC. Therefore, if you use the combination of Parallel GC and Parallel Old GC for some constants that require high throughput, it will be a good choice. .

##☆Concurrent Mark Sweep(CMS) GC -XX:+UseConcMarkSweepGC, mark-Clear algorithm, old generation) CMS GC

almost occupies

JVMHalf of the old generation garbage collector, its epoch-making significance is that the garbage collection thread can almost work at the same time as the user thread. "Almost"It's because it still can't be done at all."Stop the world", but it shortens the pause time as much as possible. Its entire garbage collection process can be divided into the following

4

steps:

    Initial mark
  1. Concurrent Marking
  2. Remarking
  3. Concurrent Cleanup
  4.  These
4

steps"Initial mark"andRemarkRequires a short period of time“Stop the world”, the process of concurrent marking is actually The above is to work with the user thread at the same time, that is"throwing away garbage and cleaning at the same time", this will cause a problem , if the generation of garbage occurs after marking, then the garbage will have to wait until next time for recycling. Of course, after the marking is completed, the garbage will naturally not conflict with the user thread, and the cleaning process can be processed at the same time as the user thread. One of the more obvious and unavoidable problems with this garbage collector is that it uses "mark-clearance algorithm, that is to say, it will compress the surviving objects differently, which will bring about the problem of memory space fragmentation. If there is a need to allocate a continuous larger memory space, then Can only trigger Full GC once. In the previous article "Common Garbage Collection Algorithms for JVM", it was mentioned that the garbage collection in the new generation is called "Minor GC" , and the garbage collection in the old generation is called "Major GC", and "Full GC" triggers a garbage collection on the entire heap. It can be imagined that the cost will be quite high, and At this time, the user thread has to be suspended, and it can only be adjusted and optimized according to the specific usage scenario by adjusting the parameters of CMS GC.

##☆

Garbage-First(G1) GC (-XX:+UseG1GC)

G1 GC is different from all previous garbage collectors. As can be seen from the second picture at the beginning, it covers the new generation and the old generation, or it is just logically Keep "New Generation" and "Old GenerationIn fact, memory generation no longer exists. It is only an experimental version in JDK6. In JDK7u4 will not be officially commercialized until later. I will explain this garbage collector separately. In addition, its paper address is: http://citeseerx.ist.psu.edu/ viewdoc/download?doi=10.1.1.63.6386&rep=rep1&type=pdf.

The above is the detailed content of Hotspot--garbage collector (JVM) explained in detail. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn