Java memory area allocation, storage, garbage collection policy and recycling mechanisms (deep JVM virtual machines)

Source: Internet
Author: User
1. Java Garbage Management mechanism

Object dead Judgment Method:

1 Reference counting method, 2 accessibility analysis algorithm (roots by GC to whether the class is reachable)

Reference:

Strong references: Common in code, generating objects with new, so strong references never recycle referenced objects

Soft reference: Before the system memory overflow, this kind of object will be a second recovery, if the memory is not enough after the recovery, will throw Oom error, provide softreference implementation soft reference.

Weak references: objects associated with a weak reference can only survive until the next GC, when the GC is working, the weak reference object is recycled, regardless of whether the current memory is sufficient, and the WeakReference class, such as Weakhashmap

Virtual reference: Cannot get an instance from a virtual reference, the sole purpose of setting a virtual reference to an object is to receive a system notification when the object is reclaimed by GC, the Phantomreference class.

Recycle object: The Finalize () method of any object is executed only once by the system (System.GC (), after which the method of the object is executed)

Recycle method Area:

The garbage collection of the permanent generation mainly reclaims two parts: discarded constants and useless classes. Discarded constants such as the string "ABC" entered the constant pool, the current system does not have any string object is "ABC", and there is no other place to refer to the literal, when the memory is reclaimed, the "ABC" will be cleared out of the constant pool;

All instances of the class have been reclaimed, that is, no instances of the class exist within the Java heap

The ClassLoader that loaded the class is reclaimed

The corresponding Java.lang.Class object of this class is not referenced anywhere, and cannot be accessed by reflection at any point in the method of the class.

The virtual machine can recycle the class that satisfies the above conditions, just "can", not necessarily.

garbage collection algorithm (in order to clean up memory, try to leave more contiguous memory area):

In general, the collection algorithm is based on generational Java heap is divided into Cenozoic and old age, according to the age of the most appropriate algorithm; In the new generation, a large number of objects are found dead in every garbage collection, with little survival, with a replication algorithm (copying those that are alive, populating them into the memory space of the objects to be reclaimed, To set aside more contiguous memory areas); In the old age, because of the high survival rate of the object, there is no extra space to allocate it to guarantee, using tag-clean (Mark clearly dead, will lead to memory fragmentation) or tag--------------------------- The rest of the memory area is basically contiguous, no need to replicate the object's algorithm.

Generally use the replication algorithm to reclaim the Cenozoic , the memory is divided into a larger Eden space and two small survivor space, each using Eden and one of the survivor; Replicate the surviving objects in Eden and survivor to another survivor, and finally clean up the content of Eden and the first Survivor space (the default eden:survivor in Hotspot is 8:1, with a maximum of 90% for each cenozoic ( Two survivor have 20%, generally speaking, the Cenozoic recovery of the variable accounted for 98%, only 2% of the variables need to replicate the reservation, of course, when the survivor space is insufficient, need to rely on other memory, generally used in some areas of the old age distribution guarantee (Handle promotion ))


What is a shared guarantee.

Before the minor GC, the JVM first checks whether the maximum available contiguous space in the old age is greater than the total space of all the new generation objects, and if that condition is true, the minor GC is guaranteed to be safe; if it is not satisfied, whether the continuous space in the old age is greater than the average size of the objects in the old age Although minor GC has the risk also will carry on, if does not satisfy the second condition, will carry on the FULLGC. ( to put it bluntly, see if the old age can hold all the objects of the new generation.) If not, see if you can install objects that may be converted from the new generation to the old age. If this also can not, on the FULLGC, otherwise on MINORGCThe second step there is a risk, if the guarantee fails, it will be a FULLGC, so the cost is greater, because the prevention work is not done well, MINORGC can not finish the ride to a full.

 

In the old age, because there may be 100% survival, so the replication algorithm will lead to insufficient space, the general use of tag-collation algorithm.

The JVM generally uses the method of generational collection.

garbage collector: There are serial parallel collectors and many other 2. Memory allocation and recycling policies:

Object memory allocations, mainly allocated on the heap,

• Object priority in Eden allocation , when Eden does not have enough memory allocated, the JVM initiates a minor GC. If not enough, it is allotted to the old age.

eat a chestnut:

For example, 3 2MB and a 4MB of objects allocated to the heap size is 20MB, not scalable, 10MB for the Cenozoic, 10MB for the old age, Eden and survivor area space ratio of 8:1, Visible Eden 8mb,fromsurvivor 1MB, Tosurvivor 1MB, New generation total 9MB. First 3 2MB are placed in the Eden area, when the 4MB object occurs when MINORGC, and the previous 3 2MB objects are alive, if the recovery by the replication algorithm to the Fromsurvivor area, but the Fromsurvivor area only 1MB, can not put these objects, Therefore, the 3 2MB objects are put into the old age through the distribution guarantee mechanism.

So at the end of this GC, 4MB objects are placed in the Eden area, Survivor Idle, old age occupies 6MB (3 2MB objects)

Use Jconsole to observe memory allocation on hotspot:


The code is:

public class Oomtest {
	static class oomobject{public
		byte[] placeholder =  new byte[64 * 1024];
	public static void fillheap (int num) throws exception{
		list<oomobject> List = new Arraylist<oomobject> () ;
		for (int i = 0;i < num;i++) {
			thread.sleep);
			List.add (New Oomobject ());
		}
	public static void Main (string[] ag) throws exception{
		fillheap (1000);
		System.GC ();
		Thread.Sleep (10000);
	}

See Eden in allocating memory, if more than Eden limit size, will consider put to the need to retain the object to the Survivor area, if the survivor not put it is promoted to the old age, visible figure in the old age finally close to 100%, if the old age is full of will occur fully GC (Mark-collation).



big objects go straight into the old age

Large objects are objects that require a large amount of contiguous memory space. such as a very long array. Some virtual machines can assign objects larger than this value through the-xx:pretenuresizethreshold parameter directly to the old age, so as to avoid a large number of copies between the Eden area and the two survivor areas. Of course, If the object is directly larger than Eden and a survivor area (that is, greater than the Cenozoic total space), put it directly into the old age.


· Long-lived objects enter the old age (tenured Generation)

The virtual machine has an age counter for each object, and when the default is more than 15 times the GC is promoted to the old age. Set by-xx:maxtenuringthreshold=15.


· Age Determination of dynamic objects

If all objects of the same age in the survivor space are greater than half the size of the survivor space, objects older than or equal to that age will go straight into the old age without waiting for the above threshold.


Cenozoic (eden+ Fromsurvivor region, new Generation): New-born objects are generally allocated in the Cenozoic Eden region.

Old age: Large objects, long-lived objects (virtual machines Define object age counters for objects, and increase by one year after GC, more than 15 years old)



What is the difference between MINORGC and full GC.

The Cenozoic GC (Minor GC) refers to a new generation of garbage collection action, characterized by frequent and fast

Old age GC (full GC): Garbage collection actions that occur in the old age are often accompanied by at least one MINORGC feature: slow, recycled by collection policy

3. Java Memory Area Storage

Program counter: The line number of the byte code that the current thread executes to, and the thread is private

Java Virtual machine stack: Thread private, memory model executed by Java method, storage method entry, etc.

Local method Stack: Nativemethod,native method Service-related methods

Java heap: Shared by threads, holding object instances without the need for continuous memory new class ().

Method Area: The area shared by each thread, storing the loaded class information, constants, static variables, compiled code data, and so on. No continuous memory required

To run a constant pool: is a part of the method area that holds the various literal and symbolic references generated by the compiler, and the runtime can place the new constant in a constant pool, such as intern () of a string object, adding an object in a constant pool, and string a = "abc" is also directly in a constant pool

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.