Analysis on JVM garbage collection mechanism

Source: Internet
Author: User
Tags garbage collection

First we need to know that Java's memory allocations and recoveries are all automatically done by the JVM garbage collection mechanism. Each JVM implementation may implement a garbage collection mechanism in different ways. Prior to the acquisition of Sun, Oracle used the JRockit JVM, which was acquired after the acquisition using the Hotspot JVM. Oracle now has two JVM implementations and two JVM implementations are merged over time. The HotSpot JVM is part of the current Oracle SE Platform standard core component. The garbage collection mechanism is discussed on the market, which is based on the hotspot JVM by default. Ok, let's take a look at the memory area model of the JVM,

This is a very intuitive illustration of the memory partitioning of the JVM, where we are concerned about the GC. The answer is in the picture below.


Comparing the two graphs, we found that the garbage collector, along with the JIT compiler, constituted the JVM's execution engine, where the "sphere of influence" of the GC was. And look at the picture

Again, this is a hotspot heap model, other JVMs (such as JRockit, IBM J9, etc.) do not have the concept of a permanent generation, interestingly, in Java8, the hotspot will also be permanently canceled, changed to Metaspace, directly using local memory, This also relieves the pressure on the heap.

Let's take a look at the heap partitions before applying the more extensive JDK8:

1, New Generation (young Generation): Most of the newly created objects will be assigned here, because most of the objects will soon become inaccessible after creation, so many objects are created in the Cenozoic, and then disappear. The process by which objects disappear from this area is what we call "minor GC".

Eden Space (Eden Space, where any instance enters the run-time memory area through Eden spaces)

S0 Survivor spaces (S0 Survivor space, instances with long periods of time will be moved from Eden Space to S0 Survivor space)

S1 survivor Space (longer instances will move from S0 survivor space to S1 survivor space)

2, Old generation: objects have not become unreachable, and survived from the Cenozoic, will be copied here. It occupies more space than the Cenozoic. Because of its relatively large space, the GC that occurs in the old age is much less than that of the Cenozoic. The process of disappearing an object from the old age, which we call "major GC" (or "full GC")

Instances in S1, if long enough to survive, will be promoted to older generations (old Generation).

3, the Permanent generation (Permanent Generation) contains the class, method and other details of the meta-information, that is, the JVM memory structure of the method area, he used to save class constants and string constants. Therefore, this area is not used to permanently store objects that survived from the old age. GC may also occur in this area. and GC events that occur in this area are also counted as major GC. Hotspot because it extends the GC into the method area, it abstracts the method area into a permanent generation, as the logical structure of the heap. To add, the famous string constant pool before JDK7, in the permanent generation inside, after JDK7 from the permanent generation to move out into the serious heap. After all, the permanent generation is not actually in the heap, just the hotspot in order to expand the sphere of influence of the GC, there is the above picture.

Let's take a look at the new generation of GC processes:

The new generation is used to preserve the objects that were created for the first time, and he can be divided into three spaces

An Eden Space (Eden)

Two survivor Space (Survivor)

There are three spaces, including two survivor space. The order of execution for each space is as follows:

1. Most of the objects that have just been created will be stored in the Eden space.

2. After the first GC was performed in Eden Space, the surviving objects were moved to one of the survivor spaces.

3. Thereafter, after the GC is performed in Eden Space, the surviving objects are stacked in the same survivor space.

4. When a survivor is saturated with space, the surviving object is moved to another survivor's space. This will then empty the survivor space that is already saturated.

5, in the above steps to repeat several times still live objects, will be moved to the old age.

If you look closely at these steps, you will find that one survivor space must remain empty. If two survivor spaces have data, or two spaces are empty, that must indicate some sort of error in your system.


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.