Java Virtual Machine study notes (b) - garbage collection and memory allocation

Disclaimer: This article is a blogger original, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/weixin_36904568/article/details/90263946

A: memory recall

1. What memory need to recycle?

  • Native method stacks, Java method stack, program counter: memory allocation and recovery with certainty, at the end or the end of the thread method, automatic recovery of memory
  • Java heap, the method area: memory with dynamic allocation and recovery, it is necessary to reclaim memory GC

(1) Java heap unusable objects

Determine whether the survival of the subject standard - references

If the reference value represents the type of data stored is the starting address a further memory, this memory is representative of a reference

Reference types Reference objects Feature
Strong references Necessary objects As long as there is still a strong reference object were references will not be recovered
Soft references (SoftReference) Useful objects are not necessary Before the system memory overflow exception will occur, these objects will be recycled into the column range, secondary recovery
Weak references (WeakReference) Objects not necessary When working in the recovery time garbage collector
Virtual reference (the PhantomReference) No effect on survival time, can not get instance, is to get the system notification when an object is recovered
Algorithm to determine whether the object is alive
Reference counting algorithm

Add a reference to an object counter, whenever a reference place, the counter is incremented; failure when reference counter is decremented by 1; when the counter is 0, the object is not available

Disadvantages: each cycle between the object reference problem can not be solved

Root search algorithm

Through a series of GC Roots object named as a starting point, the search starts from the nodes down through the chain path for reference, if there is no link between a reference object and the GC Roots, it is proved that the object is not available

GC Roots:

  • VM stack references (local variable table stack frame) objects
  • Method static property class object referenced by region
  • The method of constant reference object region
  • JNI native method stack objects referenced

Secondary marker object

  • Marked the first time: Object found no references to the chain connected to the GCRoots after root search, prepare to die
    • There is no need to perform the finalize (): Object does not cover finalize (), virtual machines have been performed finalize (). (Object death, finalize only once)
    • It is necessary to perform the finalize (): F-Queue object is placed in the queue, low priority thread Finalizer execution (object also resurrected chance) automatically created by the virtual machine
  • Second mark: F-Queue objects in the queue, can not be () method to re-establish relations with objects on the chain referred to by finalize, the object of death

(2) method and the constant region without waste classes used

Constant determine whether the survival of the standard - references

There are objects or whether the current method references a constant in the constant pool, methods, and other symbolic references

Class to determine whether it is useful
  • All instances of the class have been recovered
  • Class loader to load the class has been recovered
  • Class corresponding to the class object is not referenced, the class can not be accessed by the reflection method

2. how to reclaim memory?

(1) mark - sweep algorithm

First of all marked objects need to be recovered, recycled object in uniform after completion mark

Feature
  • low efficiency
  • A large number of discrete space debris

(2) Copy the algorithm (the new generation)

Available memory capacity is divided by two of equal size, which uses only one. Copy will run out of available memory subject to another block of memory, and then remove the used memory

Feature
  • No memory fragmentation, allocate memory only according to the order
  • The cost of large
Better Business Virtual Machine

The memory is divided into a relatively large space of Eden and two Survivor relatively small space, each time using a Survivor Eden space and a space. When the recovery of all the objects available copy once a Survivor space to another, and then remove the used memory.
If Survivor is not enough memory space, you need to rely on old's memory for allocation guarantees

(3) mark - Collation Algorithm (years old)

First of all marked objects need to be recovered, after labeling the completion of all live objects move toward one end, and then clean up the object outside the off-side boundary

(4) generational collection algorithm

According to the object of the survival period objects into pieces, according to the characteristics of each era select the appropriate algorithm

  • New Year: Only a small amount of live objects, use the copy algorithm
  • Old year: high survival rate of the object, using a mark - finishing algorithm or mark - sweep algorithm

3. The garbage collector

Concurrent: collecting user thread execution threads (could be in between)
parallel: a user thread to wait to collect a plurality of threads operating in parallel

(1) Serial collector

The most basic and oldest collector, is a virtual machine running in Client mode default collector

Feature
  • Is single-threaded, garbage collection when it is necessary to suspend the work of all the other threads
  • Simple and efficient, no thread interactions in a single CPU overhead environment

(2) ParNew collector

Multi-threaded version of the Serial collectors, but also the virtual machine of choice for the new generation of collectors run in Server Mode

Feature
  • Using multi-threaded garbage collection
  • In addition to Serial collector, the only cooperation with CMS collector
  • In the multi-CPU environment, the same number of threads enabled by default and the number of CPU (by -XX: ParallelGCThreads parameter limits the number of threads)

(3) Parallel Scavenge collector

Parallel, multi-threaded collector, collectors also use the new generation of replication algorithm

Feature
  • The purpose is to control the throughput for mission operations background, does not require too much interaction in
    • -XX: MaxGCPauseMills: set the maximum pause time garbage collection
    • -XX: GCTimeRatio: the ratio of the total garbage collection time period is the inverse of throughput
    • -XX: UseAdaptiveSizePolicy: GC adaptive strategy

(4) Serial Old collectors

Old's version of the Serial collectors, but also the use of mark - a single-threaded sorting algorithms collector.

Feature
  • The main is a virtual machine used in Client mode
  • If you are in Server mode, can be used with Parallel Scavenge collector, or as a back-up plan for CMS collector in the use of Concurrent Mode Failure concurrent collection happen

(5) Parallel Old collectors

Old's version of the Parallel Scavenge collector, is the use of mark - multithreading collector finishing algorithm.

Feature
  • Focus on throughput and CPU resources sensitive applications, regardless of the combination (Parallel Scavenge collector + Serial Old collector) under the previous single-threaded, multi-threaded but priority combination (Parallel Scavenge collector + Parallel Old collectors)

(6) CMS (concurrent low pause) collector

In a pause obtain the shortest recovery time objectives collector, based on mark - sweep collector algorithm

process

Overall, the recovery process with the CMS user concurrently executing threads

  • Initial labels: GC Roots mark object can be directly associated
  • Concurrent mark: mark secondary root search algorithm
  • Relabeled: amendment because the user program continues to operate lead to changes in the object tag identifies the record
  • Concurrent Clear: Clear marked objects
Feature
  • Focus pause time garbage collection, suitable for operation in the foreground, the task requires interaction
  • Very sensitive to CPU resources: CPU resources may occupy part of the procedure leading to slower, lower the total throughput
    • Incremental concurrent collector I-CMS: in concurrent mark and concurrent cleanup time, GC threads and user programs run alternately, reduce the time GC occupied resources
  • Can not handle floating garbage: new concurrent garbage generated in the cleanup phase, CMS can not wait longer to collect garbage generated almost, because of the need to reserve memory space for the user program is running.
    • If the reserve is not enough space to run user programs, may appear Concurrent Mode Failure, leading to another generation of Full GC (start backup plan Serial Old collectors)
    • By -XX: CMSInitiatingOccupancyFraction, set up after old's space is used to start collecting N%
  • A large amount of space debris
    • By -XX: + UseCMSCompactAtFullCollection, defragment (increase dwell time)
    • By -XX: CMSFullGCsBeforeCompaction, after setting the recovery N times defragment

(7) G1 collector

The most cutting-edge collector, based on the label - a collector of sorting algorithms

Feature
  • There is no space debris
  • Precise control pause
  • Lower standstill complete recovery without sacrificing throughput. The entire Java heap into a plurality of regions, each region recover only the most trash

(8) use of a combination of

command collector
-XX:UseSerialGC Use Serial Collector
-XX:UseParNewGC Use ParNew + Serial Old collectors
-XX:UseConcMarkSweepGC Use ParNew + Serial Old + CMS collector
-XX:UseParallelGC Use Parallel Scavenge + Serial Old collectors
-XX:UseParallelOldGC Use Parallel Scavenge + Parallel Old collectors

Two: memory allocation

1. distribution

(1) small objects

Small objects priority in the allocation of the new generation Eden District, Eden if there is not enough space, the virtual machine to initiate Minor GC

  • Survivor recyclable objects into the area, and clear
  • Recyclable objects into old's memory area, and clear

(2) large object

The need for a large amount of contiguous space Java objects (string or array of objects), directly into the memory's old district, Eden area and avoid a lot of copy work zone Survivor

  • For Serial ParNew collector and the collector, provided -XX: PretenureSizeThreshold parameters
  • For Parallel Scanvenge collector need not be provided

(3) long-term survival target

Each object has a target age Age counter reaches a certain age to enter old's memory area

  • Eden region born alive after Minor GC, area and moved to Survivor, initializing Age = 1
  • Each time through the Minor GC and survived, Age ++
  • When Age = 15 when entering the old year (by -XX: MaxTenuringThreshold setting age)

(4) dynamic objects

Survivor space the size of the sum of all the objects in the same age> Survivor half space, then aged ≥ to enter the age-old subject's memory area

2. Space allocation guarantees

Occurs Minor GC, the average size of the virtual machine detects the promotion of old age old age is greater than the remaining space

  • Greater than: direct Full GC
  • Less than: View HandlePromotionFailure is set up to allow the security failure
    • Allow: carried Minor GC (if the security failure, only to re-launch Full GC)
    • Not allowed: Full GC carried out

Guess you like

Origin blog.csdn.net/weixin_36904568/article/details/90263946