Java garbage collector

Source: Internet
Author: User

If the collection algorithm is the method of memory recycling, then the garbage collector is the specific implementation of memory recycling.

Although we are comparing the various collectors, it is not to jump out of one of the best collectors. Since we know that there are no best collectors available so far, there is no universal collector, so we are only choosing the most appropriate collector for the specific application.

1.Serial collector (Serial collector)

This collector is a single-threaded collector, but its single-threaded meaning does not only mean that it uses only one CPU or one collection thread to complete garbage collection, but more importantly, when it is garbage collected, all other threads must be paused (Stop-the-world: Suspend all the threads that the user is working on, and know that the collection is over. The collector runs as shown in the following procedure:

In

The new generation uses the replication algorithm, Stop-the-world

Old age using marker-collation algorithm: Stop-the-world

While he is working on GC, it can cause stop-the-world, but it exists because of his simplicity and efficiency (the single-line turndown with other collectors), for environments that limit a single CPU, there is no ready-to-interact start, concentrate on GC, Nature can achieve the highest single-thread collection efficiency. So the serial collector is a good choice for running in client mode (it is still the default Cenozoic collector running in client mode for the virtual machine).

2.ParNew Collector

Multi-threaded version of the serial collector (GC with multiple threads)

It is the preferred Cenozoic collector running in server mode, with the exception of the serial collector, which currently works with the CMS collector. The CMS collector is a concurrent collector that is considered to have a sense of the times, so if a garbage collector can be used with it more perfectly, then this collector must also be an integral part. The collector runs as shown in the following procedure:

In

The new generation uses the replication algorithm: Stop-the-world

Old age using marker-collation algorithm: Stop-the-world

3.ParNew Scanvenge Collector

Similar to Parnew, but more focused on throughput. The goal is to achieve a collector that can control the throughput.

Pause times and throughput cannot be tuned at the same time. On the one hand we hope that the pause time is low and the throughput is high. In fact, this is contradictory, because: in the GC, the total amount of garbage collection is constant, if the pause time is reduced, then the frequency will be increased; frequency increase indicates that the GC will be frequent, the throughput will be reduced, performance will be reduced.

Throughput: The ratio of the amount of time/cpu the CPU is used for user code. That is, the time the user code is running/(running user code time + garbage collection time). For example, virtual machines run for a total of 100 minutes, where garbage collection takes 1 minutes and the throughput is 99%

4.G1 Collector

is one of the most cutting-edge results of today's collector development, until Jdk1.7,sun that it reached a sufficiently mature commercial level.

Advantages:

Its greatest advantage is that it combines spatial integration without generating large amounts of debris and reducing the frequency of GC.

The second is to allow the user to specify the pause time, (can specify a minimum time, over this time, will not be recycled)

One of the reasons it's so efficient is that it prioritizes garbage collection, and this prioritized area recovery ensures its efficiency.

If your application pursues a pause, that G1 can now be an option to try, and if your app pursues throughput, that G1 won't do you any special good.

Note: In all of the above collectors, when the GC is executed, it will be stop-the-world, but the following CMS collector will not.

5.CMS collector (old age collector)

The CMS collector (Concurrent Mark Sweep: Concurrent tag cleanup) is a collector that targets the shortest recovery pause time. Suitable for use in the Internet station or b/S system server, this kind of application is always the server's response speed, hope the system pause time shortest.

CMS collector Run Process: (focus on implementing the tagging process)

(1) Initial mark

The root can be directly associated to an object

Fast speed

(2) Concurrency token (along with user thread)

Main tagging procedure, marking all objects

(3) Re-tagging

The user thread is still running because of the concurrency token, so make corrections before the formal cleanup.

(4) Concurrent Cleanup (along with user threads)

The entire process is as follows:

, Stop-the-world is required when an incident is marked and re-marked. The whole process of medium is the longest is concurrent tagging and concurrent purging, both of which can work with the user thread.

A little

Concurrent collection, low pauses.

Disadvantages:

(1) Reduced user execution speed.

(2) Unable to handle floating garbage, because it uses the tag-purge algorithm, it is possible that some of the garbage after the tag, you need to wait until the next GC will be recycled. If the program needs are not met during the CMS run time, the serial old collector is temporarily enabled to re-age the collection.

(3) Due to the use of the mark-clear algorithm, it will produce a lot of fragmentation. Often there is a lot of space remaining in the old age, but it is not possible to find enough contiguous space to allocate the current object and have to start the full GC in advance

Question: Since the tag-purge algorithm can cause fragmentation of memory space, why does the CMS collector use the tag cleanup algorithm instead of the tag grooming algorithm?

A: The CMS collector pays more attention to the pause, it does the GC when it is working with the user thread (concurrent execution), if the use of the tag collation algorithm, then the cleanup will move the available object's memory space, then the application's thread will probably not find where the Application object.

Java garbage collector

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.