Turn!! Memory allocation and recycling of the Java Virtual machine heap

Source: Internet
Author: User

Java memory allocation and recycling, mainly refers to the Java heap memory allocation and recycling. Java heap is generally divided into 2 large areas, one is the new generation, one is the old age. In the Cenozoic, 3 areas were divided, one Eden area and two surviver areas. Generally called from Surviver and to Surviver. The sizes of these areas can be specified by themselves. For example: (-xms20m represents the available heap memory size;-xmx40m represents the maximum heap memory, which expands to the maximum heap memory when the heap memory size is insufficient;-xmn10m represents the Cenozoic memory size).

An minor GC occurs when an object in the Cenozoic is allocated in the Eden region, and then the Eden region has insufficient memory for the object to allocate. At this point, the surviving objects from the Surviver and Eden areas are copied to the to Surviver area, and this time the to Surviver area becomes the next form Surviver. So how do survier regions and Eden areas scale? Because many of the objects in Java are facing to die, is allocated, immediately will be garbage collection, survival of the object is very few, and the surviving objects will go to the Surviver area, so Sun Company think Surviver area too big bad, surviver area big, Eden is small, so the space for allocating memory is small. So Sun's default Surviver area and Eden area are proportional to the from surviver:to Surviver:eden to 1:1:8.

Then everyone will think, surviver area of the object every time minor GC will survive, then will accumulate more, will not lead to the Surviver region to save it? The answer is, it does not exist, so the old age came out at this time.

The object of the Surviver region, after several minor GC, is upgraded to an old age object, thus entering the old age. The JVM defaults to 15 GC, and the new era survives in the old age. Of course, it is also possible that the Surviver area is not waiting for 15 times, Surviver is full, do not worry, this time the JVM will be the average age of objects, all into the old age.

Perhaps there are questions, if the new generation of the object is very large, Eden area this boat will not be able to do? That's OK, just upgrade and become an old man. But the elderly will be more and more, the JVM certainly will not tolerate these bodies no one to turn into mummies, of course, to collect them. When the old memory is not enough to allocate, this time the system will occur a full GC, the old age of garbage also recycled.

In the Java Virtual machine, the new generation and the old age garbage collection is separate. Java Virtual machines provide us with several garbage collector options.

New Generation Collectors:

1.Serial Collector, single-threaded collector, using the copy algorithm, because of single-threaded, all in Java server-side development, will definitely not use it.

2.ParNew Collector, is a multi-threaded version of serial, using the replication algorithm, it can be said that Java server-side preferred collector.

3.Parallel scavenge Collector, multi-threading, with replication algorithm, this collector is the biggest feature in the throughput that can control garbage collection, this garbage collector is suitable for non-real-time and user interaction of the server, suitable for the background running algorithm, running the job of the server.

Old Age Collectors:

1.Serial old, Serial's older version. Single-threaded, using the tag-collation algorithm, unfortunately, is also not suitable for use in the server.

Older version of the 2.Parallel Old,parallel scavenge. Multi-threading, tagging-sorting algorithm, this collector and parallel scavenge features, the 2 kinds of collectors with, for the server running job, is very good, but also the Kan practical application to configure, in case the job task interval is very short, this time in the GC, there may be problems, So we can not blindly seek the most throughput.

3.cms collector, marker-purge algorithm, this collector feature is, garbage collection pause time is short, pay attention to the server response speed, give users a good experience.

Finally, the new generation and the old age-all collectors, G1 collectors. G1 can be said to be very strong, in addition to the high throughput requirements, the other can be replaced by G1. In short, after the server to experience good, use G1 collector, to the throughput of a large parallel suit.

Turn!! Memory allocation and recycling of the Java Virtual machine heap

Related Article

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.