Understand java garbage collection

Java garbage collection algorithm

  1. Clear labeling algorithm ----
  2. Replication algorithm
  3. Mark ---- Collation Algorithm
  4. Generational collection algorithm

Why garbage collection

  Because when an object reference unreachable, or an object has no references pointing to it, then it is not necessary to continue to exist in memory, and this time it is in objects can be (garbage collector) recovered GC, jvm virtual machine dynamics the collection object is not available to achieve the purpose of freeing memory.

Garbage collection area

     jvm virtual machines under normal circumstances the memory is divided into the following pieces

  1. VM stack (life cycle and thread the same time each thread of execution will create a stack frame for storing local variable table, the operand stack, dynamic links, import and export methods and other information, from each method call to complete the implementation process , a stack frame corresponds to the process to push the stack in a virtual machine, the local variable table 8 stores the basic data types and the large object. throws an OutOfMemoryError)
  2. Native method stacks (mainly virtual machine to use the native method for service, and will throw OutOfMemoryError StackOverflowError error)
  3. The method area (common threads, runtime constant pool area also belongs to the method for storing information class is loaded virtual machine, constants, static variables, the time compiler to compile the code and other data, the alias for the Non-Heap, also GC expansion of generational collection.)
  4. Heap (GC heap, store new objects out, the biggest piece of Java memory management)
  5. The program counter (thread private, will throw OutOfMemoryError)

Wherein the virtual machine stack and in certain native method stacks virtual machine (e.g. the sun HotSpot VM) is referred to as a virtual machine stack, and the virtual machine program counter stack and are private to the thread.

Four kinds of garbage collection algorithms introduced

  1. Clear labeling ---- algorithms: mark and sweep is divided into two stages, first mark all objects need to be recovered, recycled unified object is marked in the mark after completion. This algorithm has two deficiencies, one is efficiency, marking and clearing two process efficiency is not high, the other is the space issue, will generate a lot of discrete memory fragmentation mark after clearing space debris might be too much when the program is running in the process leading to the future need to allocate large objects, can not find enough contiguous memory and had to trigger another garbage collection operation in advance.
  2. Replication algorithm: In order to solve the efficiency problem, there has been copied algorithm that the memory is divided into two equal size, each with just one of them. When this one runs out of memory, and it will also copy objects to survive on top of another piece, then that has been used to clean out the entire block of memory, so that every time you clean the area only half the memory, memory allocation would not have considered memory fragmentation complex situation, as long as the movement of the top of the stack pointer in order to allocate memory, simple, and efficient operation, but the drawback is, the memory is reduced by half, the cost of a little high. Current commercial virtual machines have this new generation collection algorithm to recover, but the memory is not a 1: 1 ratio of such a division, but the memory of Eden into a larger space and two smaller Survivor space, each Eden-use only and where a Survivor areas, when recovered, copy and survivor Eden area also survived Survivor object to another area, and finally clean out Survivor Eden space.
  3. Mark ---- Collation Algorithm: Clear labeling and similar algorithms, but the subsequent steps are not directly recycled objects to clean up, but to live objects are moving towards a period, and then clean up directly to memory beyond the boundary.
  4. Generational collection algorithm: the current garbage collection business virtual machine implements a generational collection algorithm, depending on the subject alive the memory cycle is divided into a few pieces, usually the heap is divided into the old and the new generation's, so you can characteristics of each era using the most appropriate collection algorithm. In the new generation, have found a large number of objects every garbage died, only a few survive on the selection replication algorithm, only need to pay the cost of reproduction of a small amount of live objects to complete the collection, while years old objects in the survival rate is high, it is necessary to collect when use markers or markers ---- ---- cleanup collation algorithm for recycling.

Guess you like

Origin www.cnblogs.com/yjp372928571/p/11117680.html