JVM Memory Management: Introduction to garbage collector

Source: Internet
Author: User
Tags execution thread

Introduction

In the previous chapter we have explored the various algorithms of GC, so what is a garbage collector?

In layman's terms, the algorithm is implemented using a programming language, and the resulting program is the garbage collector. Now that we've talked about the implementation of the programming language, it's time to discuss the garbage collector, and it's already involved in the implementation of the virtual machine.

Perhaps many of the apes who do Java development do not know that we usually use the JDK, the default JVM is hotspot, in other words, most of the time we use the JVM is hotspot version of the implementation, so, The LZ discusses the garbage collector is based on the hotspot version of the JVM to carry out, please you ape friends to know this.

More intuitive, we can be in peacetime development of the machine, input java-version to view the version of the JVM, I believe that most of the ape friends of this command is not unfamiliar with it, LZ's screenshot below.

Classification of garbage collectors

As we have mentioned above, the garbage collector is actually the programming language implementation of the algorithm. Since it involves the programming language, it must be thread-bound, and we've always assumed that a GC thread is doing the GC thing before we explain the algorithm.

Therefore, the garbage collector is broadly divided into the following three categories.

Serial collector (serial collector): It has only one GC thread, and as mentioned above, it needs to suspend the user program at run time (Stop the World).

Parallel collector (parallel collector): it has more than one GC thread, and it also needs to pause the user program (Stop the World).

Concurrency collector (concurrent collector): It has one or more GC threads, and it needs to suspend the user program (Stop the World) in a partial phase, in part with the user program concurrently.

concurrency (concurrent) and parallelism (parallel)

Read the above definition, I believe that a part of the ape friends have been blindfolded, a single thread, a thread, a serial, a parallel, a concurrent, this is God horse thing?

Single-threaded and multithreading do not have to say, this very good understanding, serial and parallel also better understanding, difficult to distinguish is parallel (parallel) and concurrency (concurrent).

For a lot of concurrent explanations, LZ felt that one of the most appropriate. It explains that concurrency is where two tasks A and b need to run independently of each other, and a task begins before the B task begins before the a task ends.

Concurrency itself is relatively good to understand, then it and the parallel relationship and the difference is what?

In fact, parallelism is a way of implementing concurrency. LZ feel so that you may better understand, of course, parallelism is not the only way to achieve concurrency, there is a familiar with the time slice switch. That is, a task executes for a while, the B task executes for a while, alternating execution.

Parallelism must occur under the premise of multi-core multiprocessor or distributed systems (essentially or multi-core multiprocessor), while alternately executing or switching time slices occurs on a single core processor.

Garbage collectors in the hotspot

We have discussed the classification of garbage collectors above, in HOTSPOTJVM, each kind of garbage collector has a corresponding implementation, as follows.

Serial collector implementation: serial (for the Cenozoic, using the replication algorithm), serial old (for the older generation, using the tag/collation algorithm)

Implementation of parallel Collectors: parnew (for the Cenozoic, using the replication algorithm), Parallel scavenge (for the new generation, using the replication algorithm), Parallel old (for the older generation, using the tag/collation algorithm)

Implementation of the concurrent collector: Concurrent Mark Sweep[cms] (for older generations, using the mark/Purge algorithm)

As you can see, each of these garbage collectors are designed for different memory regions, because they use algorithms that are used in the new generation, and are used for tagging/erasing or tagging/sorting algorithms for older generations.

In practical applications, we need to select a garbage collector for the new generation and the older generations of the JVM, and we can see that there are three implementations for both the Cenozoic and the old, in other words, we should have 3*3=9 choices. But that is not the case.

In fact, there are only six options for these six types of garbage collectors, because some garbage collectors cannot work together because of the way they are implemented, as shown in the following illustration.

For the above picture, Red is the serial collector, Green is the parallel collector, the only yellow is the concurrent collector. The above three are the new generation of collectors, the following three are the old generation of collectors. There is a connection between the two, which means the two can work together.

These six combinations do not say which combination is strongest, which is the weakest, or that sentence, only the most suitable, not the best. So this requires us to have a certain understanding of each combination, in order to use the time to choose a more appropriate garbage collector.

Conclusion

This is a general introduction to the six kinds of garbage collectors, and their relationship. Each kind of garbage collector's characteristic and the execution way, we discuss again in the next chapter.

Author: zuoxiaolong (Zao Jianrong)

Source: Blog Park Zao Jianrong Technology Blog--http://www.cnblogs.com/zuoxiaolong

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.