Visualization of Java garbage collection

Source: Internet
Author: User
Tags manual garbage collection thread

Garbage collection, like a double backgammon, takes only a few minutes to learn, but it takes a lifetime to master.

Ben Evans, a senior trainer and advisor, discussed garbage collection from a basic perspective in the presentation of visual garbage collection.

Here is a short summary of his speech.

Basis

When it comes to freeing memory that is no longer in use, garbage collection has largely replaced early technology, such as manual memory management and reference counting.

That's a good thing, because memory management is boring, and pedantic bookkeeping is good for computers, not for people. In this context, the language's runtime environment is stronger than the human.

Modern garbage collection is highly efficient, far exceeding the typical manual allocation in earlier languages. Typically, people with other language backgrounds stare at the disruption caused by garbage collection, but do not fully understand the context in which automatic memory management occurs.

Tag & Purge is the basic algorithm used by Java (and other run-time environments) for garbage collection.

In the tag & Purge algorithm, references stacks from each line to the program's heap. So, start with the stack, follow the pointer to find all the possible references, and then follow these references recursively.

When the recursion is done, all the living objects are found, and the rest are rubbish.

Note that the point that people often omit is that the runtime environment itself also has an "allocation list" that lists pointers to each object, which is maintained by the garbage collector and helps the garbage collector do garbage cleanup. Therefore, the run-time environment can always find objects created by it but not reclaimed.

Figure I

The stack shown in the illustration above is only a stack related to a single application thread; Each application thread has a similar stack, each of which has a set of pointers to the heap.

If the garbage collector tries to get a snapshot of a live object while the application is running, it will track the moving target, so it is easy to miss out on some of the object allocations that are heavily timed out, so that an accurate snapshot cannot be obtained. Therefore, "Stop the World" is necessary; that is, stopping the application thread long enough to capture a snapshot of a live object.

Here are two golden rules that the garbage collector must follow:

The garbage collector must recycle all the rubbish.

The garbage collector must never reclaim any live objects.

But these two rules are not equivalent; If the second rule is violated, the data will be corrupted.

On the other hand, if you violate the first rule, it will be another case that the system is not always able to recycle all the rubbish, but it will eventually recycle all the rubbish, which is acceptable, and in fact, this is the basic principle of the garbage collector.

HotSpot

Now, let's hotspot, which is actually a hybrid of C, C + +, and many platform-specific assembler programs.

When people think of an interpreter, they think of a large while loop that contains a long switch statement. But the hotspot interpreter is much more complicated than that (due to performance reasons). When you start reading the JDK source code, you will find that there are many assembler code in the hotspot.

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.