Overview
Because the garbage collector exists in the JVM, the Java programmer can not care about the memory allocation and the release process when the object is created, and when the memory is low, the JVM automatically turns on the garbage collection thread and recycles the garbage objects.
So when does the garbage collection thread trigger, and how does it implement garbage collection? This article will analyze the source code of OPENJDK and analyze the process of Java garbage collection.
Vmthread
Vmthread is primarily responsible for scheduling VM thread operations inside virtual machines, such as GC operations, to initialize when the JVM instance is created.
Vmthread::create ()
The Vmthread::create () method is responsible for creating the thread.
There are two main things to do in the Create method:
- Vmthread internally maintains a vmoperationqueue-type queue that holds the VM thread action vm_operation for the internal commit and initializes the queue when Vmthread is created.
- Since the vmthread itself is a thread, it starts by executing the loop method for polling, and takes the priority out of the queue and executes the Vm_operation object that is currently required to execute.
The entire off-the-shelf polling process is divided into two steps:
The first step
If the queue is empty, the _vm_queue->remove_next () method returns an empty _cur_vm_operation, otherwise it is reordered based on the vm_operation priority in the queue and returns the vm_operation of the queue header. If _cur_vm_operation is empty, the following logic is executed:
Wait for the VM to operation by executing the Vmoperationqueue_lock->wait method.
Step Two
If the current vm_operation needs to be performed at a secure point, such as full GC, perform the above logic, otherwise execute the following logic:
The Evaluate method of the Vm_operation object is eventually called by executing the current _cur_vm_operation through Evaluate_operation.
Subclasses implement concrete logic by overriding the doit method of the Vm_operation class.
Java GC Triggering
In the memory allocation mechanism of Java, when the Cenozoic is insufficient to allocate the memory required by the object, it will trigger a YGC, which is implemented as follows:
The above code means to create a vm_gencollectforallocation type of vm_operation, which is saved to the Vmthread queue by executing the Vmthread::execute method. The core implementation of execute is as follows:
YGC Vm_operation joins the queue, wakes the Vmthread thread by executing the Vmoperationqueue_lock notify method, waiting to be executed, where vm_ Gencollectforallocation's Doit method implementation:
The GC operation is performed by Vmthread Dispatch, and the corresponding doit method is eventually called:
1, the use of Svcgcmarker notice minor GC operation of the beginning;
2, the reason for triggering GC is gccause::_allocation_failure, that is, memory allocation failure;
3, wherein the Gencollectedheap Satisfy_failed_allocation method will invoke the GC policy Satisfy_failed_allocation method, processing memory allocation failure;
satisfy_failed_allocation
If another thread triggers a GC operation, it is allocated by expanding the capacity of the memory generation, and finally returns regardless of whether or not the allocation succeeds, waiting for the GC operation of the other thread to end;
If the incremental Gcincremental collection is feasible, the Do_collection method is used to perform a minor GC, which recycles the new generation of garbage.
If an incremental GC is not available, the full GC is executed once through the Do_collection method.
After the GC finishes, the memory block of the memory heap is allocated again from each memory generation to the specified size, and if the assignment succeeds, it is returned, otherwise it continues.
If the GC fails after the allocation fails, indicating that the GC has failed, try to allocate a memory block of the specified size again by allowing the extended memory generation capacity.
If this step is performed, after the GC is still out of memory, the Do_collection method is followed by a thorough GC, which reclaims all memory generations, compresses the heap memory, and clears the soft reference.
After a thorough GC, the last attempt is to allocate a memory block of the specified size from each memory generation in turn.
Note : From the above analysis, it can be found that the entrance to the GC operation is located in Gencollectedheap::d The O_collection method, different parameters perform different types of GC.
Do_collection implementation
There are four conditions that must be met to perform a GC operation:
1, in a synchronous security point, Vmthread will invoke the GC operation through the Safepointsynchronize::begin/end method to enter and exit the security zone, call the Begin method will force all threads to a safe point;
2. The current thread is a VM thread or a concurrent GC thread;
3, the current thread has obtained the memory heap global lock;
4, the memory heap current _is_gc_active parameter is False, that is, the GC has not started;
If the GC is currently triggered by another thread, terminate the current GC thread, or continue.
According to the parameters Do_clear_all_soft_refs and GC policy to determine whether this GC needs to clear the soft reference, record the current permanent generation of the use of perm_prev_used; if-XX:+PRINTHEAPATGC is set in the startup parameter, Prints information about the memory heap when the GC occurs.
1, set the parameter _is_gc_active to True, indicating that the current thread formally started the GC operation;
2. Determine if a full GC is currently being performed and determine the cause of the full GC, such as triggering by calling System.GC ();
3. If PRINTGC and Printgcdatestamps are set, the timestamp is added to the output log;
4. If Printgcdetails is set, the detailed CPU time of this GC is printed, such as User_time, System_time and Real_time;
5. The Gc_prologue method does some pre-processing before the GC starts, such as setting the _soft_end field for each memory generation;
6, update the number of GC occurrences _total_collections, if the current GC is full GC, you also need to update the number of full GC _total_full_collections;
Gets the usage of the current memory heap gch_prev_used; initializes the memory generation sequence that starts the collection starting_level, which defaults to 0, starting with the youngest memory generation, and starting with the oldest memory generation, if the current GC is full GC. Find the first to collect all the new generation of memory generation, and later from the memory generation to start recycling;
The memory generation starting from the serial number is starting_level, and if the current memory generation does not need to be recycled, the next memory generation is processed, otherwise the current memory is recycled, and if the current memory generation is the oldest of all memory generations, the GC process is upgraded to full GC, updating the GC, and performs the pre-processing of the full GC.
1, if the parameter HEAPDUMPBEFOREFULLGC is set, the memory heap is dump;
2. If the parameter PRINTCLASSHISTOGRAMBEFOREFULLGC is set, the object before FGC is printed;
1, statistics of each memory generation GC data;
2. If the Zapunusedheaparea is turned on, the memory upper address top of memory generation should be updated when each memory generation is recycled;
This is the first step to start a real GC operation: Set the _saved_mark value of the current memory generation, that is, set the upper address of these memory area blocks, and the garbage collection algorithm is collected by the Collect method of each memory manager, and the details of the garbage collecting algorithms will be analyzed later.
1, if the current is FGC, then call the Post_full_gc_dump method to notify the GC has completed, you can follow up, if the parameter HEAPDUMPAFTERFULLGC is set, the heap memory can be dump after the GC If the parameter PRINTCLASSHISTOGRAMAFTERFULLGC is set, the surviving object can be printed after the GC;
2. If the parameter printgcdetails is set, the change of the memory heap can be printed after the GC, and if it is still FGC, the memory change of the permanent generation can also be printed.
After GC completes, adjusts the memory heap the memory generation the size, if is FGC, then also needs to adjust the permanent generation size, obtains the Fullgccount_lock lock, updates the _full_collections_completed, and notifies this FGC through the lock mechanism to have completed;
The total number of GC and FGC times of the printed memory heap; Exitaftergcnum default is 0, if the setting Exitaftergcnum is greater than 0, and the total number of GCs exceeds Exitaftergcnum, the entire JVM process is terminated. This Java JVM garbage collection process terminates the GC process.
Deep understanding of Java garbage Collection