search
HomeJavajavaTutorialHotspot--garbage collector (JVM) explained in detail

Hotspot--garbage collector (JVM) explained in detail

Jul 17, 2017 am 11:51 AM
hotspotRecycleRubbish

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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools