Perspective JVM Garbage Collection

Source: Internet
Author: User



JVM is the core of the Java world, understand that it helps us better debugging, tuning and development programs, recently scattered in the Java special forces to see a book, read that the author of the content is very good, we are interested, can also buy this temperature so know the new under.

In the JVM, what we often mention is the heap, the heap is really important, in fact, in addition to the heap, there are several important modules, see:



In most cases, we don't need to care about the bottom of the JVM, but if we understand it, it's very useful for our system tuning, and knowing the GC principle of the JVM here is a very important piece of knowledge. We all know that for a large web site, if the JVM frequently occurs full GC, then it will be a fatal danger, not only will cause the website to respond slowly, more serious time can cause the system crash, this is for the user experience, we do not want to see.

The memory space in the JVM, divided from the large plane, mainly has the new generation space (young) and the old age space, in which young space, is divided into 2 parts and 3 plates, respectively 1 Egen district, and 2 Survivor District, see:



OK, here's a concrete look, what's in each part?
(1) The Eden Zone is used to store objects created using new or newinstance, which are stored in the Eden area by default, unless the object is too large or exceeds the set threshold-xx:pretenuresizethresold, Such objects are assigned directly to the old area.

(2) 2 survivor (surviving) areas, generally called S0,S1, theoretically they are the same size, explaining how they work:
In the process of constantly creating objects, the Eden area will be full, and this time it will start to make young G also known as minor GC, while the first GC of young space is to find the surviving objects in the Eden area, and then put these objects into S0, or one of the S1 areas, assuming that the first time you choose S0 , it will gradually copy the Living object to the S0 area, but if the S0 area is full and the remaining live object can only be placed in the old area, the next thing to do is to empty the Eden area, at which time the S1 area is empty.

When the second Eden area is full, the live objects in the Eden area are migrated to the S1, and if the S1 is not placed, the rest of the department will be placed in the old area, but this time the object source area is increased S0, and the Eden Area +s0 area will be emptied +s0

The third and fourth times, and so on, always ensure that S0 and S1 have an empty, used to store temporary objects, for the purpose of exchanging space, repeatedly there are no obsolete objects, will be put into the old area, the default is 15 times. The specific exchange process is similar to the information in.




Question one: How to define a living object?
Starting with the root reference, the internal properties of an object may also be references, as long as the level is linked to a living object

Question two: What is root?
Local variable references, operand stack references, PC registers, local method stack references, and so on are all root.

Question three: What is the disadvantage of an object entering the old area?
Old area is generally referred to as the older age, the old age and the new generation is different, young generation, we can think that the survival of the object is very few, and the old age is the opposite, the survival of many objects, so the JVM's heap memory, is our usual focus on the main battlefield, because there are many living objects, so there is a full GC, to find out that all surviving objects are very time consuming, so we should try to avoid the occurrence of full GC.

Question four: S0 and S1 General how big, rely on what parameters to control, what change?

Generally very small, we probably know that it is about the same ratio as young, set the parameters of the main two:
-xx:survivorratio=8
-xx:initialsurvivorratio=8

The first parameter is the Eden and survivor area gravity, note is the size of a survivor, if it is set to 8, then the Eden area is a survivor area of 8 times times, in other words S0 or S1 space is 1/10 of the entire young space, the remaining 80% Used by the Eden Zone.

The second parameter is the ratio of YOUNG/S0, when set to 8 o'clock, which means that s0 or S1 accounts for 12.5% of the entire young space.


Question five: Each time an object minor GC, the living object will be transferred in the S0 and S1 areas, and after the minor GC, how many times will it enter the old area?

The default is 15 times, the parameter is set-XX:MAXTENURINGTHRESHOLD=15, the counter will record the number of times it is exchanged on the object's head


Question six: Why does full GC cause a lot of harm?

In the event of a full GC, it means that the JVM will safely suspend all the executing threads (Stop the World) to reclaim memory space, and in this time period, all other Java-related programs, except the garbage-collected threads, will still be reflected on the system, There will be a large slowdown in system response, card machine and other states.

To give an easy to understand point of example, is in a room, if there is a person, keep throwing rubbish, and then there is a cleaner kept sweeping rubbish, at this time, our system is OK, because the basic will not appear garbage heap full room scene, and because the cleaners can deal with, assuming now there are 10 people keep throwing rubbish, Then the room will soon be piled up, at this time the cleaners, because the work does not come, loudly roar, you all suspended 3 minutes, do not throw, I first clean this room, you can throw.
In this scenario, a person throws, a person sweeps, similar to the minor GC, at this time, does not affect the person who throws the garbage, and then once 10 people still, and soon no place is still, then, will trigger the full GC, then the JVM ordered, you do not Still, When I finished recycling garbage, you are still, now everyone is clear, so-called 10 people, is similar to our hundreds of Java class, in the continuous implementation of the task, the so-called cleaner, is our GC mechanism, so, everyone in peacetime coding, we must pay attention to the less to create a bit of garbage object, This will trigger the full GC's chance of getting smaller.

Perspective JVM Garbage Collection

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.