JVM Memory Area and GC policy-Learning notes _JVM

Source: Internet
Author: User
Tags xms

For developers engaged in C, C + + program development, in the field of memory management, they have the "ownership" of each object, but also bear the responsibility for the beginning of each object's life to the end of the maintenance.

For Java programmers, with the help of the virtual machine automatic memory management mechanism, it is no longer necessary to write a matching delete and free code for each object's new operation, it is not easy to have memory leaks and memory overflow problems, and it looks good to manage memory by the virtual machine. But if you don't understand how a virtual machine uses memory, it's going to be a tough job to troubleshoot if there is a memory leak and a memory overflow problem.

The way memory is used in the JVM, including the partitioning of the virtual machine memory area, the processing principles and logic of Java object allocation, and the policy and algorithm of GC recovery, which we need to be most concerned with in our daily development, is to develop a deep understanding of the excellent and stable Java software products.

Next, according to their own understanding of finishing, for their own learning to use.

1.JVM Run Time Data area

Java Virtual machines divide the memory that they manage into several different data regions during the execution of Java programs. Each zone has its own purpose, as well as the time to create and destroy, some areas exist as the virtual machine process starts, and some areas are built and destroyed by the start and end of the user's thread.

where all the threads in the JVM share are: Method area and heap, and each independently running thread has its own: virtual machine stack, program counter, local method stack. Next, focus on the method area and the heap.

(1) Method area: In fact, we often say that the permanent generation (PermGen), in the Java program debugging will encounter the "Java.lang.OutOfMemoryError:PermGen space" occurred in this area. This permanent generation is the JVM memory area shared by each thread, used to store class information that the virtual machine has loaded, constants, static variables, even the compiled code of the compiler, and so on, will include some class-related object arrays and type arrays, internal objects used by the JVM, and compiler-optimized usage information. In the previous JVM versions, the constant pool was also placed in a permanent generation. However, at the beginning of JDK 1.7 in hotspot, the string constant pool has been removed from the permanent generation. The size of the method area, which can be set with-XX:MAXPERMSIZE=XXXM in the JVM startup parameters

(2) Java heap: This is the largest chunk of memory that the JVM manages and is shared by all threads. The only use of this area is to store object instances. All Java object instances and arrays have to allocate memory on the heap. So the heap has become the main area of garbage collector management, the allocation of general objects and garbage recycling strategy is mainly targeted at this area. JVM Startup Parameters-xmx and-XMS can be used to control heap memory size.
(-xms initial heap size, memory allocated for JVM startup,-xmx maximum heap size for maximum memory allocated during JVM run)

(3) Program counter: is a small memory space, can be seen as the current thread to execute the bytecode of the line number indicator.

(4) Virtual machine stack: Describes the Java method implementation of the memory model, used to store local variables table, operand stack, dynamic link, method of export information. Local Variables table: It holds the various basic data types known to the compiler, the reference types of objects, and so on.

(5) Local method stack: similar to the role of the virtual machine stack, the virtual machine stack for the virtual machine to perform Java method services, the local method stack for the virtual machine to use the native method service.

2. Allocation strategies and principles for objects

The creation of a normal Java object, usually starting with the new keyword invoking the constructor method of a class. When the program executes, the assignment of objects in the Java heap memory begins.
(Tips: Create an object from a new statement; Create an object from a reflection mechanism; Create an object from a clone () method; Create an object by deserialization)

(1) The first step is to examine the parameters of the new directive and whether a reference to a class can be positioned in a constant pool. such as a string object, which is returned directly if a reference to that string already existed in the string constant pool. After you find a constant pool, you check that the class has been loaded, parsed, and initialized. If the class is not loaded, the corresponding class loading process is performed first. After the class is loaded, the new instruction needs to allocate as much memory space as can be determined.

(2) The second step is to allocate memory to the object. Usually, the object is distributed in the new generation of Eden area. If the Eden area does not have enough space to allocate, the JVM initiates a MINORGC (young generation GC).

A large object, that is, a new generation that does not have enough contiguous space for its use, usually a long string or array object that requires a large amount of contiguous memory space, which is allocated directly in the old age when the space required is greater than the-xx:pretenuresizethreshold parameter. Manually setting this value avoids a large amount of memory replication between the young generation's Eden and survivor areas, which are still not distributed enough.

When designing a program, we should try to avoid the allocation of large objects, especially the large object allocation with a short life cycle. Because this will cause the Eden area to have a lot of space in the case (but the space is not continuous, can not accommodate the large objects we need), early trigger GC to obtain contiguous space.

3.GC Strategy and algorithm

(1) The method of GC generational:
In order to manage and reclaim heap memory more efficiently, the JVM takes the hotspot virtual machine as an example, based on the assumption that heap memory is physically divided into two parts, namely the young generation (younger Generation) and the old generation Generation.
(Tips: The default YG and OG ratio is 1:2)
The heap memory allocation diagram for the abstract JVM is as follows:

These two assumptions are:

1. The life cycle of most objects is not very long. This means that the references to these objects will soon become unreachable, with only a few references to the newborn object by the old object (the object that was created longer). For young generations, the vast majority of new assigned objects are created in this area;

2. Older generations, the space will occupy more than the younger generation, the younger generation of minor GC when the surviving objects will eventually enter here. The number of GC (i.e. full GC) occurs in the older generation is much less;

The younger generation (young Generation) is divided into three regions: Eden, two survivor areas, the proportion of which is 8:1:1.

Eden Area Needless to say, object allocation is the first space allocated from here. The effect of two survivor is that the object that survives after a minor GC in Eden enters one of the survivor areas, while the other survivor area is left as the backup space for the current survivor area. When the space in the survivor a zone is saturated, the minor GC that occurs at this time copies the surviving objects in Survivor A, as well as the surviving objects in Eden, to survivor B and empties survivor A. In this way, two of survivor replicate each other.

Note: How to judge the old age of objects.
Since GC collects ideas in a generational sense, it has the concept of object age counting. The object born in Eden, after a minor GC, still survives, age +1. If it can be accommodated in the Survivor area, it will enter the survivor area. In other words, as long as they live once MINORGC, you can enter the Survivor area. Then every time minorgc, the object of life is +1, reaching a certain age of the object will enter the old age (the default is 15 years old). The threshold value can be set by-xx:maxtenuringthreshold.

At the same time, if the survivor area a lot of age not too big object How to do it, everyone age is not enough to enter the old age, but the quantity is too much, survivor also unbearable. So there is also a rule, that is, survivor area of all ages equal to the size of objects if more than half of the survivor space, the age is more than equal to the age of objects are directly into the old age, not subject to parameter maxtenuringthreshold parameters.

Tipsa.
Minor GC: Cenozoic GC. occur relatively frequently, recovery speed is also faster.
Major gc/full GC: Old age GC. Usually accompanied by a minor GC, which is slower and typically 10 times times more time-consuming than the minor GC.

Minor GC Trigger Condition: Typically, when a new object is generated and the Eden application space fails, the Minor GC is triggered.
The trigger condition for full GC: The old generation is filled; the persistent generation (Perm) is full; System.GC () is displayed for invocation;

(2) GC policy
The objects that the JVM reclaims are objects that are no longer being used. But how can you tell if an object is available? There are two ways of doing this:

Reference counting algorithm: Add a reference counter to an object, and whenever there is a reference to it, the counter is 1; the counter is reduced by 1 whenever the reference is invalidated. An object with a counter of 0 at any time cannot be used again.

Accessibility analysis algorithm: That is, from the GC root, the reference chain between the objects does not point to the object, we call unreachable.

Objects that are available as GC root include:
* The object referenced by the static field of the class loaded in the method area, and the object referenced by the constant;
* Objects referenced in the virtual machine stack of each thread object;
* Local object or constant referenced in the local method stack.

(3) GC algorithm
Because the implementation of garbage collection algorithm involves a lot of program details, and each platform virtual machine to operate the memory of the same method, and then briefly introduce the next several algorithms ideas.

Mark-Purge algorithm:
The algorithm is divided into two stages: marking and clearing, first marking all the objects that need to be reclaimed, and then uniformly reclaiming all the marked objects after the mark is complete.

Main deficiencies: One is the problem of efficiency, marking and clearing two of processes is not high efficiency, on the other hand, the problem of space, marking the elimination of a large number of fragmented memory fragments, too much space debris will cause later in the program to run the process of allocating large objects, Unable to find enough contiguous memory space and had to trigger another GC action in advance.

Replication algorithm:
To solve the problem of efficiency, the replication algorithm appears, which divides the available memory by capacity into two blocks of equal size, each using only one of them. When a piece of memory is used up, the surviving object is copied to another piece, and then the used memory space is cleaned up once.

Tag-Collation algorithm:
The replication collection algorithm needs more replication operation when the object survival rate is high, and the efficiency is lower. In general, the old age does not apply to the replication algorithm, someone proposed another tag-collation algorithm, the tag process is no different, the next step is not to clean the recyclable objects directly, but let all the surviving objects move to one end, and then directly clean out the memory outside the end of the domain.

Generational collection algorithm:
The general virtual machine will use the generational collection strategy, generally in the Cenozoic, every time garbage collection has found a large number of objects die, only a small number of survival, the use of replication algorithm, only need to pay a small number of living objects replication costs can be completed collection. In the old age, because the object survival rate is relatively high, there is no extra space to allocate it guarantee, you must use the tag-collation or tag-clearing algorithm.

(4) Simple tuning of the JVM

XMS/XMX: Defines the total size of the Young+old segment, XMS the memory size Young+old when the JVM is started, and xmx the maximum available young+old memory size. The two values are typically set to the same in the user's production environment to reduce the overhead that the system spends on memory requests during the run.

Newsize/maxnewsize: Defines the size of young segments, newsize the memory size of young when the JVM is started, and maxnewsize the maximum available young memory size. The two values are typically set to the same in the user's production environment to reduce the overhead that the system spends on memory requests during the run.

Permsize/maxpermsize: Defines the size of the perm segment, permsize the memory size perm when the JVM is started, and maxpermsize the maximum available perm memory size. The two values are typically set to the same in the user's production environment to reduce the overhead that the system spends on memory requests during the run.

Survivorratio: Setting the proportion of Survivor space and Eden space

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.