jvm-memory allocation and garbage collection

Source: Internet
Author: User
Tags xms

Memory allocation :

Entire Memory: Heap memory (young generation size + old generation size) + non-heap (persistent generation).

1. Heap Parameters:

-XMS: initial memory, default is 1/64 of physical memory.

-XMX: Maximum memory, default is 1/4 of physical memory. When the default free heap memory is less than 40%, the JVM increases the heap until the maximum limit of-xmx, and when the free heap memory is greater than 70%, the JVM reduces the heap until the minimum limit of-XMS. So the server generally sets-xms,-xmx equal to avoid resizing the heap after each GC.

-XMN: Designated Young generation: Includes Eden and two survivor districts.

-xx:newratio: The ratio of the younger generation (-xmn) to the old generation (except for the persistent generation), the default value of 4 means that the ratio of the young generation to the old generation is 1:4, the young generation takes up the 1/5,xms=xmx of the entire stack and the XMN is set without the need to set-XX: Newratio.

2, non-heap is the JVM left to their own use, so the method area (virtual machine specification does not specify the specific data structure of this area, by the virtual machine manufacturer self-implementation), the JVM internal processing or optimization of the required memory (such as the JIT compiled code cache), each class structure (such as running a constant pool, field and method data) And the code for methods and construction methods are in non-heap memory, which is the area of memory shared by each thread.

Non-heap (persistent generation) memory allocation

-xx:permsize: Initial value, default is 1/64 of physical memory;

-xx:maxpermsize: The maximum value, which defaults to 1/4 of the physical memory.

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/8C/E9/wKioL1h90sjQ5Cp_AABMZWOxHes891.png "title=" 7ab57369-b27c-4239-8c55-8a48744e37ed.png "alt=" Wkiol1h90sjq5cp_aabmzwoxhes891.png "/>

Recovery algorithm:

1, Mark clear: will produce a large number of discontinuous memory fragmentation, when allocating large objects, unable to find enough contiguous memory, triggering a garbage collection.

2. Replication: Divide the available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once. This makes every time the entire half of the memory collection, memory allocation will not consider the complexity of memory fragmentation, as long as the mobile heap top pointer, in order to allocate memory, easy to implement, efficient operation. Only the cost of this algorithm is to reduce the memory to half the original.

The new generation recycles using a replication algorithm that divides the memory into a larger Eden space and two smaller survivor spaces, each using Eden and one of the survivor[1]. When recycled, the objects that are still alive in Eden and survivor are copied to another piece of survivor space at a time, and finally the survivor space that was used is cleared, so there is always a void in the Survivor area. If another piece of survivor space does not have enough space to store the surviving objects collected by the last generation, it is necessary to rely on other memory (this refers to the old age) for allocation guarantees.

parameters involved:-xx:survivorratio default is 8, then two survivor zone and one Eden area ratio is 2:8, one survivor area of the entire young generation of 1/10.

3, marking-sorting algorithm: The characteristics of the old age, the marking process is the same as the "mark-clear" algorithm, but the next step is not to clean up the recyclable objects directly, but to let all the surviving objects move to one end, and then directly clean out the memory outside the end of the boundary.

4, sub-generation: In the new generation, the selection of replication algorithm, only a small number of surviving objects can be copied cost to complete the collection. In the old age, because the object has a high survival rate and no extra space to guarantee it, it must be recycled using the "mark-clean" or "mark-sweep" algorithm.

The size of the memory required by an object is fully determined after the class is loaded, and the task of allocating space to an object is equivalent to dividing a size of memory from the Java heap.

Garbage collector:

The implementation of the recovery algorithm is temporarily skipped.

Memory allocation Method:

1, the pointer collision: If the memory in the Java heap is absolutely regular, all used memory is put on one side, the free memory on the other side, the middle of a pointer as a demarcation point indicator, the allocated memory is simply to move that pointer to the free space beyond the distance of the object to the same size, This distribution is called "Pointer Collisions" (Bump the Pointer).

2, Idle list: If the memory in the Java heap is not regular, the memory used and the free memory interleaved, there is no way to simply do a pointer collision, the virtual machine must maintain a list of which memory blocks are available, At the time of allocation, find a large enough space in the list to divide the object instance and update the records on the list, which is called the free list.

Memory allocation options:

The choice of which allocation method is determined by whether the Java heap is structured or not, and whether the Java heap is structured or not, is determined by whether the garbage collector in use has a compression collation function. Thus, when using collectors with compact processes such as serial and parnew, the allocation algorithm used by the system is a pointer collision, while the use of CMS, a collector based on the mark-sweep algorithm, usually takes a free list.

Concurrency control for Memory allocations:

Object creation is a very frequent behavior in a virtual machine, and even if you just modify the location pointed to by a pointer, it is not thread-safe in concurrency, it is possible to allocate memory for object A, the pointer has not yet been modified, and object B uses the original pointer to allocate memory.

There are two main ways to resolve this: 1.CAS plus failed retry to allocate memory address. 2. Tlab allocates a buffer area for each thread for object allocation, and the allocation of the new object's memory needs to be locked, which is why the new overhead is large, so the sun Hotspot JVM allocates a separate space for the created thread in order to increase the efficiency of the object memory allocation. This space, also known as Tlab,tlab, acts only on the new generation of Eden, so when writing a Java program, it is often more efficient to allocate smaller objects than large objects. Using-xx:+/-usetlab

After the memory allocation is complete, do the following:

The virtual machine needs to initialize the allocated memory space to a value of 0 (excluding the object header), and if Tlab is used, the work process can be done in advance to Tlab allocation. Next, the virtual machines make the necessary settings for the object, such as which instance of the class it is, how to find the metadata information of the class, the hash code of the object, the age of the object's GC, and so on. This information resides in the object header of the object. Depending on the current running state of the virtual machine, such as whether biased locking is enabled, the object header can be set differently. After the work is done, a new object has been created from the virtual machine perspective, but from the Java program perspective, object creation has just begun, the method has not been executed, and all fields are zero. After executing the new instruction, the method is followed and the object is initialized according to the programmer's wishes so that a truly usable object is fully generated.

Object header:

1, officially known as "Mark Word", including two parts of information, the first part is used to store the object's own run-time data, such as hash code (HASHCODE), GC generational age, lock status flag, thread-held lock, biased thread ID, biased time stamp, and other part of the type pointer, That is, the object points to its class metadata pointer, which the virtual machine uses to determine which class the object is an instance of. Not all virtual machine implementations must keep type pointers on object data, in other words, finding the metadata information for an object does not necessarily go through the object itself.

2, the length of this part of the data in 32-bit and 64-bit virtual machines (not open compression pointers) are 32bit and 64bit respectively.

3. If the object is a Java array, then there must be a piece of data in the object header to record the length of the array, because the virtual machine can determine the size of the Java object through the metadata information of the ordinary Java object, but the size of the array cannot be determined from the array's metadata.

4, the object needs to store a lot of runtime data, in fact, has exceeded the 32-bit, 64-bit bitmap structure can record the limit, but the object header information is independent of the object's own defined data of the additional storage costs, considering the virtual machine space efficiency, Mark Word is designed to be a non-fixed data structure that stores as much information as possible in a very small amount of space, which re-uses its own storage space based on the state of the object. For example, in a 32-bit hotspot virtual machine, if the object is in an unlocked state, 25bit in Mark Word's 32bit space is used to store the object hash code, 4bit is used to store the object's generational age, 2bit is used to store the lock flag bit, 1bit is fixed to 0, Other spaces are reused in other states (lightweight locking, Heavyweight locking, GC marking, and bias).

Object instance data:

1, the instance data part is the valid information which the object actually stores, also is the various types of field content defined in the program code.

2, whether inherited from the parent class, or defined in the subclass, need to be recorded.

3. This part of the storage order is affected by the virtual machine allocation policy parameters (Fieldsallocationstyle) and the order in which the fields are defined in the Java source code. The default allocation policy for the hotspot virtual machine is longs/doubles, INTs, Shorts/chars, Bytes/booleans, oops (ordinary Object pointers), as can be seen from the allocation policy. Fields of the same width are always assigned together. In cases where this precondition is met, the variables defined in the parent class appear before the child class. If the Compactfields parameter value is True (default is True), then the narrower variable within the subclass may also be inserted into the void of the parent class variable.

4, because the hotspot VM's automatic memory management system requires that the object start address must be an integer multiple of 8 bytes, that is, the size of the object must be an integer multiple of 8 bytes. The header portion of the object is exactly a multiple of 8 bytes (1 time times or twice times), so when the object instance data part is not aligned, it needs to be filled by aligning the padding.

Hotspot initiates the memory recycling process:

Root node gc Roots:

1. The object referenced in the local variable table in the stack frame.

2. The class static property in the method area, and the object referenced by the final constant.

3. The object referenced by JNI in the local method stack (that is, the generally said native method).

Safety points:

1, the selection of the security point is basically the program for a long time execution, the most obvious feature is the instruction sequence multiplexing, such as method call, loop jump, abnormal jump, etc., so the instructions with these functions will produce safepoint.

2, in the security point to generate the OOPMAP data structure: Hotspot on what offset in the object is what type of calculation, in the JIT compilation process, also in the specific location of the stack and register where is the reference, so that the GC scan can be directly aware of this information.

3. GC occurs when all threads (which do not include threads that perform JNI calls) run to the nearest security point, enumerate the root nodes, have already generated OOPMAP data structures in advance at the security point (which can be done quickly and accurately gcroots enumeration), accessibility analysis, It must be done in a snapshot that ensures consistency, and it is not possible for the object reference relationship to be changing during the parsing process.

Security Zone:

Refers to a piece of code, the reference relationship will not change, anywhere in this area to start GC is safe.

When the thread executes the code in safe region, it first identifies itself as having entered safe region, so that when the JVM launches the GC during that time, it does not have to identify itself as a safe region state. When the thread leaves the safe region, it checks to see if the system has completed the root node enumeration (or the entire GC process), and if it does, it continues, otherwise it must wait until it receives a signal that it can safely leave the safe region.

GC Interrupt:

1, preemptive interrupt: Do not need the execution code of the thread to cooperate actively, when the GC occurs, all the threads are interrupted first, if it is found that the thread break is not on the security point, then restore the threads, let it "run" to the security point.

2, active Interrupt: When the GC needs to interrupt the thread, do not directly to the thread operation, simply set a flag, each thread executes the active to poll this flag, found that the interrupt flag is true when the interrupt itself hangs. The polling flag is coincident with the place and the security point.


After JIT compilation is broken up into scalar types and indirectly allocated on the stack, the local thread allocation buffer is started, will be assigned by thread priority on Tlab, and in rare cases may be directly allocated in the old age.

Garbage collection:

1. Weak reference: When the garbage collector is working, the objects associated with the weak reference are always recycled unconditionally.

2. Virtual reference: Unable to obtain an object instance by a virtual reference, the sole purpose of setting a virtual reference association for an object is to receive a system notification when the object is reclaimed by the collector.

3. Finalize:

Execute the Finalize () method, the object will be placed in a queue called F-queue, and later by a virtual machine automatically established by a low-priority finalizer thread to execute it. The so-called "execution" here means that the virtual opportunity triggers this method, but does not promise to wait for it to end, because if an object executes slowly in the Finalize () method, or if a dead loop (more extreme) occurs, Will most likely cause other objects in the F-queue queue to be permanently waiting, even causing the entire memory-recycling system to crash. The Finalize () method is the last chance for an object to escape the fate of death, and later the GC will make a second small-scale mark on the object in F-queue, if the object is to successfully save itself in Finalize ()-just re-associate with any object on the reference chain. For example, assigning yourself (the This keyword) to a class variable or to a member variable of an object, it will be removed from the collection that is "about to be recycled" at the second mark, and if the object has not escaped at this time, it is basically recycled. The Finalize () method of the object is automatically called only once by the system, and its finalize () method will not be executed again if the object faces the next collection

Method Area Recycling: Garbage collection (permanent generation) mainly recycles two parts: obsolete constants and useless classes

The useless classes include:

1. All instances of this class have been reclaimed, that is, no instances of the class exist in the Java heap.

2. The classloader that loaded the class have been recycled.

3. The corresponding Java.lang.Class object of this class is not referenced anywhere and cannot access the method of the class from anywhere by reflection.

4, a large number of use of reflection, dynamic agents, cglib, such as bytecode framework, dynamic generation of JSP and OSGi, such as frequent custom ClassLoader scenarios require the virtual machine has the function of class unloading, to ensure that the permanent generation will not overflow.

New Generation Recovery:

1, dynamic object Age judgment: The virtual machine is not always required the age of the object must reach maxtenuringthreshold to promote the old age, if the same age in Survivor space the sum of all object size is greater than half of survivor space, Objects older than or equal to that age can go straight into the old age without waiting for the ages required in the maxtenuringthreshold.

2, before the occurrence of minor GC, the virtual opportunity to check whether the largest available continuous space in the old age is greater than the total space of all the new generation of objects, if this condition is established, then the minor GC can ensure that it is safe. if not, the virtual opportunity to see if the Handlepromotionfailure setting value allows the warranty to fail. If allowed, then will continue to check whether the largest available continuous space in the old age is greater than the average size of the previous promotion to the old age object, if greater than, will try to do a minor GC, although this time the minor GC is risky; or the Handlepromotionfailure setting does not allow for adventure, then it is also time to perform a full GC instead. If a handlepromotionfailure fails, then you have to re-launch the full GC after the failure. Although the circle is the largest when the guarantee fails, most of the time the handlepromotionfailure switch is turned on to avoid the full GC being too frequent

3, the rules after JDK 6 Update 24 become as long as the old age of continuous space is greater than the total size of the new generation of objects or the average size of successive promotions will be minor GC, otherwise the full GC. No longer cares about the handlepromotionfailure settings.

This article from "talking About technology" blog, declined reprint!

jvm-memory allocation and 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.