[Depth understanding of java virtual machine] Chapter 3 garbage collection and memory allocation strategy

Personal blog article link

3.1 Overview

3.2 determine whether the object is alive

3.2.1 reference counting algorithm

  • Add a reference to an object counter, whenever a reference to its place, a counter is incremented when there are references fail, a counter is decreased at any time counter for the object 0 it is impossible recovered.
  • Mainstream java virtual machine is not the choice of the algorithm, the most important reason is difficult to solve the problem of circulation between object references.

3.2.2 reachability analysis algorithm

  • Through a series of "GC Roots" object as a starting point, the search starts from the origin downward, when an object can not be "GC Roots" to search for an object, the object will be determined recyclable objects.
  • It can be used as the target GC Roots
    1. Virtual Machine stack (Local Variable Table stack frame) in the object reference.
    2. Method static property class object referenced area.
    3. Object literal reference methods zone.
    4. Native method stacks in the JNI (i.e., the general said method Native) object reference.

3.3 garbage collection algorithm

3.3.1 mark - sweep algorithm

  • Divided into "mark" and "clear" in two stages, first mark all objects need to be recovered, unified all objects marked recovery after labeling is complete.
  • Cons: One is the efficiency, marking and removal efficiency of the two processes is not high; the other will produce a large number of discrete memory fragmentation After labeling cleared.

3.3.2 replication algorithm

  • The available memory is divided into two equal size, wherein each except one, a copy when it will run out of surviving further object onto another one, and then dropped to a clean up memory space used.
  • This algorithm wasted half the space. But there are solutions, can be optimized by generational. Suitable for the new generation.

3.3.3 Mark - Collation Algorithm

  • All live objects are moved to the end, then clean out the memory directly outside the terminal boundary.
  • Applicable to years old

3.3.4 generational collection algorithm

  • The java heap into the new generation and the old era, the most appropriate algorithm according to the characteristics of each collection's adoption.
  • Large number of objects to die in the new generation, a small amount of live objects, select Copy algorithm. The object's old high survival rate, there is no guarantee extra space, you must use the "mark - sweep" or "mark - to clean up."

3.5 garbage collector

3.5.1 Serial Collector

  • Single-threaded collectors, garbage collection is performed, it is necessary to suspend the work of other threads until the end of the collection.
  • Apply to virtual machines in Client mode.

3.5.2 ParNew collector

  • In fact, multi-threaded version of the Serial collector.
  • Used in the new generation of collectors virtual machine running in Server mode.

Note: concurrent collector and parallel collector

  • Parallel (Parallel): refers to the number of threads in parallel garbage collection, but this time the user thread in a wait state.
  • Concurrency (Concurrent): refers to the user thread and garbage collection thread simultaneously.

3.5.3 Parallel scavenge collector

  • Using multi-threaded replication algorithm collectors.
  • It is characterized by different concerns and other collectors, and other concerns CMS collector is to shorten the pause time garbage collector thread of the user as much as possible, Parallel Scavenge collector's goal is to achieve a controlled throughput.

Throughput is the ratio of time used to run user code CPU CPU total time consumed

3.5.4 Serial Old collectors

  • Is Serial old collector's edition, the same is a single-threaded collector, use the "mark - finishing" algorithm.
  • To the virtual machine in the Client mode.

3.5.5 Parallel Old collectors

  • It is the Parallel Scavenge collector's version of the old, the use of multi-threading and "mark - finishing" algorithm.
  • Paralle Scavenge collector may be blended composition.

3.5.6 CMS collector

  • It is a pause in order to obtain the shortest recovery time objectives collector.
  • Server architecture suitable for Internet sites or B / S system.
  • Implementation process:
    1. Initial labels: GC Roots mark object can be directly linked to the very fast.
    2. Concurrent mark: search, mark recyclable objects.
    3. Relabeled: User program continues to operate because of the correction period which led to the concurrent mark mark mark produced record that part of the object changes.
    4. Concurrent Clear: Clear recyclable objects.
      Wherein the initial marking and re-marking needs to stop a user process.

3.5.7 G1 collector

  • Have the characteristics: parallel and concurrent, generational collection, Spatial Integration, predictable pause.
  • Operation of the process: the initial mark, concurrent mark, the final mark, filter recycling.

3.6 memory allocation and recovery strategies

3.6.1 Objects priority allocation in Eden

  • In most cases, the new generation of object allocation in Eden area. When the Eden area is not enough space to be allocated, the virtual machine will launch a Minor GC.

Note Minor GC and Full GC

  1. New Generation GC (Minor GC): refers to the place in the new generation garbage collection action.
  2. Old's GC (Major GC / Full GC): GC refers to occur in old age, the emergence of Major GC, often accompanied by at least one Minor GC (not absolute). Major GC generally slower than the speed of more than 10 times Minor GC.

3.6.2 large objects directly into the old year

  • The so-called large objects are, need a lot of java object contiguous memory space, typically long strings and arrays.
  • The reason is generally believed that the higher survival rate of large objects, and if large objects in the Eden area, using the copy algorithm, consumption is relatively large, but the benefits are not large.

3.6.3 Long-term survival will go on like years old

  • The subject has experienced many times Minor GC still alive promoted to the old era, how many times can the parameters -XX: to set MaxTenuringThreshold.

3.6.4 Dynamic target age determination

  • In order to better adapt to the situation of different programs memory, the virtual machine is not always required to reach the target age must MaxTenuringThresold years before promotion is not old, if Survivor space in the same age of all objects greater than the sum of the size of the space of half Survivor, age greater than or equal to the age of an object you can go directly to the old era.

3.6.5 space allocation guarantees

  • Minor GC is to check whether it is safe to perform if, because of the use of replication algorithm, Survivor region may not be able to hold objects survive, need to borrow memory of old age, so it is necessary to detect memory of old age meets the requirements.
  • process:
    1. Before Minor GC, it will check whether old's maximum available contiguous space will be greater than the new generation of the total space of all objects, if the establishment of this Minor GC is safe.
    2. Otherwise, the virtual opportunity to review whether to allow the security settings HandlePromotionFailure fail, if allowed to continue will check whether old's largest contiguous space will be larger than previous years old promoted to the average size of the object, if more than the attempt to conduct a Minor GC.
    3. If less than, or settings do not allow the HandlePromotionFailure adventure, then conduct a Full GC.
Published 84 original articles · won praise 50 · views 7013

Guess you like

Origin blog.csdn.net/qq_43115606/article/details/104055239