GC & understanding of JVM memory allocation

Garbage collection

  • Object survived decision algorithm
  • Garbage collection algorithm
  • HotSpot algorithm and the garbage collector
  • Memory allocation and recovery strategies
    1.  Objects survival determination algorithm (JVM recycle those objects)

  The concept: four kinds of reference types

  •  Strong Quote: lack of timely memory will not be GC, not free to reclaim strong reference to the object; even throws OOM (OutOfMemoryError), will not be free to reclaim strong reference objects
  •  Soft Quote: Only out of memory, will be GC; recovered memory shortage will throw an exception OOM
  •  Weak references: Whether the adequacy of current memory will be GC
  •     False quote: Any time will do GC

  . A reference counting algorithm: add a reference to the object counter, whenever a reference to the place of his time is incremented when there is failure of the reference decrements; any time the counter 0 of the object can not be used

  (But because he could not solve mutual circular reference problem, it is not used by mainstream JVM)

  . b reachability analysis: GC ROOT] to [the object as a starting point, to start the search from the node down, called search path traversed chain of references , when an object to GC ROOT when nobody any reference chain, then the object is not available.

It can be used as an object of GC ROOT

  • Target virtual machine stack references, mainly refers to the local variable stack frame
  • Native method stacks of objects referenced by Native Method
  • Method static property class object referenced by region
  • Method zone constant reference object

Note that: being judged unreachable objects may not really be sentenced to death in the reachability analysis method, at least twice to go through the labeling process, to determine whether the object is necessary to perform a finalize (), if it is determined if necessary, also will be conducting a screening, if the object is to establish a relationship with any object references in the chain, it will be removed "will recover" in the finalize () sets in

 

 

 

 

 

 

    2. garbage collection algorithms (JVM introduce recycling out how these objects)

      a. generational collection algorithm (an algorithm is currently used in commercial virtual machine)

  • Cenozoic: a large number of dead objects, only a few survive. Use "replication algorithm", only a small amount of live objects can be copied
  •    Old year: high survival rate of the object . The use of "mark - cleaning algorithm" or "mark - sorting algorithm", just less marked recovery of the object can be.

      b. Copy the algorithm

  • According to the available memory capacity is divided into two equal, which uses only one. When a copy of which runs out of memory, but also the survival of the object to another block of memory, a memory in these clean out.
  • Pros: each time for memory recovery for the entire half-area, regardless of circumstances such as memory fragmentation. As long as the top of the stack pointer movement, in order to allocate memory, simple, efficient operation
  • Disadvantages: each available memory is reduced to half of the original, low memory usage

Studies have shown that 98% of the new generation of the object is toward evening students dead, it is not necessary in accordance with the 1: 1 to divide the memory space, but the space is divided into a larger piece of Eden and two Survivor smaller space,

In the HotSpot virtual machine default ratio of 8: 1: 1 . Eden and every time you use a Survivor, the recovery of these two objects are alive once copied to another piece of Survivor, do clean up. Visible only 10% of the memory is "waste", if insufficient space Survivor also need to rely on other memory (years old) allocated guarantee.




 

 

 

 

 

       . C Clear labeling law: first mark objects need to be recovered, then unified remove these objects

      Disadvantages: mark, removal efficiency is not high; when the space debris too much, it will produce a large number of discrete space debris, could lead to a greater need to allocate objects in the back, because they can not find a large continuous space in advance trigger another GC, affect performance.

 

      . d mark - Collation Algorithm: First "tag" all the objects need to be recovered, then "Refresh", so that the object of survival are moved to the end, and finally clean out the memory directly outside the terminal boundary .

      Pros: That did not waste 50% of the space, there is not space debris, the higher cost .

      Under normal circumstances, would choose to mark years old - Collation Algorithm.

 

Guess you like

Origin www.cnblogs.com/9797ch/p/12556345.html