Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
Some knowledge about memory management and garbage collection has emerged from time to time throughout the tutorial. Here is a small summary.
Java runs in the memory environment virtualized by JVM. Memory is divided into two parts: Stack and heap. We will examine these two regions separately.
Stack
The basic concepts of stacks can be discussed on paper: stacks ). Many languages use the stack data structure to record the order of function calls and related variables (refer to Linux from program to process ).
In Java, the stack in JVM records thread method calls. Each thread has a stack. If a new method is called during the running of a thread, a storage unit (FRAME) is added to the stack corresponding to the thread ). In the frame, save the parameters, local variables, and return address of the method call.
Call Stack
Java parameters and local variables can only beBasic TypeOrReference(Reference ). Therefore, only variables and object references of basic types are saved in the stack.
The referenced object is stored in the heap. (The reference may be a null value, that is, it does not point to any object)
References and objects
When the call method ends, the frames corresponding to the method will be deleted, and the space occupied by parameters and local variables will be released. The thread returns to the original method and continues execution. When all stacks are cleared,ProgramIt also ends.
Heap
As mentioned above, stacks can take care of themselves. But heap must be taken with caution. Heap is a zone in the JVM that can be freely allocated to objects. When we talk about garbage collection, we mainly RecycleHeap).
Java common objects are stored in the heap. Unlike the stack, the stack space will not be cleared as the method call ends. Therefore, the object created in a method can continue to exist in the heap after the method call is completed. One problem with this is that if we constantly create new objects, the memory space will eventually be exhausted.
Garbage Collection
Garbage Collection (GC) can automatically clear objects that are no longer used in the heap. The garbage collection mechanism first appeared in 1959 and was used to solve problems in LISP Language. Garbage collection is a major feature of Java. Not all languages have the garbage collection function. For example, in C/C ++, there is no garbage collection mechanism. The programmer needs to manually release the heap memory.
Since you do not need to manually release the memory, programmers can also reduce the chance of making mistakes in programming. With garbage collection, programmers can avoid some bugs related to pointer and Memory leakage (this type of bug is usually concealed ). However, garbage collection requires more computing time. Garbage collection actually transfers the responsibilities of programmers to computers. It takes longer to run the garbage collection program.
In Java, the object is used by reference (the object is like a fatal poison, and the reference is like a tweezers used to extract the poison ). If there is no reference pointing to an object, we will no longer be able to call or process the object. Such an object willNot reachable(Unreachable ). Garbage collectionRelease inaccessible objectsMemory occupied. This is the basic principle of garbage collection.
(The Inaccessible object is a dead object, which is the garbage to be recycled by garbage collection)
Early garbage collectionReference count(Reference counting. Each object contains a counter. When there is a new reference pointing to this object, add 1 to the counter. When the reference is removed, the counter minus 1. When the counter is 0, the object can be recycled.
However, one possible problem is that if there are two objectsLoop reference(Cyclic reference). For example, if two objects reference each other and there is no reference (pointing to a or pointing to B) at this time, we can't actually reach these two objects by referencing.
Therefore, we use stack and static dataRoot)Starting from the root and following all references, you can find all reachable objects. That is to say, a reachable object must be referenced by the root or another reachable object.
Orange, reachable; green, not reachable
JVM implementation
JVM garbage collection is a mixture of multiple mechanisms. The JVM determines the garbage collection method based on the program running status.
Let's first understand"Mark and sweep". Under this mechanism, each object will have tag information to indicate whether the object can arrive. When garbage collection starts, the Java program stops running. JVM starts from the root, finds all reachable objects, and marks (mark ). Then, JVM needs to scan the entire heap, find the remaining objects, and clear the memory occupied by these objects.
The other is"Copy and sweep". Under this mechanism, the heap is divided into two areas. The object always exists in one of the two regions. When garbage collection starts, the Java program stops running. JVM starts from the root, finds the reachable objects, copies the reachable objects to the blank area, and closely arranges them to modify the reference address changes caused by object movement. Finally, clear the entire area where the object was originally alive and make it a new blank area.
We can see that "copy and sweep" requires more complex operations, but it also allows the objects to be closely arranged to avoid gaps in "Mark and sweep. When you create an object, "copy and sweep" provides a large continuous space. Therefore, if all objects are "Longevity", it applies to "Mark and sweep ". If the object's "metabolism" is active, it applies to "copy and sweep ".
The above two mechanisms are implemented throughGenerational recovery(Generational collection. Each object record has its generation (generation) information. The so-called generation refers to the number of garbage collection times that the object has experienced. Objects of older generations will survive in the memory for longer.
According to the statistical observation of Java programs, the longer the generation of objects, the more likely it is to be recycled (the richer the rich, the poorer the poor ). Therefore, when we recycle garbage, we need to pay more attention to the young objects.
Now, let's take a look at the heap in JVM:
We can see that the heap is divided into three generations. ThePermanent generation(Permanent generation) contains class objects. These objects will not be recycled. We have learned in rtti that each class object represents a class, contains class-related data and methods, and provides class-definedCode. When creating an object, you must refer to the corresponding class object. Each object contains a reference pointing to its corresponding class object.
Young Generation(Young generation) andMature generation(Tenured generation) garbage collection is required. The object generation in the young generation is closer, while the object generation in the mature generation is longer.
Generation
The young generation is further divided into three regions
EDEN (edian): The new object exists in this region. A new object is a new object created after the last GC.
Neighborhood lives in Eden
From,: The two regions are equal in size, which is equivalent to the two regions in copy and sweep.
When the new object cannot be placed in the Eden area, the minor collection starts. JVM adopts the copy and sweep policy to copy the reachable objects in the Eden and from areas to the to areas. After a garbage collection, the Eden and from areas are cleared, while the to areas closely store the surviving objects. Then, the from area becomes the new to area, and the to area becomes the new from area.
If you find that the to area cannot be placed during minor collection, some objects are placed in the mature generation. On the other hand, even if the to zone is not full, JVM will still move the objects of the generation to the mature generation.
If the mature generation is filled with objects and new objects cannot be moved into, the major collection is triggered. JVM adopts the mark and sweep policy to collect garbage from mature generations.
Summary
The above is an overview of JVM memory management. In fact, JVM has many versions. The GC mechanism implemented by different versions varies greatly. On the other hand, Java does not define the jvm gc implementation method. GC is still a hotspot in JVM development. We can expect many changes in the GC mechanism of JVM in the future.
Reference
Http://www.ibm.com/developerworks/java/library/j-jtp11253/
Http://www.cs.princeton.edu/picasso/mats/HotspotOverview.pdf
Http://www.cs.umd.edu/class/spring2008/cmsc433/lectures/gc.pdf
Welcome to continue reading the "Java quick tutorial" SeriesArticle