Memory allocation and recycling strategy in Java, new generation, old generation __java

Source: Internet
Author: User
Tags garbage collection

There are a number of strategies for garbage collection in Java, at present, the garbage collection of commercial virtual machines adopts "generational collection", which divides memory into several pieces according to the different survival cycle of the object, and generally divides the Java heap into Cenozoic and old years, and adopts a more suitable collection algorithm according to the characteristics of each age.

The collection algorithm used in the Cenozoic: Replication algorithm. The idea of the algorithm is to divide the available memory into two pieces of equal size, one at a time, and when a piece of memory is used up, the object that is still alive is divided into another piece. The used memory space is then cleaned up once. The disadvantage of this algorithm is obvious, that is to waste half of the space. According to IBM Research, 98% of the new generation of objects to die, so do not need to allocate according to 1:1, but according to memory divided into a larger Eden space and two smaller survivor space (from and to two), each using Eden and one of the survivor. When recycled, copy Eden and one of the surviving objects of the survivor to another survivor, then clean up Eden and the survivor space just used. The general default ratio is: Eden:from:to=8:1:1. Note: The default i=8 can be set by-xx:survivorratio=i. When the survivor space is not enough, it is necessary to rely on other memory (old age) for allocation guarantee. Allocation of memory Guarantee: If the other survivor space does not have enough memory space for the last generation of surviving objects collected, then these objects will be directly through the allocation of security mechanisms into the old age.

The collection algorithm used in the old age: Tag-sorting algorithm. Algorithm idea: First mark the object that needs to be recycled, then let all the surviving objects move to one end, and then directly clean out the memory outside the boundary.

Memory allocation and recycling policies

1, object priority in Eden allocation: In most cases, the object in the new area of Eden allocation, when Eden does not have enough space allocation, the virtual machine will initiate a minor GC (see the following code comments), the GC after the existing objects into the survivor, if the survivor space is insufficient, is transferred to the old age by allocating the guarantee mechanism in advance.

public class TESTMINORGC {

	private static final int _1m=1024*1024;
	
	/**
	 * VM parameters:-verbose:gc-xms20m-xmx20m-xmn10m-xx:+printgcdetails-xx:survivorratio=8
	 * 
	 * The above parameters are configured when the program is run. -xms20m-xmx20m set Heap size to 20M   -xmn10m: Heap in the Cenozoic 10M.
	 * 
	 *-xx:survivorratio=8:eden account for 8M. -xx:printgcdetails: This parameter tells the virtual machine to
	 print the  Memory recycle log when the garbage * collection behavior occurs.
	 * 
	 * *
	
	/public static void Testallocation () {
		byte[] a1,a2,a3,a4;
		
		A1=new byte[2*_1m];
		A2=new byte[2*_1m];
		A3=new byte[2*_1m];
		/**   in Eden allocated memory for A4, found only 2M, memory is not enough, then a Minor GC (Minor GC refers to the new generation of
		 *    garbage collection action, high frequency. Extension: Old age GC (Major gc/full GC): Occurs in the old age of GC, slow, generally minor
		 *    1/10. The GC found that an existing 3 2M object could not be placed in the survivor (not enough space), so it was necessary to use the allocation guarantee mechanism to move them forward
		 *    to the old age.
		 * * * *
		/a4=new byte[4*_1m];   
	}
	

2, large objects directly into the old age. A large object is a Java object that requires a large amount of contiguous memory space, such as a longer string and a longer array. The virtual machine provides a-xx:pretenuresizethreshold parameter, so that the parameter greater than this value is allocated directly in the old age.

public class Bigobjectold {

	private static final int _1m = 1024 * 1024;

	/**
	 * VM parameters:-verbose:gc-xms20m-xmx20m-xmn10m-xx:+printgcdetails
	 *-xx:survivorratio=8 -xx:pretenuresizethreshold=3145728
	 * 
	 Note:-xx:pretenuresizethreshold after the parameters can not write 3M, but to write 3145728.
	 *
	/public static void Testbigobjectold () {
		byte[] A;
		/**
		 *  A takes up more memory than the set parameter, so it goes straight into the old age.
		 * * * *
		/a=new byte[4*_1m];
	}


3, long-term survival of the object into the old age. Since the virtual machine uses the idea of generational collection to manage memory, memory recycling must be able to identify objects in the new generation, which objects in the old age. To do this, the virtual machine defines an object age counter for each object. If the object is born in Eden and is still alive after the first MINORGC and can be accommodated by survivor, it is moved to survivor and the object age is set to 1. The object was not once in the survivor minor GC, age is increased by one year. When the age increases to a certain extent, it will be promoted to the old age. The age threshold for promotion in the old era can be set by parameter:-xx:maxtenuringthreshold.

4, dynamic object age determination. In order to better adapt to the memory of different programs, the virtual machine is not always required objects must reach maxtenuringthreshold to promote the old age, if the same age in the survivor space of all objects larger than half the total size of survivor space, Objects older than or equal to that age can go directly into the old age without having to meet the requirements of maxtenuringthreshold.

5, Space distribution guarantee. Before the minor GC occurs, the virtual opportunity checks to see whether the oldest available contiguous space in the old age is larger than the total space of all the Cenozoic objects, and if that condition is true, then the minor GC can ensure that it is safe. If not, the virtual opportunity to see if the Handlepromotionfailure setting value is guaranteed to fail, and if so, will continue to check whether the maximum available contiguous space in the old age is greater than the average size of the previous promotion to the old age (equivalent to an empirical value, not guaranteed to succeed), if greater than, Despite this minor GC adventure, a minor GC is also attempted, if less than, or handlepromotionfailure set to not allow adventure, then a full GC is required.

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.