Chapter 1- objects death decision algorithm

Outline

Garbage collection (Garbage Collection, GC), accompanying the product it is not the Java language, its history much older than Java.

People think three main things GC needs to be done:

  1. What memory need to recycle?
  2. When the recovery?
  3. How to recycle?

To the present, dynamically allocated memory and memory recovery technology is already quite mature. So why do we need to understand the GC and memory allocation it? The answer is simple: when you need to troubleshoot a variety of memory overflow, memory leak problem becomes when the garbage collection system to achieve higher concurrency bottleneck, we need to intervene in the operation process GC.

Java's garbage collection focuses on the heap and method area. Program counter, stack virtual machine, native method stacks with three regions born threads, with threads OFF: the stack and the stack frame with the entry and exit method performed with an orderly operation of the station and the stack. How much memory is allocated for each stack frame is substantially determined when the class structure down to known, memory allocation and recovery of these regions are provided with certainty when the end of the process or the thread ends, just as the memory recovery NATURAL a. The Java heap and method area is not the same, more of a class needs to implement memory interfaces may be different, multiple branches need a method of memory may not be the same, we are only in the program in order to know when during operation which objects will be created, which is part of the memory allocation and recovery are dynamic garbage collector concern is this memory.

Object death judgment algorithm

Reference counting algorithm

Ideas: adding a reference to the object counter, whenever a reference to its place, the counter value is incremented by 1; when referring to the failure, the counter value is decreased by 1; 0 any time counter object is no longer being used.

Advantages: simple, high efficiency.

Cons: reference each other circulatory problems can not be solved objects.

Case: Microsoft's COM technology, the use of ActionScript FlashPlayer 3, Python language.

Reachability analysis algorithm

Through a series of "GC Roots" object as a starting point, to start the search downward from these nodes, called search path traversed reference chain (Reference Chain), when an object is not connected to any reference GC Roots strand: Thinking , then it proves that this object is not available. This algorithm can solve the problem of mutual references between objects.

In the Java language, as an object GC Roots may include the following categories:

  1. Virtual Machine stack (Local Variable Table stack frame) in the object reference.
  2. Method static property class object referenced area.
  3. Object literal reference methods zone.
  4. Object native method stack JNI (Native Method) references.

Java references category

To describe some "tasteless gesture," the object we want enough space in the memory, remain in memory, if memory after garbage collection is still relatively tight, then abandon these objects. So Java concept was expanded references, references are organized as follows (quoted in turn weakened the strength):

  • A strong reference (Strong Reference): similar to the "Object obj = new Object ()" such a reference, as long as there are strong references, GC will never recover lost referenced object.
  • Soft references (Soft Reference): For such objects, OOM when the system will take place, these objects will be included in the scope of recovery for the second recovered. If the second recovery has not enough memory, memory overflow exception will be thrown. It provides in Java SoftReferenceto achieve soft references.
  • Weak references (Weak Reference): For such objects, only live to occur before the next GC, when GC occurs regardless of the adequacy of the current memory, the recovery will be out only a weak reference object associated. It provides in Java WeakReferenceclass to implement a weak reference.
  • Virtual reference (Phantom Reference): also become ghost or phantom cited references, it is the weakest one reference relationship. Whether there is a phantom reference object has completely will not affect their survival time, can not be obtained by a phantom reference object instance. The sole purpose of setting a virtual reference associated with the object is able to receive a notification when the object system is recovered collector. It provides in Java PhantrmReferenceto implement virtual reference.

Recovery process area

Java virtual machine specification does not require a virtual machine implementation of garbage collection in the zone method, and the method area were almost no object will be recovered after a garbage collection.

Generational garbage collection main permanent recycling of two parts: a constant and useless waste classes. Constant is determined whether or not a "waste constant" is relatively simple, and it is determined whether or not a category is "useless class" is relatively many harsh conditions. Class needs to meet the following three conditions in order to be regarded as "useless class."

  • All instances of the class have been recovered, which is in the heap any instance of the class does not exist.
  • ClassLoader class load has been recovered.
  • Java.lang.Class corresponding to the class object is not referenced in any place, not by the method of accessing the class reflected anywhere.

Virtual machines can be kind of useless meet the above three conditions are recovered, said here is just "may", rather than as objects and, without a bound collection. Whether the class is recovered, HotSpot VM also provides -Xnoclassgcparameter control can also be used -verbose:classas well -XX:+TraceClassLoading, -XX:+TraceClassUnLoadingsee the class load and unload information, -verbose:classand -XX:+TraceClassLoadingcan be used in Product version of the virtual machine, the -XX:+TraceClassUnLoadingparameters required FastDebug version of the virtual machine support.

In the use of a large number of reflective, dynamic proxies, etc. CGLib ByteCode frame, and dynamically generated JSP OSGI defines ClassLoader such frequent scenarios need to have the virtual machine based unload function, to ensure that the method is not overflow.

Source: Zhou Zhiming of "in-depth understanding of the Java Virtual Machine: JVM advanced features and best practices", Second Edition

Guess you like

Origin www.cnblogs.com/wuqinglong/p/11128453.html