1. The memory that is managed by the JVM contains a few runtime data regions, where the method area and heap are shared by the thread , the program counters, the virtual machine stack, and the local method stack are thread-private data regions .
Program counter: Can be seen as the line number indicator of the byte code executed by the current thread, telling the bytecode interpreter which instruction to read
Virtual machine stack: The life cycle and threads are the same, each method executes at the same time will create a stack frame, used to store local variables table, operand stack, dynamic link, method exit and other information, each method from the call to completion of the process is corresponding to a stack frame in the virtual machine into the stack into the stack process. The stack contains the various basic data types and object references that the compiler has known.
Local method Stack: Very similar to the virtual machine stack, except that the local method stack serves the native method.
Heap: The object instance is stored in the heap. almost all of the object instances are assigned here . The heap is the primary area that the garbage collector manages. From the memory recovery point of view, because the existing garbage collector basically uses the Generational collection algorithm, so the heap can be subdivided into the new generation and the old age, and then more detailed can be divided into: Eden Space, from Survivor space, to Survivor space.
Method area: Used to store the class information loaded by the virtual machine, constants, static variables, the immediate compiler compiled code and other data. (The hotspot virtual machine extends GC generational collection to the method area, using a permanent generation to implement the method area.) Therefore, the method area is considered as a permanent generation.
Run a constant pool: part of the method area that holds the various literal and symbolic references generated by the compiler.
2. For a string, the reference to its object is stored in the stack, and if the compilation period has been created (defined directly in double quotes), it is stored in a constant pool, which is stored in the heap if the runtime (new) is determined. for a string equal to equals, there is always only one copy in the constant pool, with multiple copies in the heap. when a string is generated by new, the constant pool is first searched for whether the object is already in existence, and if none is created in the constant pool, then a copy object of the object in the constant pool is created in the heap. For example: string s = new string ("XYZ"), and if there is no "xyz" in the constant pool, two objects are generated.
3. How to tell if the object is dead:
(1) Reference counting method: Add a reference counter to each object, have reference counter +1, reference the failure counter-1, and the counter is 0 the object is not available. The disadvantage is that you cannot resolve the issue of circular references.
(2) Accessibility Analysis : This object is not available when an object to GC roots does not have any reference chains connected (a tree-like leaf node is disconnected from the root node).
4. Even those unreachable in the Accessibility analysis algorithm are not immortal, when they are temporarily in the ' probation ' stage, in order to truly declare that an object's death has to go through at least two times the marking process:
If an object is found unreachable in the accessibility analysis, it is first marked and filtered, and the criteria for filtering is whether this object is necessary to perform a Finalize method (when the object does not override the Finalize method of the parent class or the Finalize method has been called). The virtual machine sees no need to implement the Finalize method, and it is worth noting that the object's Finalize method is only called once, and if the object is judged to be necessary to execute the Finalize method, then the object will be placed in a queue called F-queue. And at a later time by a virtual machine automatically establish a low-priority finalizer thread to execute it (the virtual machine departs the Finalize method, but does not necessarily wait for it to complete). Later, the GC will make a second small mark on the objects in the F-queue, and if the object is still marked for recycling (if the object has a reference relationship with other accessible objects before the second tag, it can ' escape '), then the object is essentially recycled.
5. garbage collection generally occurs on the heap, but garbage collection can also occur in the method area . In the heap, especially in the Cenozoic, a garbage collection of conventional applications can generally reclaim 70%~ 95% of space, and the efficiency of permanent garbage collection is much lower. The garbage collection of the permanent generation mainly collects two parts: obsolete constants or useless classes. It is simpler to judge a constant (no object references this constant in a constant pool), and it is more difficult to determine that a class is a useless class:
(1). All instances of this class are recycled
(2). The ClassLoader that loaded the class have been recycled
(1). The corresponding Java.lang.Class object of this class is not used anywhere, and it is no longer possible to access the method of the class by reflection anywhere else.
6. Garbage Collection algorithm:
tag-purge algorithm : Divided into tags, clear two phases, first mark out all the objects that need to be recycled, after the mark is complete, the unified collection of all tagged objects. This algorithm has two deficiencies: first, the efficiency of the two processes is not high, and the second is that the mark will result in a large number of discontinuous memory fragmentation (when the need to allocate large objects can not find enough contiguous memory and have to trigger another garbage collection action).
Copy Algorithm : The available memory is divided into two blocks of equal size, each time only one of the pieces, when a piece of memory ran out to copy the surviving object to another, and then the used memory space once cleaned out. This algorithm is simple and efficient, but the lack of memory is generally reduced.
tag-Grooming algorithm : The tagging process is the same as the tag-purge algorithm, but instead of erasing the memory directly, the surviving objects are moved to one side, and the unexpected memory is cleared off the boundary.
Generational collection Algorithm : Divide the memory into several blocks according to the different life cycle of the object, generally divide the Java heap into the new generation and the old age. Now the commercial virtual machine commonly uses the replication algorithm to recover the Cenozoic generation , because most of the new generation of objects are "to die", so do not need to divide the memory space by 1:1, but divided into the larger Eden area and two smaller survivor area, Copy the surviving objects to another Survivor (from Survivor) each time you use Eden and a piece of SURVIVOR,GC, and finally clean up the Eden and Survivor (to Survivor) you just used. The default Eden and survivor ratio for the hotspot virtual machine is 8:1, which means that the memory space available for each new generation is 90% (80%+10%) for the entire Cenozoic. When Survivor (to Survivor) has insufficient memory, it is necessary to rely on other memory for allocation guarantees (old age). The replication algorithm is inefficient when the object has a high survival rate and if you don't want to waste 50% of memory, you need extra space to wake up the allocation guarantee, so it is not suitable for the old age of such a large number of surviving objects in the region, so the general use of marker-collation algorithm to recycle the old
7. Memory allocation and recovery policy:
The automatic memory management advocated in the 1.Java system ultimately boils down to two issues: allocating memory to objects and reclaiming memory allocated to objects. The objects are mainly distributed in the new generation of Eden, and in rare cases may be distributed directly in the old age.
(1) In the case of large logarithm, the object is allocated in the new generation of Eden, and when the Eden area does not have enough space to allocate, the virtual machine will initiate a Minor GC.
(2). Large objects directly into the old age, the so-called large object refers to the need for a large number of contiguous memory space objects, the most typical large object is the kind of long strings and arrays
(3) long-term survival of the object will enter the old age. If the object is still alive after the first Minor GC is born and can be accommodated by survivor, it will be moved to survivor space, and the object age is set to 1, and the object is increased by 1 years after each Minor GC, when his age increases to a certain extent ( By default, 15 years old) will be promoted to the old age. Virtual machines do not always require the age of the object to reach Max tenuring threshhold to be promoted to the old age, if the sum of all objects of the same age in Survivor space equals the general of Survivor Space, Objects older than or equal to that age can enter the old age without waiting for the age required by Max Tenuring threshhold.
(4) Before the Minor GC occurs, the virtual opportunity first checks whether the most contiguous space available in the old age is greater than the total space of all new generation objects, and if this condition is true, then the Minor GC can ensure that it is safe.
Java memory allocation and garbage collection