Java Virtual Machine learning Note: Garbage collection methods in the JVM memory model

Source: Internet
Author: User
Tags xms
The previous article introduced the JVM memory model of the relevant knowledge, in fact, there are some content can be more in-depth introduction, such as the dynamic insertion of the constant pool, direct memory, and so on later time to perfect the next blog, today to introduce some of the JVM garbage collection strategy.

one, Finailize () method

Before introducing GC policy, introduce the Finailize method in the GC. When an object does not have any references, the object is usually recycled, but what if we want to do something before the object is recycled, such as shutting down some resources, or getting the object to revive, and not let him be recycled? This is the time to use the Finailize method. The Finailize method is a method defined in the object class, which means that any object has this method. But this method will only be called once, if the object is resurrected after the death of the object, the 2nd time to reclaim the object is not called the Finailize method, and the priority is relatively low, and is not guaranteed to be executed, so it is not recommended to use the Finalize method. Summed up is 3 features: ①, GC was called before. ②, will only be called once. ③, unreliable, not guaranteed to be executed, is not recommended for use . For the Finalize use method, refer to the following code:

 1 public class Finalizetest {2 3 private static finalizetest test; 4/** 5 * VM parameters:-xx: +PRINTGCDETAILS-XM X=1M-XMS=1M 6 * 7 * @param args 8 */9 public static void Main (string[] args) {10//Assign a value to the test object first One test = new finalizetest (); int _1m = 1024 * 1024;13//SET test to NULL for easy recovery of test = NULL; try {System.GC () 17//Analog sleep 5s,finalize low priority to ensure that finalize can perform the Thread.Sleep ( (Interruptedexception e) {e.printstacktrace ();}22 if (Test! = N  ull) {System.out.println ("First,i am Alive"),}else{25 System.out.println ("First,i am             Dead "); 26}27//Because test is resurrected in the Finalize method, the test is again set to null28 test = null;29 try {30 System.GC (); Thread.Sleep (5000);//Simulated sleep 5s, let the GC reclaim the} catch (Interruptedexception e) {e.    Printstacktrace (); 34     }35 if (test! = null) {System.out.println ("Second,i am Alive"); PNs}else{38 System.out.println ("Second,i am Dead");}40}42 @Override43 protected void Finalize () throws Thr   owable {test = this; System.out.println ("Finalize excuted"); Super.finalize (); Call the Finailize method of the parent class 47}48}

The result of this code operation is as follows:

As you can see, after the Finalize method executes, the test object is reactivated again, so the first,i am Alive is printed. But in the second GC, the Finalize method was not executed, so the Second,i am dead was printed. The previous reference to finalize is that the priority is low and unreliable, and if there is no Thread.Sleep (5000), then look at the code and the results:

 1 public class Finalizetest {2 3 private static finalizetest test; 4/** 5 * VM parameters:-xx: +PRINTGCDETAILS-XM X=1M-XMS=1M 6 * 7 * @param args 8 */9 public static void Main (string[] args) {10//Assign a value to the test object first One test = new finalizetest (); int _1m = 1024 * 1024;13//SET test to NULL for easy recovery of test = NULL; try {System.GC () 17//Analog sleep 5s,finalize low priority, ensure that finalize can perform 18//Do not perform sleep operation, THR Ead.sleep (Exception e) {e.printstacktrace ();}22 if (Test! = Nu ll) {System.out.println ("First,i am Alive"),}else{25 System.out.println ("First,i am Dead "); 26}27//Because test is resurrected in the Finalize method, the test is again set to null28 test = null;29 try {S Ystem.gc (); 31//Do not perform sleep operation, Thread.Sleep (5000);//Simulated sleep 5s, allow GC to reclaim the catch (Exception e) {E.PR      Intstacktrace (); 34   }35 if (test! = null) {System.out.println ("Second,i am Alive"); PNs}else{38 S Ystem.out.println ("Second,i am Dead");}40}42 @Override43 protected void Finalize () throws Throw   Able {test = this; System.out.println ("Finalize excuted"); Super.finalize (); Call the Finailize method of the parent class 47}48}

The results of the operation are as follows:

It is clear here that the priority of the Finalize method is relatively low.

A reflection on this example: the first piece of code in this example is implemented in the code of "in-depth understanding of Java Virtual Machine", but there is a total of 2 questions: Why is the test object present as a static-modified member variable? In the case of static modification, there is a method area, and the GC of the method area usually does not work well. The other is in the form of member variables, so that when the Finalize collection is not reflected in the current object itself is recycled, so the feeling this example is not very good.

Second, the reference counting method

Reference Counting method is a relatively early GC recovery algorithm, the main idea is that: each object maintains a reference counter, the initial value is 0, when an object is referenced, the object's reference counter is added 1, when not referenced, The object's reference counter is reduced by 1, and if the reference counter of an object becomes 0, the object is considered recyclable . The advantages and disadvantages of this approach are obvious, the advantage is simple implementation, high efficiency, the disadvantage is that there may be circular references, resulting in memory overflow.

Third, mark-Clear Method

The mark-and-erase method is divided into 2 stages of "Mark" and "purge" by name, the basic idea is that all the surviving objects are marked first, and after the mark is complete, all the tagged objects are cleared uniformly. How can you tell if an object is recyclable? GC, starting from a series of GC roots root node traversal, traversed by the path is called the reference chain, if an object and GC roots no reference chain related, then this object is not available, it will be judged to be recyclable, this algorithm is also called the root search algorithm . So which objects can become GC roots objects? In the Java language, the objects that can be used as GC roots include the following 4 types:

Reference variables in the virtual machine stack

Object referenced by class static property in the method area

The object referenced by a constant in the method area

Object referenced by JNI (that is, the native method) in the local method stack

The algorithm for the tag-purge method is as follows:

NOTE: The GC recovery algorithm in this paper is transferred from a Netizen's article (point here), the user's picture content is also consistent with the original, but the color is different.

Iv. reproduction method of the Cenozoic

The basic idea of copying is to divide the memory into 2 blocks of equal size, using only one block at a time, and each time the GC copies all the surviving objects to another area and cleans up the memory .

These are all reference objects in the method area and the stack. The advantage of replication is that it is simple to implement, fast to recycle, and does not produce memory fragmentation. However, memory utilization is low due to the use of only one piece at a time. The following are the replication algorithms:

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.