Java GC to four references

Common garbage collection algorithm

  1. Copy (Copying) algorithm, I mentioned earlier the new generation GC, which are mostly based replication algorithm to copy the object to be alive to the area, the copy process will be placed in the order of objects, you can avoid memory fragmentation. The cost of doing so is, as we have to be copied, either in advance to reserve memory space, there is a certain waste; region of the addition, which was carved out as a large number of G1 region of GC, copied, not moved, means GC maintenance needs reference relationships between objects, this overhead is not small, either memory usage or time overhead.
  2. Mark - Clear (Mark-Sweep) algorithm, first marking work, identify all objects to be recovered, and then cleared. In addition to marking to do so, the limited efficiency of the cleaning process, the other is inevitable fragmentation problems arise, which led it does not fit particularly large heap; otherwise, Full GC occurs once, pause time may simply unacceptable.
  3. Mark - finishing (Mark-Compact), similar to the mark - sweep, but in order to avoid memory fragmentation, it will move in the process of cleaning up the object in order to ensure that the objects occupy contiguous memory space after the move.

GC

Serial GC

The oldest garbage collector, "Serial" is reflected in its collection is single-threaded, and the garbage collection process is performed, it will enter the infamous "Stop-The-World" state (ie, when the garbage collection will stop run) entire program. Of course, it also means that a single-threaded design to achieve a streamlined GC, maintenance complex data structures, initialization is simple, it has been the JVM is the default option under Client mode. From the perspective of age, which is usually implemented separately referred Year Old Serial Old, it uses a tag - finishing (Mark-Compact) algorithm, different from the new generation copy algorithm. Serial GC parameters corresponding to the JVM are:-XX:+UseSerialGC

ParNew GC

GC implement the new generation, it is actually a multithreaded version of the Serial GC, the most common scenario is in line with the old CMS GC's work, the following is the corresponding parameter-XX:+UseConcMarkSweepGC -XX:+UseParNewGC

CMS(Concurrent Mark Sweep) GC

Based mark - sweep (Mark-Sweep) algorithm, the design goal is to minimize the pause time, which is very important for the Web such as reaction time-sensitive applications, until today, there are still many systems use CMS GC. However, the use of CMS mark - sweep algorithm, there is memory fragmentation problems, it is difficult to avoid the full GC occurred in the case of long-running, leading to poor standstill. In addition, since the emphasis on the concurrent (Concurrent), CMS uses more CPU resources, and user threads and competition.

  • Clear labeling algorithmic process:
    1. Initial labels (CMS-initial-mark): Roots numerals refer to an object directly
    2. Concurrent mark (CMS-concurrent-mark): perform GC Root Tracing
    3. Changes during the amendment because the user program is running concurrent mark caused by: re-mark (CMS-remark)
    4. Concurrent Clear (CMS-concurrent-sweep): cleanup work

Parrallel GC

In earlier versions JDK 8 and the like, which is the default mode JVM GC selected server, also called GC certain priority. Serial GC and its algorithm is quite similar, although much more complex to achieve, which is characterized by the new generation and the old GC's are carried out in parallel, in common server environments more efficiently. Options are open:-XX:+UseParallelGC

  • Further, the introduction of Parallel GC developer-friendly configuration items, we can set the dwell time or the like directly throughput targets, the JVM will automatically adaptively adjusted, for example, the following parameters: -XX:MaxGCPauseMillis=valuewhere GC and user time ratio = 1 / (N + 1)-XX:GCTimeRatio=N

G1 GC

This is a balanced GC throughput and pause time of realization, is Oracle JDK 9 after the default GC options. G1 can visually set the target pause time, compared to the CMS GC, G1 may not be able to do CMS standstill in the delay in the best case, worst case but is much better.

  • G1 GC's concept still exists, but it is not a simple memory structure striped division, but similar to the board of one region. Region is between replication algorithm, but on the whole can be seen as the actual mark - finishing (Mark-Compact) algorithm, can effectively avoid memory fragmentation, especially when the Java heap is very large, G1 of the more obvious advantages.
Dwell time predictable model

G1 able to establish a predictable pause time model because it can be planned to avoid garbage collection in the whole area of ​​the entire Java heap. G1 track individual Region inside the garbage accumulation value size (space recovery obtained and experience required for recovery time), in the background to maintain a priority list, according to each collection time allowed, the maximum recovery value of priority Region) . This use of memory space and Region division have priority area recovery methods to ensure the G1 collectors can obtain the highest possible collection efficiency in a limited time.

Collection process
  1. The initial mark (Initial Marking), marking the initial stage just mark what objects can be linked directly to the GC Roots, which need to pause the thread stage, but it takes very short.
  2. Concurrent mark (Concurrent Marking), concurrent mark phase is the beginning of the heap objects reachability analysis from GC Root, find live objects, this stage takes a long time, but may be complicated by the user program execution.
  3. The final mark (Final Marking) marks the final stage is to correct the record mark in that part of the program because the user continues to operate during the concurrent mark which led to marked changes produced, this stage requires pause threads, but can be executed in parallel.
  4. Filter Recycling (Live Data Counting and Evacuation) screening recovery stage first on the recovery value and cost of each Region to sort, to develop recycling programs according to user desired GC pause time, so can guarantee garbage collection, but also ensures pause time, but not too much lower throughput.
  • G1 throughput and pause performance is very good, and still continue to improve, and CMS has been marked as deprecated in JDK 9 in.

Finally, a brief consolidation

  1. Serial Collector: serial operation; applied to the new generation; copy algorithm; response speed priority; client mode suitable for a single CPU environment.
  2. ParNew Collector: run in parallel; applied to the new generation; copy algorithm; response speed priority; CMS used in conjunction with the multi-CPU environment Server mode.
  3. Parallel Scavenge collector: run in parallel; acting on the new generation; replication algorithm; throughput priority; suitable for background operation without the need for too many scenes interaction.
  4. Serial Old collectors: Serial run; the role of the old year; mark - Collation Algorithm; response speed priority; Client mode single-CPU environment.
  5. Parallel Old Collector: run in parallel; the role of the old year; mark - sort algorithm; throughput priority; suitable for background operation without the need for too many scenes interaction.
  6. CMS collector: run concurrently; the role of the old year; mark - sweep algorithm; response speed priority; applies to the Internet or B / S business.
  7. G1 Collector: run concurrently; or may act on the new generation of the old year; mark - Collation Algorithm + copy algorithm; response speed priority; for server applications.

Quote

Talked about in front of the garbage collection process GC needs to find Roots, and then follow it to find and Root objects have their own association, then filter garbage, then the GC is how to find these objects can survive it?

First, in java, as the object GC Roots are:

  1. Virtual Machine stack (Local Variable Table stack frame) in the object reference;
  2. Method region static property class object reference;
  3. A method of object reference constant region;
  4. Native method stacks in the JNI (i.e., the general said method Native) referenced objects

Different reference types, mainly reflected a different object reachability status and the impact on garbage collection.

  1. The so-called 强引用, is our most common ordinary object reference, as long as there is a strong reference point to an object, you can also indicate that the object "alive", the garbage collector will not touch such objects. For an ordinary object, if no other references to the relationship, as long as more than a reference to the scope or explicitly appropriate (strong) references assigned to null, that can be collected garbage, of course, will depend on the timing of specific recycling garbage collection policy.
  2. 软引用, Is a relatively strong references cited some weakening, so that the object can exempt some garbage collected only if JVM deemed insufficient memory, only to attempt to recover the soft reference object pointing. JVM will ensure that before throwing OutOfMemoryError, clean up the soft reference object pointing. Soft references are usually used to implement memory-sensitive cache, if there is free memory, you can temporarily retain the cache, clean out when memory is low, thus ensuring at the same time using the cache will not run out of memory.
  3. 弱引用Garbage collection does not make exemption subject only to provide a reference under access to state of the object in a weak way. This can be used to build a relationship of no specific constraints, for example, to maintain a non-mandatory mappings if the objects are still trying to acquire, use it, otherwise reproduce instantiated. It is also a lot of cache implementation options.
  4. For 幻象引用, sometimes translated into phantom reference, you can not access it through the object. Phantom references merely provide an object is to ensure that after finalize, do certain things mechanisms, such as, generally used for the so-called Post-Mortem cleanup mechanism, as I described in the Java platform itself Cleaner mechanisms such as speaking on the column , it was also cited the use of phantom creation and destruction of the monitored object.

Up state - Reachable

  1. 强可达, Is that when an object can have one or more threads can access to a variety of circumstances not by reference. For example, we create a new object, then it is the thread that created it up to strength.
  2. 软可达It is when we can only refer to access to the object through a soft state.
  3. 弱可达Similar mentioned earlier, is not strong or soft references cited by the visit, only through the state's weak reference access. This is very close to finalize the timing of the state, when weak references are cleared when you finalize the conditions of the accord.
  4. 幻象可达, The above Scheme has been very straightforward, is not strong, soft, weak references association, and finalize too, only illusion references to the object of the time.
  5. There is also a final state, that is 不可达, means that the object can be cleared.

Why garbage collection to once again before garbage 最终标记

  1. In addition 幻象引用(as always get return null), if the object has not been destroyed, you can get the original object through the get method. This means that the use of 软引用and 弱引用that we can have access to the object, redirected 强引用, that is man-made changes in the reachability state of the object!
  2. So, for 软引用, 弱引用like, a garbage collector might confirm the existence of secondary issues in order to ensure that the object is in a state of weak references, no change is 强引用.
  3. If we are wrong maintained 强引用(for example, assigned to the static variable), then the object may not have the opportunity to change back to a similar 弱引用reachability state, it will generate a memory leak. So, check 弱引用point whether the object is garbage collected, the idea is to diagnose whether there is a specific memory leak, if we use the framework to weak references and suspect a memory leak, you can examine from this point of view.
Reference: 36 Q core Java technology

Guess you like

Origin juejin.im/post/5d6a0674f265da03b574618c