"Pick Up the Flowers" Memory (ii) Java memory Recycling

Source: Internet
Author: User

In the previous log ("to pick Up" memory (a) Java memory allocation), the JVM memory consists of program counter, virtual machine stack, local method Stack, GC heap, five parts of the method area. Where the GC heap is a multi-threaded shared area, its role is to store object instances. The various scenarios that are described in this section occur in this area, and garbage collection occurs primarily in GC heap memory . The content of this chapter is almost a must-ask knowledge point in high quality interview, especially the knowledge points of GC Root, generational algorithm, reference type and so on, which can well embody the internal strength of the programmer. This article is mainly in the relevant articles on the basis of the collection and collation, but also contains some of their own understanding and summary, which involves the code, put out the results of the operation, are running their own personally. In addition, the content of this article for the author word Knocked Up, if can be helpful to the reader and was, please specify, if the content of this article has different meaning, please also kindly enlighten.

The main contents of this chapter are as follows:

First, what is garbage collection

Garbage collection, that is, Gc:garbage Collection. In Java, when the memory originally assigned to an object is no longer pointed to by any object, the memory is discarded as garbage. This partially useless memory space needs to be reclaimed at the appropriate time for use by the new object instance. garbage Collection is the process of reclaiming useless memory space and making it available to future instances.

Second, why to carry out garbage collection

Because the memory space of the device is limited, and the program needs to be loaded into memory first, if there is too much garbage in memory, the available space is too small, the system will be stuck, and even make the program not run properly. In order to make full use of the memory space, the memory needs to be garbage collected. Garbage collection can automatically free up memory space, reduce the programmer's programming burden, a system-level thread of the JVM will automatically free the memory block, which is what we are familiar with, the JVM for the programmer to automatically complete the memory of the recovery work. Garbage collection discards the "useless information" of objects that the program no longer needs in order to allocate the space to new objects for use. In addition to cleaning up discarded objects, garbage collection also clears the memory fragments and completes the memory cleanup.

Iii. algorithms used by Java GC

First, the mind map, summarizes the Java GC process used in the relevant algorithms. There are two categories: (1) The algorithm of judging whether the object survives, (2) the algorithm used in different stages of GC

1. Algorithm for judging whether the object is alive

GC Heap memory holds almost all object instances, the garbage collector before the memory is recycled, the first need to determine which of these objects are "alive", which has "died", the method is mainly determined by the following two kinds:

(1) Reference counting method

The algorithm is not used in Java because it cannot deal with the problem of circular reference between objects, so it does not do in-depth exploration, and is interested in self-learning.

(2) Root search algorithm (GC root tracing)

This algorithm is used in Java to determine if an object is alive.

Algorithm idea: Through 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 graph theory is from the GC Roots to this object is not available, it proves that the object is not available, that is, the object is "dead", in the same vein, if there is a reference chain connected, it proves that the object can be "alive". As shown in the following:

So, what can be the object of GC roots? The Java language contains the following:

1) The referenced object in the virtual machine stack (the local variable table in the stack frame).

2) The object referenced by the class static property in the method area.

3) The object that the constant in the method area refers to.

4) The referenced object of JNI (that is, generally speaking, the native method) in the local method stack.

Expand reading: www.zhihu.com/question/50381439

2. Algorithms used by Java GC

In different areas of the GC heap, different garbage collectors are chosen to complete the GC at different stages of the GC, and of course, the different garbage collector uses different algorithms.

(1) Tracing algorithm

Tracing algorithm

The call mark-Clear (mark-and-sweep) algorithm, as its name implies, is to mark the surviving object and clear the Dead object . As shown above, the algorithm is based on the root search method, scans from the root collection, flags the surviving objects, and then scans the unmarked objects in the entire space for recycling after the tags are complete. The tag-purge algorithm does not need to move objects, and it only handles objects that are not alive, and is extremely efficient in the case of many surviving objects, but because the algorithm directly reclaims objects that do not exist, it can cause memory fragmentation. It is as follows:

(2) Compacting algorithm

Compacting algorithm

The algorithm is also known as the mark-and-organize (mark-and-compact) algorithm, as the name implies, is to mark the surviving objects, organize the reclaimed space . As shown above, the algorithm and the tag-purge algorithm first tag the surviving object, and then clear the unmarked object, the object that is not alive. But unlike the tag-purge algorithm, the algorithm has a finishing process, after reclaiming the space of the objects that are not surviving, will move all the surviving objects to the left free space, and update the corresponding pointers, thus solving the problem of memory fragmentation, of course, the algorithm more than a finishing process, the object moved, So the cost is higher. In the implementation of the collector based on the compacting algorithm, the handle and the handle table are generally added.

(3) Copying algorithm

Copying algorithm

The algorithm is proposed to overcome the overhead of the handle and to solve the garbage collection of heap fragments. As shown above, it begins by dividing the heap into object polygons (from space) and free polygons (to space), where the program allocates space for instance objects on the object surface, and when the object is full, the copying algorithm-based garbage collector scans the surviving objects from the base and copies each surviving object to the free surface. There is no fragmentation between the memory occupied by the surviving object. The idle face becomes the object face, the original object face becomes the idle surface, and the program allocates memory in the new object face. A typical garbage collection based on copying algorithm is the stop-and-copy algorithm, which divides the heap into object and idle area polygons, and the program suspends execution during the switching between the object surface and the idle area. The advantage of this algorithm is: regardless of the non-surviving objects, copy number only depends on the number of surviving objects, and at the same time copy, organized the heap space, eliminate the memory fragmentation, the space use of the idle area is always continuous, memory use efficiency is improved . The disadvantage is: the object surface and the free surface are divided, the memory usage isthe same. The collector must replicate all the surviving objects, which increases the program wait time .

(4) Generation algorithm

Generation algorithm

The life cycle of different objects is not the same, and the generational garbage collection strategy is formally based on this. Therefore, different life cycle objects can take different recycling algorithms to improve the efficiency of recycling. The algorithm consists of three regions: young Generation, Old Generation, and persistent (Permanent Generation)

1) younger generation (young Generation)

    • All newly generated objects are first placed in the younger generation. The goal of the young generation is to recover as quickly as possible those objects with short life cycles.
    • The Cenozoic memory is divided into one Eden area and two Survivor (Survivor0,survivor1) regions according to the 8:1:1 ratio. Eden District, literally translated, is the place of Eden, where human life begins. when an instance is created, it is first stored in the area, and most of the objects are generated in the Eden area . Survivor District, survivor area, is literally used to store surviving objects. The Eden zone survivor is copied to a Survivor0 area before being reclaimed, and then the Eden area is emptied, and when the Survivor0 area is full, the surviving objects in the Eden and Survivor0 areas are copied to another Survivor1 area. Then empty Eden and this Survivor0 area, where the Survivor0 area is empty. Then the Survivor0 area and the Survivor1 area are exchanged, that is, to keep the Servivor1 empty, so reciprocating.
    • When the Survivor1 area is not sufficient to store the surviving objects of the Eden and Survivor0, the surviving objects are placed directly into the old generation. If the older generation is full, it will trigger a major GC (that is, the fully GC), that is, both the new generation and the old generation are recycled.
    • the occurrence of the new generation of GC is also called minor GC,MINORGC frequency is relatively high , not necessarily wait until the Eden area full to trigger.

2) older generation (old Generation)

    • Objects that survived after multiple GC in the Cenozoic are put into the old generation. Therefore, it can be considered that older generations are storing objects with longer life cycles.
    • Older generations are much larger than the new generation of memory (roughly 2:1?). ), when the old generation of the time when the major GC, that is, full gc,full GC frequency is relatively low, older generation of the object survival time longer, survival is relatively high .
    • The compacting algorithm is used here, because the region is larger, and the object life cycle is usually longer, compaction takes a certain amount of time, so this part of the GC time is relatively long .

3) Durable generation (Permanent Generation)

persistent generations are used to store static files , such as Java classes, methods, and so on, which are relatively stable and have no significant effect on the GC. This part is also known as the run -time, some versions say JDK1.7 after the section from the method area moved to the GC heap, some version said, JDK1.7 after the part was removed, to be verified.

For garbage collection in a durable generation, see "garbage collection of method areas" below.

Generaton algorithm Structure Mind Mapping

Modern commercial virtual machines are basically using generational collection algorithms for garbage collection. This algorithm is nothing special, is to combine the above-mentioned algorithms, according to the different life cycle of the object to divide the memory into several pieces, and then according to the characteristics of the most appropriate collection algorithm. The low survival rate of the new generation of objects, the use of replication algorithm, in different areas of the internal replication, replication costs are relatively low; older generations have a high survival rate, no additional space for allocation, and a tag-cleanup algorithm or marker-collation algorithm.

Iv. garbage collector

Garbage collector is the specific implementation of the garbage collection algorithm theory in the previous section. The garbage collectors provided by different virtual machines can vary greatly, and we usually develop a hotspot virtual machine, which is all the collectors included in the virtual machine. As mentioned in the previous section, the modern commercial virtual machine basically adopts the generational collection algorithm, which shows the usage of each garbage collector in different generation of memory, and uses different collectors in different life cycles of the objects. There is no best garbage collector, no universal collector, only the most appropriate collector for the specific application, which is why the hotspot is implementing so many collectors.

The implementation of the 7 garbage collectors shown in a lifecycle phase or use alone, or in conjunction with (Wired) implementations, is implemented in accordance with the various collection algorithms described in the previous section. As for the detailed description of each garbage collector, this article began to explain, interested in self-study.

Extended reading 53983650

V. Garbage collection in the method area

As I said at the beginning of this article, GC occurs primarily in GC heap memory, but not only in this part, but also in the method area for garbage collection . The method area, like the heap, is an out-of-the-box shared memory area that is used to store data such as class information that has been loaded by the virtual machine, immediately compiled code, static variables, and constants. Depending on the Java Virtual Machine specification, the OutOfMemoryError exception is thrown when the method area fails to meet the memory allocation requirements, although the specification can not implement garbage collection because the recovery efficiency of the method area is too low compared to GC heap memory garbage collection, but that part of the area can also be recycled.

The garbage collection of the method area mainly consists of two kinds: obsolete constant recycle and garbage collection.

1. When a constant object is not referenced anywhere, it is marked as an obsolete constant, which can be recycled. In the case of literal constant recycling, if a string "ABC" has entered a constant pool, but the current system does not have any string object referencing the literal called "abc", then "ABC" will be moved out of the constant pool if GC occurs, and other classes (interfaces) in the constant pool , methods, and field symbol references are similar.

2, the class in the method area needs to meet the following three conditions to be marked as useless class: (1) No instance object of the class exists in the Java heap, (2) The class loader loading the class has been recycled; (3) The corresponding Java.lang.Class object of this class is not referenced anywhere, and the method of the class cannot be accessed anywhere by reflection. When a class that satisfies these three conditions can be recycled, but not necessarily recycled, parameters need to be controlled, and the-XNOCLASSGC parameter is provided in the Hotspot virtual machine to control whether it is recycled.

As mentioned in the previous article, the run-time constant is present in the method area before jdk1.7, which is also known as the Permanent generation (permanence Generation). In the previous section of the Generaton algorithm, the permanent generation was also mentioned. The method area garbage collection is the garbage collection of permanence Generation (this is the author's personal understanding, there is no authoritative information to declare this).

Vi. reference types

There are four references, strong references, soft references, weak references, and virtual references in Java, so there are two purposes: (1) It allows programmers to determine the life cycle of certain objects through Code, and (2) facilitates the JVM's garbage collection.

1. Strong references

A strong reference is one that creates an object and assigns the object to a reference variable. Like what:

1 people people = New people ();
2 String str = "ABC";

When a strong reference has a reference variable pointing, it will never be garbage collected by the JVM, and the JVM would rather throw a outofmemory exception than a strong reference object when the system memory is tight . Like what:

1 public class Referencedemo {2  3 public     static void Main (string[] args) {4         Referencedemo demo = new Referen Cedemo ();  5         demo.test (); 6     } 7  8 public     Void Test () {9         people people = New people ();         people[] Peoplearr = new People[1000];11     }12}13 class People {public     String name;16 public     int age;17     P Eople () {         this.name = "Zhangsan";         this.age = 20;21}22 All public     people (String name, Int. age) {24         this.name = name;25         this.age = age;26     }27     @Override29 public     String toString () {         Return "[Name:" + name + ", Age:" + Age + "]";     }32}

When running to people[] Peoplearr = new people[1000]; when there is not enough memory, the JVM throws an oom error and does not reclaim the object pointed to by object. It is important to note, however, that when Test runs out, both people and Peoplearr will no longer exist, so the objects they point to will be reclaimed by the JVM.

if you want to break the association between a strong reference and an object, you can display the reference assignment to null so that the JVM reclaims the object when it is appropriate. For example, in the clear () method of the vector class, the cleanup is done by assigning a reference to NULL.

2. Soft Reference (SoftReference)

(1) using SoftReference to implement soft references

if an object has soft references and enough memory space, the garbage collector does not recycle it. The memory of these objects is reclaimed only if there is insufficient memory space . The object can be used by the program as long as it is not reclaimed by the garbage collector. Soft references can be used to implement memory-sensitive caches, such as Web caching, image caching, and so on. Using soft references can prevent memory leaks and enhance the robustness of the program.

The SoftReference feature is that an instance of it holds a soft reference to a Java object, and the existence of the soft reference does not prevent the garbage collection thread from reclaiming the Java object. Once SoftReference has saved a soft reference to a Java object, the Get () method provided by the SoftReference class returns a strong reference to the Java object before the garbage thread recycles the Java object. Also, once a garbage thread reclaims the Java object, the Get () method returns NULL.

1 people people = New People (), 2 softreference softref = new softreference<> (people);

At this point, for this people () object, there are two reference paths, one is a strong reference from Mpeople, and the other is a soft reference from SoftReference. So this people () object here is a strong and accessible object. The strong reference to this people object can be ended by the following way: Mpeople

1 people = null;

Thereafter, the people () object becomes a soft reference object. If the garbage collection thread is in memory garbage collection, it does not always retain the object because it has a softreference reference to the object.

The JVM's garbage collection thread treats soft-and objects differently from other generic Java objects: The cleanup of soft and object is determined by the garbage collection thread according to its memory requirements based on its particular algorithm. The garbage collection thread reclaims the soft objects before throwing OutOfMemoryError in the virtual machine, and the virtual opportunity reclaims the soft objects that are unused for long periods of time as much as possible, and the JVM retains as much as possible the "new" soft objects that have just been built or used. Before we recycle these objects, we can pass:

1 People softrefpeople =  (People) softref.get ();

Regain a strong reference to the instance. If the soft and the object is also recycled, then msoftref.get () can only get null.

The following code demonstrates the use of SoftReference:

1 public class Referencedemo {2  3 public     static void Main (string[] args) {4         people people = New people ();//To Strong reference from People 5         softreference softref = new softreference<> (people);//Soft reference from SoftReference 6         people = NULL ;//End people a strong reference to the people instance. 7         People softrefpeople = (people) softref.get ();//Get back a strong reference to people by getting (). 8         System.out.println (softrefpeople.tostring ()); 9     }10}

The results of the operation are as follows:

1 [name:zhangsan,age:20]

(2) Use Referencequeue to clear the softreference that lost the soft reference object

In addition to having the particularity of saving soft references, the SoftReference object is also a Java object and has the general characteristics of Java objects. Therefore, when the soft and object is recycled, the Get () method of the SoftReference object returns NULL, no longer has the value of being present, and an appropriate cleanup mechanism is required to avoid the memory leaks caused by a large number of SoftReference objects. The Referencequeue is also available in the Java.lang.ref package. If a Sorftreference object is created, a Referencequeue object is used as the constructor for the softreference, such as:

People people = new people (); Referencequeue queue = new referencequeue<> (); SoftReference SoftRef2 = new SoftReference (people,queue);

When the people that the SoftReference soft reference is reclaimed by the garbage collector, the SoftReference object that the SoftRef2 strongly references is included in the Referencequeue. That is, the object saved in Referencequeue is a reference object, and it is a reference object that has lost its soft reference object. Also, from the name of Referencequeue, it is a queue that, when we call its poll () method, returns the first reference object in the queue if it is not an empty queue. At any time, we can call Referencequeue's poll () method to check if there are non-strong objects that it cares about being recycled. If the queue is empty, a null is returned, otherwise the method returns the first reference object in the queue. Using this method, we can check which softreference the soft reference object has been recycled. So we can erase those softreference objects that have lost the soft reference object.

1 SoftReference softRef3 = null;2 while ((SOFTREF3 = (softreference) queue.poll ())!=null) {3     //Clear REF4}

3. Weak references (WeakReference)

Weak references are also used to describe non-essential objects, and when the JVM is garbage collected, the objects associated with the weak references are reclaimed regardless of the adequacy of the memory. In Java, it is represented by the Java.lang.ref.WeakReference class. Here's how to use it:

1 public class Referencedemo {2 3 public     static void Main (string[] args) {4         weakreference<people> weakRef = New Weakreference<people> (New People ()); 5         System.out.println (Weakref.get ());//Gets the object protected by a weak reference. 6         System.GC ();//notify JVM for garbage collection 7         System.out.println (Weakref.get ()); 8     }9}

The results of the operation are as follows:

1 [name:zhangsan,age:20]2 NULL

The second result is null, which means that the object associated with the weak reference must be reclaimed as long as the JVM is garbage collected. Note, however, that the object referred to here is associated with a weak reference, meaning that only a weak reference is associated with it. If a strong reference is associated with it, the object is not reclaimed when garbage collection is present (as is the case with soft references). For example, make a small change to the code:

1 public class Referencedemo {2  3 public     static void Main (string[] args) {4         people people = New people (); 5< c3/>weakreference<people> weakRef = new weakreference<people> (people); 6         System.out.println (Weakref.get ()); 7         System.GC (); 8         System.out.println (Weakref.get ()); 9     }10}

The result of the operation is as follows: The second line has a big change, no longer null, and the description is not recycled.

1 [name:zhangsan,age:20]2 [name:zhangsan,age:20]

Weak references can also be used in conjunction with the reference queue Referencequeue, and if the object referenced by the weak reference is reclaimed by the JVM, the weak reference is added to the reference queue associated with it, using the same method as the soft reference.

When using soft references and weak references, we can display the System.GC () to notify the JVM of garbage collection, but note that, although a notification is issued, the JVM does not necessarily execute immediately, that is, the code is not sure that the JVM will be garbage collected at this time, It may be recycled at a suitable time after the notification has been given.

In addition, in Android development, it is often used in conjunction with handler to avoid the occurrence of memory leaks.

4. Virtual Reference (Phantomreference)

In Java, represented by the Java.lang.PhantamReference class. if an object is associated with a virtual reference, it can be reclaimed by the garbage collector at any time, as with no reference to it . It is important to note that a virtual reference must be used in association with a reference queue, and when the garbage collector is ready to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it (this differs from the previous soft and weak references, both of which join the queue when the object is reclaimed). The program can see if the referenced object is going to be garbage collected by judging whether the reference queue has been added to the virtual reference. If the program discovers that a virtual reference has been added to the reference queue, it can take the necessary action before the memory of the referenced object is reclaimed.

1 public class Referencedemo {2 3 public     static void Main (string[] args) {4         referencequeue<people> queue = n EW referencequeue<> (); 5         phantomreference<people> phanref = new Phantomreference<people> (new People (), queue), 6         System.out.println (Phanref.get ()); 7     }8}

The results of the operation are as follows:

1 null

The result verifies that, "if an object is associated with a virtual reference, it can be recycled at any time by the garbage collector, just as there is no reference associated with it."

5. Summary

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.