Analysis on JVM garbage collection mechanism

Source: Internet
Author: User

First we need to knowJavamemory allocations and recoveries are all determined by theJVMThe garbage collection mechanism is automatically completed. Each type ofJVMimplementations may use different methods to implement garbage collection mechanisms. In the acquisitionSUNbefore theOracleusing AJRockit JVM, after the acquisition of the useHotSpot JVM. CurrentlyOraclehas two kindsJVMimplemented and after some time twoJVMThe implementation will be merged. HotSpot JVMis currentlyOracle SEpart of the platform's standard core components. The garbage collection mechanism is discussed on the market by defaultHotSpot JVMof the. Ok, let's get down to the chase, first lookJVMthe memory area model,

This diagram shows the memory partitioning of the JVM very visually, where is the GC we are interested in? The answer is in the picture below.


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

Again this is the heap model of the hotspot, other JVMs(such as JRockit,IBM J9 etc.) is not the concept of a permanent generation, interestingly in the Java8 ,thehotspot will be permanently canceled, changed to metaspace , the direct use of local memory, which also reduces the pressure on the heap.

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

1, New Generation (youngGeneration): 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 (EdenSpace, where any instance enters the run- time memory area through Eden spaces)

S0 Survivor spaces (S0 Survivorspace, instances with long periods of time will move from Eden space to S0 Survivor space)

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

2, Oldgeneration: 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 "fullgc")

instances in S1, if long enough to survive, will be promoted to older generations (oldGeneration).

3. Permanent generation (Permanent Generation) contains meta-information about the details of the class, method, and so on, i.e.JVMThe method area in the memory structure, which he uses to hold class constants and string constants. Therefore, this area is not used to permanently store objects that survived from the old age. This area could also happen .GC. And it's happening in this area.GCevents can also be counted asMajor GC. hotspotdue to theGCextension to the method area, so the method area is abstracted into a permanent generation, as the logical structure of the heap. To add, the famous string constant poolJDK7before, in the permanent generation,JDK7and then moved from the permanent generation to the serious heap. After all, the permanent generation is not actually in the heap, justhotspotto expandGCof the sphere of influence, only 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 wasperformed in Eden Space , the surviving objects were moved to one of the survivor spaces.

3. Thereafter, after the GC isperformed 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.


Analysis on JVM garbage collection mechanism

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.