Graphical analysis of JVM memory heap layout

Source: Internet
Author: User

Java is able to achieve a root cause of cross-platform, is defined by the class file format standard, all implementations of the standard JVM can load and interpret the class file, it is also possible to know why the Java language execution speed is slower than the C + + language execution, Of course the reason is definitely more than this one, such as there is no data register in the JVM, the instruction set uses the stack to hold the intermediate data ... And so on, although Java contributors for the implementation of the improvement of speed to think of various methods, such as JIT, dynamic compiler, and so on, the following is a topic in Leetcode in different languages implementation performance comparison chart ...

Here is a basic architecture diagram of the JVM, in this basic architecture diagram, the stack has two parts, Java line stacks and the local method stack, the concept of the stack and C + + program is basically a concept, which is stored in the stack frame, a stack frame is a function of the call, in the stack frame inside the function of the parameter, function of the local variables, return address, etc., but with a C + + is an important difference is that C + + in the value of the communication and the difference between the address, when the transmission is an object (struct can also be used as an object, is actually the object ~, but the method of the default is public, do not believe you can try, Add a function in the structure, the compiler will not error, the program is still running ~ ~ ~), the object will be back to the stack, and Java only the basic type is the value of the transfer, the other types are referred to, what is a reference, learn the C + + reference as a pointer to understand it ~ ~ ~, in this basic structure diagram, It can be seen that the JVM also defines a local method stack, which is called the local method for Java, "These local methods are written by other languages"

In the above figure, there are two stacks in the JVM, but there is only one heap, and each thread has its own thread stack. The size of the line stacks can be configured by setting the JVM's-XSS parameters, the default size is 512K under 32-bit systems, and the data in the thread stack is private to the thread. But all the threads share a heap space, the heap is the object data, what is the object data, the exclusion method, excluding the basic type and the reference type of data will be placed in the heap space, the following concrete analysis of the heap space ...

The heap space is partitioned in the JVM as shown in

, describes the Java program Runtime heap space, can be described as follows 2

1.JVM in the heap space can be divided into three large regions, the new generation, the old age, the permanent generation

2. Cenozoic can be divided into three zones, Eden Zone, two surviving areas

When the JVM is running, you can change the configuration scale of the entire JVM heap by configuring the following parameters

Size of the 1.JVM run-time heap-xms The minimum value of the heap-xmx the maximum value of the heap space 2. Cenozoic Heap Size adjustment-xx:newsize Cenozoic minimum-xx:maxnewsize cenozoic maximum-xx: Newratio set the size of the new generation and the old age in the heap space-xx:survivorratio the size of Eden in the Cenozoic 3. Permanent generation size adjustment-xx:maxpermsize4. Other  -xx: Maxtenuringthreshold, set how many garbage collections are required to transfer the new generation to the old age, but are still not recycled

In the above configuration, the old generation of space occupied by the-xx:survivorratio This parameter configuration, after reading the above JVM heap space allocation diagram, it may be strange, why the new generation of space to be divided into three areas Eden and two Survivor District? What is the purpose? Why are you so divided? To understand this problem, you need to understand the JVM garbage collection mechanism (the copy algorithm is also called the copy algorithm), the steps are as follows:

Replication (Copying) algorithm

Divide the memory evenly into a, b two blocks, the algorithm process:

1. The new object is allocated to unused memory in block a. When the memory of block A is run out, copy the live object object of block a to block B.
2. Clean up all objects of block A.
3. The new object is allocated in the unused memory in block B. When the memory of Block B is run out, copy the surviving object of block B to block a.
4. Clean up all objects in block B.
5. Goto 1.

Advantages: simple and efficient. Cons: High memory cost, valid memory is half of the memory consumed.

The illustrations are as follows: (in the picture, back view is a cyclic process)

Further optimization of the replication algorithm: using EDEN/S0/S1 three partitions

The average divided into a/B block is too wasteful of memory, using EDEN/S0/S1 three zones more reasonable, the space ratio of eden:s0:s1==8:1:1, effective memory (can allocate memory of the new object) is the total memory of 9/10.

Algorithm process:

1. EDEN+S0 can allocate new objects;
2. Eden+s0 is garbage collected and the surviving objects are copied to S1. Clean the EDEN+S0. One new generation GC ends.
3. EDEN+S1 can allocate new objects;
4. Eden+s1 is garbage collected and the surviving objects are copied to S0. Clean the EDEN+S1. Two times the new generation GC ends.
5. Goto 1.

Default eden:s0:s1=8:1:1, therefore, the new generation can use the size of the memory space to occupy the Cenozoic 9/10, then some people will ask, why not directly into two zones, one area of 9/10, another area of 1/10, the reason for this is probably the following several

The interval between 1.S0 and S1 is obviously smaller, the effective Cenozoic space is eden+s0/s1, so the effective space is large and the memory utilization is increased.
2. In favor of the calculation of the object generation, when an object in the S0/S1 to reach the set Xx:maxtenuringthreshold value, it will be divided into the old age, imagine, if there is no s0/s1, directly into two areas, how to calculate how many times the object after the GC has not been released, You might say that a counter is added to the object to record the number of GC passes, or there is a mapping table record object and GC number of relationships, yes, OK, but this will scan the whole generation of objects, with s0/s1 we can only scan the S0/S1 area ~ ~ ~

GC instance analysis, next analysis ~

Graphical analysis of JVM memory heap layout

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.