New Generation Eden and survivor areas in the JVM

Source: Internet
Author: User
Tags garbage collection
Talk about the younger generation of the JVM

1. Why are there young generations

We come first, why we need to divide the heap. Can not accomplish what he has done without being generational. In fact, there is no substitute, the only reason for generational is to optimize GC performance. First you think, if there is no generational, then all of our objects are in one piece, the GC when we want to find out which objects are useless, so that all areas of the heap will be scanned. And many of our objects are going to die, if generational, we put the newly created objects into a place, when the GC first to save this piece of "to die" the area of the object to be recycled, so that will free up a lot of space.

2. The GC in the younger generation

The HotSpot JVM divides the young generation into three parts: 1 Eden area and 2 survivor area (called From and to). The default scale is 8:1, so why is this scale by default, and then we'll talk about that. In general, newly created objects are assigned to the Eden area (some large object special handling) that, after the first minor GC, will be moved to the survivor area if it survives. Object in the Survivor area each time minor GC, age will increase by 1 years, when its age to a certain extent, it will be moved to the old generation.

Because the young generation of objects are basically facing to die (more than 80%), so in the young generation garbage collection algorithm uses the replication algorithm, the basic idea of the copy algorithm is to divide the memory into two pieces, each time only one piece, when this piece of memory is used up, will be still alive objects copied to another. The replication algorithm does not produce memory fragmentation.

At the beginning of the GC, the object will only exist in the Eden area and the survivor area named "from", and the Survivor area "to" is empty. All surviving objects in the Gc,eden area are then copied to the "to", and in the "from" area, the surviving objects are determined according to their age values. Objects that reach a certain age (age threshold, which can be set by-xx:maxtenuringthreshold) are moved to older generations, and objects that do not reach the threshold are copied to the "to" area. After this GC, the Eden Zone and the from zone have been emptied. At this time, "from" and "to" will exchange their role, that is, the new "to" is the previous GC "from", The new "from" is the previous GC "to". In any case, the survivor area named to is guaranteed to be empty. The Minor GC repeats this process until the "to" area is filled and the "to" area is filled, and all objects are moved into the old generation.

3. An object of this lifetime

I'm an ordinary Java object, I was born in Eden, and I saw a little brother like me in the Eden area, and we played in Eden for a long time. One day in the Eden District is really too many people, I was forced to go to the "from" area of the Survivor District, since went to the survivor District, I began to drift, sometimes in the survivor "from" district, sometimes in the survivor "to" district, there is no indefinite. Until I was 18 years old, my father said I am an adult, should go to the society to break through. So I went to the old generation over there, old generation, a lot of people, and age is quite large, I also know a lot of people here. In the old age, I lived for 20 years (each GC plus one year old) and was then recycled.

4. JVM parameters for the young generation

1)-xx:newsize and-xx:maxnewsize

For setting the size of the young generation, it is recommended to set the size of the entire heap to 1/3 or 1/4, and two values to be set as large.

2)-xx:survivorratio

This value is also important for setting the ratio of Eden to one of the survivor.

3)-xx:+printtenuringdistribution

This parameter is used to display the size of objects of each age group in the survivor area each time the GC is minor.

4).-xx:initialtenuringthreshol and-xx:maxtenuringthreshold

Used to set the minimum and maximum age of an object for promotion to the old age, with each object having persisted once minor GC, the age increases by 1.


Article Source: http://ifeve.com/jvm-yong-generation/

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.