There are 4 ways to describe memory allocation and recycling, namely how memory is allocated , what memory needs to be recycled , under What circumstances , and how to monitor and optimize the GC mechanism .
Java GC (garbage collction) garbage collection mechanism is one of the main differences between Java and C + +. Self-reclaim some useless memory by tagging the memory in the JVM. The most current use is the hotspot in the sun company JDK, so this article also uses the JVM as the root of the introduction.
1 . Java Internal Storage area
In the Java runtime Data fetch, the memory area managed by the JVM is divided into several parts:
Program Counter Register: The program counter is a comparative memory unit used to represent an indicator of where the current program is running. Because each thread is executed in its own order, the program counter is thread-private, and each thread is instructed by its own program counter to indicate which instruction (thread) to execute next.
If the program executes a Java method, the counter records the virtual machine bytecode instruction address that is executing, and if you are performing a local method (native method), the value of the counter is undefined. Because the program counter records only the current instruction address, there is no memory leak, and it is the only area in the JVM memory area that does not have the oome (out of storage error) definition.
Virtual machine stack (JVM stack): When each method of a thread executes, a stack frame is created to store the local variables in the method, method exits, and so on, and the stack frame is placed in the JVM stack, and the stack frame is stacked when the method call is complete. Each thread has its own virtual machine stack to hold the data of its own method invocation, so the virtual machine stack is also thread-private.
Two exceptions are defined in the virtual machine stack, which throws stackoverflowerror if the thread call has a stack depth greater than the maximum allowable depth for the virtual machine, but the virtual machine basically allows the size of the virtual machine stack to be dynamically scaled. In this case, the thread can always request the stack until the memory is low and throws a oome (out of memory error) overflow.
Local method Stack Native: The local method stack is similar to the virtual machine stack, except that the stack frame stored on the local method stack is generated when the Native method is called. In some virtual machines, the local method stack and the virtual stack are put together, so the local method stack is also thread-private.
Heap: Heap is the most important area in the Java GC mechanism. The heap is to place an " instance of an object " where the object is allocated memory on the heap, and the heap is logically contiguous, not necessarily physically contiguous. All threads share a heap, the size of the heap is extensible, and if there is not enough memory to allocate after the GC is executed and the heap size is not extendable, Oome will be thrown.
method Area: Also known as static zone, used to store the information of the class, Chang, etc., is logically part of the heap, is the area shared by each thread, in order to distinguish from the heap, also known as non-heap. The method area is used as a permanent generation when the permanent generation is still present. The method area can choose whether to turn on garbage collection. Oome is thrown when the JVM is running out of memory.
DirectMemory: Direct memory refers to non-JVM-managed memory, which is the remaining memory of the machine. Memory allocations are made using channels (channel) and buffers (buffer), which are referenced by Directbytebuffer stored in the JVM and thrown oome when the machine itself is out of memory.
Example: Object obj = new Object ();
obj represents a local reference that is stored in the local variable table of the JVM stack , where new object is placed in the heap as an object, the type information (interfaces, methods, object types, and so on) of the object class is placed in the heap, and the addresses of these types of information are placed in the method area.
Here you need to know how to access a specific object by reference, that is, how to find the object by means of obj, which can be found in new, mainly in two ways, through a handle and through a direct pointer .
by handle:
In the Java heap, a particular area is divided into a handle pool, a reference behind a pointer to an object instance data (in the Java heap) and a pointer to the object type information (in the method area), both of which are on the Java heap. This method is more stable than the advantage, but the speed is not very fast.
by direct pointer :
A reference is behind an object's instance data, which contains "pointers to object type information". The advantage of this approach is that it's fast, and that's the way it's used in hotspots.
2. How the memory is allocated and recycled
Memory allocation is mainly on the heap allocation, such as the previous new out of the object, placed on the heap, but modern technology also supports the allocation on the stack, relatively rare, this article does not consider. The standard for allocating memory and reclaiming memory is eight words: generational allocation, generational recycling. So what is this generation?
The JVM divides the objects into three generations according to their surviving time: The young Generation, the old Generation, and the Permanent generation (permannent Generation). The permanent generation is no longer used in jdk1.8, so it is no longer introduced here.
Young generation : Also known as the New generation, all the newly generated objects are placed first in the young generation. The young generation is divided into three districts, one Eden area, two survivor districts, one called from, and one called to (the name is dynamic). When Eden is full, execute the minor GC to clean up the extinct objects, and the surviving objects will be copied to the from zone in Survivor, emptying Eden. When the from zone is full, the surviving objects are copied to the to area, the from area is emptied, and the original from area becomes the to zone, and the original to zone becomes the from zone, so that the to area is always empty. When the from zone is full and no objects can be cleaned or the from-to zone is switched more often than set (hotspot defaults to 15, through-xx:maxtenuringthreashold control), the surviving objects enter the old age. The proportions of Eden and Servivor in the young generation are configured by the-xx:serivorration parameter, which defaults to 8, which is also said to be eden:from:to=8:1:1. The recovery method for young generations is called minor GC, also called stop-copy cleanup. When this method is recycled, it is necessary to suspend execution of all other threads, resulting in low efficiency, now optimized, but only to shorten the stop time and not completely cancel the stop.
Older generation : Older generation of large space, when the old generation of memory is insufficient, will be executed Major GC is also called full GC. If the object is larger, it may be assigned directly to the old age without passing through the younger generation. Use-xx:pertenuresizethreashold to set this value, and objects larger than this will be allocated directly to the old age.
3. Garbage collector
In the GC mechanism, the garbage collector works. The garbage collector used in the HotSpot1.6 is as follows (there is a link to indicate that there is a connection):
Serial collector: The New Generation (young generation) collector, using a stop-copy algorithm, uses one thread for GC, and other worker threads to pause.
Parnew: New Generation collector, use stop-copy algorithm, serial multi-threaded version of the collector, use multiple threads for GC, other worker threads to pause, focus on shortening garbage collection time.
Parallel Scavenge Collector: A new generation collector that uses the stop-copy algorithm to focus on CPU throughput, which is the time/total time to run user code.
Serial Old collector: older generation collectors, single-threaded collectors, using the tagging-grooming algorithm (the sorting method includes sweep cleanup and compact compression , Mark-Clean is the object that needs to be recycled first, An object that is clearly marked after the mark is completed, so that free memory is not contiguous after the cleanup, and tag-compression marks the object that needs to be reclaimed, moves the surviving object to one end, and then cleans the memory outside the end boundary directly, so that the idle memory is contiguous after the cleanup.
Parallel Old collector: older collectors, multi-threaded collectors, using the tagging-grooming algorithm (collation methods include summary rollup and compact compression , tag-compression as with serial old, Mark-Summarize is to copy the surviving objects to the pre-prepared area and then clean up the previous objects.
CMS (Concurrent Mark Sweep) Collector: Old generation collectors, multi-threaded collectors, attention to the shortest payback time pauses, using the tag-purge algorithm, the user thread can work concurrently with the GC thread.
G1 Collector: Published in JDK1.7, used less, not introduced.
The Java GC is a very complex mechanism, and it takes a lot of time to make it clear that he is wrong.
memory allocation and recovery mechanism of Java Virtual machine