Java memory model and garbage collection

Source: Internet
Author: User
Tags compact throwable

1. Java Memory model

The Java Virtual machine divides the memory it manages into several data regions as it executes the program, as shown in the distribution of these data regions:

    • Program counter: A small area of memory that points to the current byte code being executed. If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed, and if the native method is executed, the value of this calculator is null.
    • Java Virtual machine stack: Thread-Private, its lifecycle and thread-consistent, each method executes will create a stack frame for storing local variable table, operand stack, dynamic link, method exit and other information.
    • Local method Stack: similar to the virtual machine stack functionality, except that the virtual machine stack executes Java method services for the virtual machine, while the local method stack is used by the native method service.
    • Java heap: is the largest chunk of virtual machine management memory, shared by all threads, which is used to hold object instances, and almost all objects are allocated in that region. Java heap is the main area of memory recovery, from the perspective of memory recovery, because the current collectors are mostly using generational collection algorithm, so the Java heap can be subdivided into: the new generation and the old age, then subdivide a point can be divided into Eden space, from Survivor space, to Survivor space and so on. According to the Java Virtual Machine specification, the Java heap can be in a physically discontinuous space, as long as it is logically contiguous.
    • Method area: Like Java, it is shared by each thread to store data that has been loaded by the virtual machine class information, solid, static variables, the immediate compiler compiled code, and so on.
    • Run a constant pool, run a constant pool is a part of the method area, in addition to the class file has a version of the classes, fields, methods, interfaces and other descriptive information, there is one more information is a const pool, used to hold the compilation period generated by the various literal and symbolic references. The new constants can be put into a constant pool during run time, and more so is the Intern () method of the String class, when a string instance calls intern, Java looks for the same Unicode string constant in the constant pool, and if so, returns its reference; Adds a reference to the constant pool where Unicode equals the instance string and returns it.
2. How to determine the garbage object

The Java heap holds a few instances of all objects, and the garbage collector first needs to determine which objects are "alive" and which are "dead", i.e. objects that will not be used by any means before being reclaimed by the heap.

Reference counting method

The reference counting method is simple and efficient, and is a good algorithm in most cases. The principle is: To add a reference counter to the object, whenever there is a place to refer to the object, the Counter plus 1, when the reference fails, the counter minus 1, when the counter value of 0 indicates that the object is no longer used. It is important to note that the reference counting method is difficult to solve the problem of circular referencing between objects, and the mainstream Java Virtual machine does not use reference counting method to manage memory.

Accessibility analysis algorithm

The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, the path of the search is called the reference chain (Reference Chain), when an object to the GC Roots no reference chain connected (in the case of graph theory, Is that this object is not available when the GC roots to this object is unreachable). , objects 5, 6, and object 7 are associated with each other, but they are not accessible to GC roots, so they will be judged as recyclable objects.

In the Java language, the objects that can be used as GC roots include the following:

    • The object referenced in the virtual machine stack (the local variable table in the stack frame).
    • The object referenced by the class static property in the method area.
    • The object referenced by the constant in the method area.
    • The object referenced by JNI (that is, generally speaking, the native method) in the local method stack.

Now the question is, can the accessibility analysis algorithm appear the problem of circular reference between objects? The answer is yes, that is, there is no problem of circular referencing between objects. GC root is a specially defined "starting point" outside the object graph and cannot be referenced by objects within the object graph. For specific explanations see: http://www.zhihu.com/question/29218534.

Object survival or death (to dies or not)

Even those unreachable in the Accessibility analysis algorithm are not "immortal", at this time they are temporarily in the "probation" stage, to truly declare an object to die, at least two times to go through the marking process: If an object is found to have no reference chain connected to GC roots after accessibility analysis, Then it will be marked for the first time and filtered to see if it is necessary for this object to execute the Finalize () method. When the object does not overwrite the Finalize () method, or the Finalize () method has been called by the virtual machine, the virtual machine treats both cases as "no need to execute". The program can be a "thrilling" self-rescue process by overwriting finalize (), but there is only one chance.

/** * This code demonstrates two points: * 1. Objects can be saved by the GC itself. * 2. This chance of self-help is only one time, because the Finalize () method of an object is automatically called only once by the system * @author ZZM */public class FINALIZEESCAPEGC {public static F     INALIZEESCAPEGC save_hook = null;    public void IsAlive () {System.out.println ("Yes, I am still alive:)");     } @Override protected void Finalize () throws Throwable {super.finalize ();     System.out.println ("Finalize Mehtod executed!");    Finalizeescapegc.save_hook = this;      public static void Main (string[] args) throws Throwable {Save_hook = new finalizeescapegc ();     The object successfully saved itself for the first time Save_hook = null;     System.GC ();     Because the Finalize method has a low priority, pause for 0.5 seconds to wait for it to Thread.Sleep (500);     if (Save_hook! = null) {save_hook.isalive ();     } else {System.out.println ("No, I am dead:(");     }//The code below is exactly the same as above, but this time the rescue failed save_hook = null;     System.GC ();     Because the Finalize method has a low priority, pause for 0.5 seconds to wait for it to Thread.Sleep (500);     if (Save_hook! = null) {save_hook.isalive (); } else{System.out.println ("No, I am dead:(");  }    }  }

The result of the operation is:

Finalize Mehtod executed!  Yes, I am still alive:)  
and then quote.

Whether the reference count of the object is judged by the reference counting algorithm or whether the reference chain of the object can be reached by the accessibility analysis algorithm, it is related to "reference" to determine whether the object is alive or not. Prior to JDK 1.2, the definition of references in Java was traditional: if the value stored in the data of the reference type represents the starting address of another piece of memory, it is said that this memory represents a reference. After JDK 1.2, Java extends the concept of references into strong references (strong Reference), soft references (Soft Reference), weak references (Weak Reference), virtual references (Phantom Reference) 4 species, these 4 kinds of reference strength gradually weakened.

    • A strong reference is a common reference in program code, such as "Object obj = new Object ()", as long as a strong reference exists, and the garbage collector never reclaims the referenced object.
    • Soft references are used to describe objects that are useful but not necessary. For objects associated with soft references, these objects are then listed in the collection scope for a second collection before the system is about to occur with a memory overflow exception. If this collection does not have enough memory, a memory overflow exception will be thrown. After JDK 1.2, the SoftReference class was provided to implement soft references.
    • A weak reference is also used to describe a non-required object, but its strength is weaker than a soft reference, and the object associated with the weak reference only survives until the next garbage collection occurs. When the garbage collector is working, the objects associated with a weak reference are reclaimed regardless of whether the current memory is sufficient. After JDK 1.2, the WeakReference class was provided to implement weak references.
    • A virtual reference, also known as a phantom reference or phantom Reference, is the weakest reference relationship. Whether an object has a virtual reference exists, does not affect its lifetime at all, and cannot obtain an object instance through a virtual reference. The only purpose of setting a virtual reference association for an object is to be able to receive a system notification when the object is reclaimed by the collector. After JDK 1.2, the Phantomreference class is provided to implement the virtual reference.

Examples of soft reference use:

Package Jvm;import Java.lang.ref.softreference;class Node {public    String msg = "";} public class Hello {public    static void Main (string[] args) {        node Node1 = new Node ();//strong reference        node1.msg = "nod E1 ";        softreference<node> Node2 = new softreference<node> (NODE1); Soft reference        Node2.get (). msg = "Node2";                System.out.println (node1.msg);        System.out.println (Node2.get (). msg);}    }

The output is:

Node2node2
3, the typical garbage collection algorithm

1.mark-sweep (Mark-Clear) algorithm

This is the most basic garbage collection algorithm, the reason is that it is the most basic because it is the easiest to achieve, the idea is the simplest. The tag-purge algorithm is divided into two stages: the tagging phase and the purge phase. The task of the tagging phase is to mark out all objects that need to be recycled, and the purge phase is to reclaim the space occupied by the tagged objects. The exact process is as follows:

It is easy to see that the tag-purge algorithm is easier to implement, but one of the more serious problems is that it is prone to memory fragmentation, and too many fragments can cause the subsequent process to allocate space for large objects without finding enough space to trigger a new garbage collection action ahead of time.

2.Copying (copy) algorithm

In order to solve the defect of mark-sweep algorithm, the copying algorithm is proposed. It divides the available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is used up, copy the surviving object to another piece, and then clean up the used memory space once, so the memory fragmentation problem is not easy. The exact process is as follows:

This algorithm is simple, efficient, and not prone to memory fragmentation, but it has a high cost of using memory space because it can use less memory than half the original.

Obviously, the efficiency of the copying algorithm is very much related to the number of surviving objects, if there are many surviving objects, then the efficiency of the copying algorithm will be greatly reduced.

3.mark-compact (marker-collation) algorithm

In order to solve the defect of copying algorithm and make full use of memory space, the mark-compact algorithm is proposed. The algorithm marks the same stage as Mark-sweep, but after the token is completed, it does not clean the recyclable object directly, but instead moves the surviving object to one end and then cleans up memory outside the end boundary. The exact process is as follows:

4.Generational Collection (generational collection) algorithm

The generational collection algorithm is the algorithm used by most of the JVM's garbage collectors today. Its core idea is to divide the memory into several different regions based on the life cycle of the object's survival. In general, the heap zoning is divided into the old age (tenured Generation) and the New Generation (young Generation), the characteristics of the old age is that each garbage collection only a small number of objects need to be recycled, and the new generation is characterized by a large number of objects to be recycled each time the garbage collected, Then we can take the most suitable collection algorithm according to the characteristics of different generations.

At present, most of the garbage collectors take the copying algorithm for the new generation, because each garbage collection in the Cenozoic has to reclaim most of the objects, that is, the number of operations that need to replicate is less, but the actual is not in accordance with the ratio of 1:1 to divide the new generation of space, In general, the Cenozoic is divided into a larger Eden space and two smaller survivor space (typically 8:1:1), each time using Eden space and one of the survivor space, when recycling, Copy objects that are still alive in Eden and survivor to another survivor space, and then clean up Eden and the survivor space you just used.

Because of the characteristics of the old age is that each recycling only a small number of objects, the general use of the mark-compact algorithm.

Java memory model 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.