Home  >  Article  >  Java  >  Detailed explanation of examples of singleton mode garbage collection in Java

Detailed explanation of examples of singleton mode garbage collection in Java

黄舟
黄舟Original
2017-08-08 10:25:341594browse

This article mainly introduces the relevant information of singleton mode garbage collection in detail, which has certain reference value. Interested friends can refer to

Discussion proposition: When a singleton object is not used for a long time, will it be recycled by the jvm's garbage collection mechanism?

First let me talk about why this question arises. The author has never considered the impact of garbage collection on the singleton pattern before. Until I read a book last year, "Zen of Design Patterns" 》 by Qin Xiaobo. It is mentioned in the book that in j2ee applications, the jvm garbage collection mechanism will treat singleton objects that have not been used for a long time as garbage and recycle them when the CPU is idle. Several design pattern books I have read before, including "Java and Patterns", did not mention the impact of the jvm garbage collection mechanism on singletons. And during the work process, I have never had the experience of singleton objects being recycled. In addition, many seniors at work have warned me: try not to declare too many static properties, because these static properties will not be released after being loaded. Therefore, I am skeptical about the statement that jvm garbage collection will recycle singleton objects. Gradually, I found that among my colleagues and technical staff on the Internet, there were basically two opposing factions on this issue. So will jvm recycle singleton objects that have not been used for a long time?

Regarding this issue, the author’s personal opinion is:

will not recycle.

My test code is given below


class Singleton {
 private byte[] a = new byte[6*1024*1024];
 private static Singleton singleton = new Singleton();
 private Singleton(){}
 
 public static Singleton getInstance(){
 return singleton;
 }
}

class Obj {
 private byte[] a = new byte[3*1024*1024];
}

public class Client{
 public static void main(String[] args) throws Exception{
 Singleton.getInstance();
 while(true){
  new Obj();
 }
 }
}

## The purpose of this program is to simulate the j2ee container, first instantiate the singleton Class, this singleton class occupies 6M of memory, and then the program enters an infinite loop, constantly creating objects, forcing the jvm to perform garbage collection, and then observe the garbage collection information. If the memory is still greater than 6M after garbage collection, it means that the garbage collection will not Recycle singleton objects.


The virtual machine used to run this program is the hotspot virtual machine, which is the virtual machine officially provided by Java that we use most, commonly known as jdk, and the version is jdk1.6.0_12


The runtime vm arguments parameters are: -verbose:gc -Xms20M -Xmx20M, which means that the memory information is displayed every time the jvm performs garbage collection, and the jvm's memory is set to a fixed 20M.


Running result:

……
[Full GC 18566K->6278K(20352K), 0.0101066 secs]
[GC 18567K->18566K(20352K), 0.0001978 secs]
[Full GC 18566K->6278K(20352K), 0.0088229 secs]
……

## From the running result, you can see that there is always 6M space that has not been used collect. Therefore, the author believes that, at least in the hotspot virtual machine, garbage collection will not recycle singleton objects.

Later, I checked some relevant information. The garbage collection algorithm of the hotspot virtual machine uses the root search algorithm. The basic idea of ​​​​this algorithm is: for any "live" object, it must ultimately be traced back to its reference living in the stack or static storage area. Using a series of references named roots (GC Roots) as the starting point, starting from these roots and searching through a series of paths, if the object in the Java heap can be reached, then the object is "live" and cannot be recycled. Objects that can be used as roots are:


 Objects referenced in the virtual machine stack (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 in the local method stack.

  • The method area is a memory area of ​​the jvm, used to store class-related information. Obviously, the object created by the singleton mode in Java is referenced by the static properties in its own class, which complies with the second article. Therefore, the singleton object will not be garbage collected by the JVM.

Although the singleton object in the jvm heap will not be garbage collected, will the singleton class itself be collected if it is not used for a long time? Because jvm also has a garbage collection mechanism for the method area. If the singleton class is collected, the objects in the heap will lose their path to the root and will inevitably be garbage collected. In this regard, the author checked the garbage collection method of the method area of ​​the hotspot virtual machine. The judgment conditions for the jvm uninstallation class are as follows:


All instances of this class have been recycled. That is, there is no instance of this class in the java heap.
  • The ClassLoader that loaded this class has been recycled.
  • The java.lang.Class object corresponding to this class is not referenced anywhere, and the methods of this class cannot be accessed through reflection anywhere.

Only when all three conditions are met, the jvm will unload the class during garbage collection. Obviously, the singleton class does not meet condition 1, so the singleton class will not be unloaded. That is to say, as long as the static reference in the singleton class points to the singleton object in the jvm heap, then the singleton class and singleton object will not be garbage collected. According to the root search algorithm, whether the object will be garbage collected or not The length of use does not matter, it just depends on whether the object is "live". If an object has not been used for a long time and is recycled, then the collection algorithm should be the longest recently unused algorithm. The longest recently unused algorithm is generally used in the internal and external memory exchange of the operating system. If it is used in virtual machine garbage collection, wouldn't it be Too unsafe? The above is the author's opinion.

The above is the detailed content of Detailed explanation of examples of singleton mode garbage collection in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn