Before reading this article, I recommend you an Ali Cloud Double 11 activities, really very very very recommended, for the new welfare, Ali cloud this time is really a big expense, it is suggested that Ali cloud new must certainly must not miss. If you think this is just an advertisement, you can skip the text.

Ali Cloud double 11 latest activity (only new users of Ali cloud can buy, old users can pull new users can get cash back red envelope, the subsequent opportunity to share million red envelope), the discount is very, very, very large, in addition to join the group, there is a chance to share 100W red envelope! At present my clan has 12 new people, now is a discount on the 50% discount is also a discount to buy!! . Highlights: 1 core 2G cloud server only 99.5 yuan for 1 year!! 1 core 2G cloud server only 298.50 yuan for 3 years!! Only 8.2 yuan a month this discount is only for newcomers! This is the address of my group: m.aliyun.com/act/team111… !

Review: “Probably the clearest article on Java memory regions.”

Writing in the front

This section often meet questions:

The answers to these questions are given in the passage

  • How to tell if an object is dead (two ways).
  • This section briefly introduces strong references, soft references, weak references, and virtual references (the differences between virtual references and soft references and weak references, and the benefits of using soft references).
  • How to determine if a constant is discarded
  • How do you tell if a class is useless
  • What are the algorithms for garbage collection and their characteristics?
  • Why HotSpot is divided into new generation and old generation?
  • What are the common garbage collectors?
  • Introduce the CMS,G1 collector.
  • What’s the difference between the Minor AND Full Gc?

Text fuse

These “automated” technologies need to be monitored and tuned as necessary when various memory overflow issues need to be addressed and when garbage collection is a bottleneck for systems to achieve higher concurrency.

Java programmers must see documentation

Ha ha skin once! I open source a Java study guide document myself. A cover most Java programmers need to master the core knowledge, is gradually improved step by step, looking forward to your participation. Github address: github.com/Snailclimb/… . Take a look, I think you will not regret, if possible, you can give a Star encouragement!

Demystify JVM memory allocation and reclamation

Automatic memory management in Java focuses on object memory reclamation and object memory allocation. At the same time, the core function of Java automatic memory management is the allocation and reclamation of objects in heap memory.

Diagram of heap memory before JDK1.8:

As you can see from the figure above, the heap memory is divided into new generation, old generation, and permanent generation. The Cenozoic is further divided into Eden +Survior1 +Survior2 zones. It’s worth noting that in JDK 1.8, the entire permanent generation was removed and replaced with an area called Metaspace (the permanent generation uses the JVM’s heap memory space, whereas the Metaspace uses physical memory and is directly limited by the native physical memory).

1.1 Objects are preferentially allocated in Eden area

The current mainstream garbage collector will use generational collection algorithm, so we need to divide the heap memory into new generation and old generation, so that we can choose the appropriate garbage collection algorithm according to the characteristics of each generation.

In most cases, objects are allocated within the Eden region of the new generation. When the Eden area does not have enough space to allocate, the virtual machine will initiate a Minor GC. Let’s do the actual test below.

Before we test this out, what’s the difference between the Minor AND Full Gc?

  • Minor GC: Refers to a new generation of garbage collections. Minor GC is very frequent and usually fast.
  • Major GC (Full GC) : A GC that occurs in an older era, usually with at least one Minor GC (but not always one), usually 10 times slower than a Minor GC.

Testing:

public class GCTest {

	public static void main(String[] args) {
		byte[] allocation1, allocation2;
		allocation1 = new byte[30900*1024];
		//allocation2 = new byte[900*1024];}}Copy the code

Run by:

Added parameters: -xx :+PrintGCDetails

Running results:

We can see from the figure above that the Eden area memory is almost fully allocated (even if the program does nothing, the new generation will use more than 2000 K of memory). What happens if we also allocate memory to allocation2?

allocation2 = new byte[900*1024];
Copy the code

Here’s a quick explanation of why this is the case: When allocation2 is allocated, the Eden memory is almost allocated. We just said that when Eden space is insufficient, the VIRTUAL machine will initiate a Minor GC. During GC, the virtual machine also finds that Allocation1 cannot store Survior space. So we have to use the allocation guarantee mechanism to move the objects of the new generation to the old age. The old age has enough space to store allocation1, so Full GC will not occur. After a Minor GC, memory is allocated in the Eden region if the later allocated objects can have Eden region. This can be verified by executing the following code:

public class GCTest {

	public static void main(String[] args) {
		byte[] allocation1, allocation2,allocation3,allocation4,allocation5;
		allocation1 = new byte[32000*1024];
		allocation2 = new byte[1000*1024];
		allocation3 = new byte[1000*1024];
		allocation4 = new byte[1000*1024];
		allocation5 = new byte[1000*1024]; }}Copy the code

1.2 Large objects directly into the old age

Large objects are objects that require a large amount of contiguous memory (e.g., strings, arrays).

Why is that?

To avoid the loss of efficiency when allocating memory for large objects due to replication caused by the allocation guarantee mechanism.

1.3 Long-lived objects will enter the old age

Since virtual machines use the idea of generational collection to manage memory, memory collection must be able to identify which objects should be placed in the new generation and which objects should be placed in the old generation. To do this, the virtual machine gives each object an object Age counter.

If the object survives after Eden is born and passes through the first Minor GC and can be accommodated by Survivor, it is moved to Survivor space and the object age is set to 1. Each time an object survives MinorGC in a Survivor, its age increases by one year, and when it reaches a certain age (15 by default), it is promoted to the old age. The age threshold for the object to be promoted to the old age can be set by using -xx :MaxTenuringThreshold.

1.4 Dynamic object age determination

In order to better adapt to the memory conditions of different programs, the VIRTUAL machine does not always require the object age to reach a certain value to enter the old age. If the sum of all the objects of the same age in the Survivor space is greater than half of the size of the Survivor space, the objects older than or equal to this age can directly enter the old age. No age required.

2 Is the object dead?

There are almost all object instances in the heap, and the first step before garbage collection is to determine which objects are dead (that is, objects that can no longer be used in any way).

2.1 Reference counting method

Add a reference counter to the object, incrementing it every time it is referenced; When a reference is invalid, the counter is decayed by 1; Any time an object with a 0 counter is no longer usable.

This method is easy to implement and efficient, but the mainstream virtual machine does not choose this algorithm to manage memory, its main reason is that it is difficult to solve the problem of object reference cycle. The cross-reference problem between objects is shown in the following code: There are no references between objects except objA and objB refer to each other. But because they refer to each other, their reference counters are not zero, and the reference counting algorithm cannot tell the GC collector to reclaim them.

public class ReferenceCountingGc {
    Object instance = null;
	public static void main(String[] args) {
		ReferenceCountingGc objA = new ReferenceCountingGc();
		ReferenceCountingGc objB = new ReferenceCountingGc();
		objA.instance = objB;
		objB.instance = objA;
		objA = null;
		objB = null; }}Copy the code

2.2 Accessibility analysis algorithm

The basic idea of this algorithm is to start from a series of objects called “GC Roots” and search down from these nodes. The path taken by the nodes is called the reference chain. If an object is not connected to GC Roots by any reference chain, it is proved that the object is unavailable.

2.3 References

Whether we judge the number of references by reference counting method or whether the reference chain of an object is reachable by reachability analysis method, the survival of an object is related to “references”.

Prior to JDK1.2, the definition of a reference in Java was traditional: if the value of a data store of type Reference represented the starting address of another piece of memory, that piece of memory represented a reference.

After JDK1.2, Java has expanded the concept of reference, including strong reference, soft reference, weak reference, and virtual reference.

1. Strong reference

Most of the references we used before were actually strong references, which are the most commonly used references. If an object has a strong reference, it is like an essential household item, and the garbage collector will never collect it. When running out of memory, the Java virtual machine would rather throw outofMemoryErrors to abort the program than randomly recycle objects with strong references to resolve the memory problem.

2. SoftReference

If an object has only soft references, it is similar to a living object that can have objects. If there is enough memory, the garbage collector will not reclaim it, and if there is not enough memory, it will reclaim the memory of these objects. As long as the garbage collector does not collect it, the object can be used by the program. Soft references can be used to implement memory sensitive caching.

A soft reference can be used in conjunction with a ReferenceQueue (ReferenceQueue), and if the object referenced by the soft reference is garbage collected, the JAVA virtual machine adds the soft reference to the ReferenceQueue associated with it.

3. WeakReference

If an object has only weak references, it is similar to a living object that can have objects. The difference between weak and soft references is that objects with only weak references have a shorter lifetime. When the garbage collector thread scans the memory area under its control, once it finds an object with only weak references, it reclaims its memory regardless of whether the current memory space is sufficient. However, because the garbage collector is a low-priority thread, objects that have only weak references are not necessarily found quickly.

Weak references can be used in conjunction with a ReferenceQueue (ReferenceQueue), and if the object referenced by a weak reference is garbage collected, the Java virtual machine adds the weak reference to the ReferenceQueue associated with it.

4. Virtual Reference

A “virtual reference” is, as the name implies, a virtual reference. Unlike the other references, a virtual reference does not determine the lifetime of an object. If an object holds only virtual references, it can be garbage collected at any time, just as if there were no references at all.

Virtual references are mainly used to track the activity of objects being garbage collected.

One difference between a virtual reference and a soft or weak reference is that a virtual reference must be used in conjunction with a ReferenceQueue. When the garbage collector is about to reclaim an object and finds that it has a virtual reference, it adds the virtual reference to the reference queue associated with it before reclaiming the object’s memory. A program can determine whether a referenced object is about to be garbage collected by determining whether a virtual reference has been added to the reference queue. If a program finds that a virtual reference has been added to the reference queue, it can take the necessary action before the memory of the referenced object is reclaimed.

In particular, weak references and virtual references are rarely used in program design, but soft references are often used. This is because soft references can speed up the recycling of garbage memory by JVM, maintain the safety of system operation, and prevent memory overflow and other problems.

2.4 Unreachable objects are not necessarily dead

Even in the reachability analysis, unreachable objects are not necessarily dead. At this time, they are temporarily in the “probation stage”. In order to truly declare an object dead, at least two marking processes must be experienced. In the reachability analysis method, unreachable objects are marked for the first time and screened once, and the screening condition is whether it is necessary to implement finalize method for this object. When objects do not overwrite the Finalize method, or finalize method has been called by the virtual machine, the virtual machine considers these two cases as unnecessary to execute.

Objects judged to need to be executed are marked a second time in a queue, and are actually reclaimed unless the object is associated with any other object in the reference chain.

2.5 How can I Determine whether a Constant is obsolete

The runtime constant pool mainly recycles obsolete constants. So, how do we tell if a constant is a discarded constant?

If the String “ABC” exists in the constant pool, the constant “ABC” is discarded if there is no current String reference to the constant. If memory reclamation occurs and it is necessary, the “ABC” will be cleaned out of the constant pool.

Note: In perhaps our clearest article on the Java memory area, we also reported that JDK1.7 and later JVMS have moved the runtime constant pool out of the method area and created an area in the Java Heap that holds the runtime constant pool.

2.6 How can I Determine whether a Class is useless

The method section mainly recycles useless classes, so how do you tell if a class is useless?

It’s easier to determine whether a constant is “obsolete,” whereas the criteria for determining whether a class is “useless” are much tougher. A class must meet all three of the following criteria to be considered “useless” :

  • All instances of the class have been reclaimed, meaning that there are no instances of the class in the Java heap.
  • The ClassLoader that loaded the class has been reclaimed.
  • The java.lang.Class object corresponding to this Class is not referenced anywhere, and the methods of this Class cannot be accessed anywhere through reflection.

A virtual machine can recycle useless classes that meet the above three conditions. This is only “yes”, not necessarily recycled when they are no longer used.

Garbage collection algorithm

3.1 Mark-clear algorithm

The algorithm is divided into “mark” and “clear” stages: firstly, mark all objects to be recycled, and uniformly recycle all marked objects after the completion of marking. It is the most basic collection algorithm, and it is very efficient, but it brings two obvious problems:

  1. The efficiency problem
  2. Space issues (large number of discontinuous fragments after marker clearing)

3.2 Replication Algorithm

To solve the efficiency problem, “copy” collection algorithms emerged. It divides memory into two equally sized pieces and uses one piece at a time. When this area of memory is used up, the surviving objects are copied to another area, and then the used space is cleaned up again. In this way, half of the memory range is reclaimed each time.

3.3 Mark-collation algorithm

The marking process is still the same as the “mark-clean” algorithm, but instead of directly collecting the recyclable objects, the next step is to move all the surviving objects towards a segment and then directly clean up the memory beyond the end boundary.

3.4 Generational collection algorithm

Current virtual machine garbage phones all use generational collection algorithms, which have no new ideas and just divide the memory into chunks according to different object life cycles. The Java heap is generally divided into the new generation and the old generation, so that we can choose the appropriate garbage collection algorithm based on the characteristics of each generation.

For example, in the new generation, a large number of objects die in each collection, so a replication algorithm can be used to complete each garbage collection for a small amount of object replication cost. Older objects have a higher chance of survival, and there is no extra space for allocation guarantees, so we must choose “mark-clear” or “mark-tidy” algorithms for garbage collection.

Extended interview question: Why HotSpot is divided into new generation and old generation?

Answer according to the introduction of the generation collection algorithm above.

4 Garbage Collector

If the collection algorithm is the methodology of memory collection, then the garbage collector is the concrete implementation of memory collection.

Although we are comparing collectors, we are not picking the best collector. Knowing that there is no single best garbage collector, let alone a universal garbage collector, all we can do is choose the one that suits our application scenario. Just think about it: if there were a perfect collector that could be used universally in any situation, our HotSpot VIRTUAL machine would not implement so many different garbage collectors.

4.1 Serial Collector

The Serial collector is the most basic and oldest garbage collector. As you can see from the name, this collector is a single-threaded collector. Its “single-threaded” significance not only means that it uses only one garbage collection thread to complete garbage collection, but it must “Stop The World” while garbage collection is in progress.

The new generation adopts copy algorithm and the old generation adopts mark-collation algorithm.

The designers of The virtual machine were certainly aware of The poor user experience of Stop The World, so The pauses were reduced in subsequent garbage collector designs (there were still pauses, and The search for The best garbage collector continued).

But does the Serial collector have any advantages over other garbage collectors? Of course it does, it’s simple and efficient (compared to the single-threaded efforts of other collectors). The Serial collector naturally achieves high single-thread collection efficiency because it has no overhead of thread interaction. Serial collectors are a good choice for virtual machines running in Client mode.

4.2 ParNew collector

The ParNew collector is essentially a multithreaded version of the Serial collector, with the same behavior (control parameters, collection algorithms, collection strategies, and so on) as the Serial collector, except that it uses multiple threads for garbage collection.

The new generation adopts copy algorithm and the old generation adopts mark-collation algorithm.

It is the first choice for many virtual machines running in Server mode, and in addition to the Serial collector, it is the only one that works with the CMS collector (a truly concurrent collector, described below).

Additions to the concepts of parallelism and concurrency:

  • Parallel: Multiple garbage collection threads work in Parallel while the user thread is still in a waiting state.

  • Concurrent: When the user thread executes concurrently (but not necessarily in parallel, but possibly alternately) with the garbage collector running on another CPU.

4.3 Parallel Scavenge

The Parallel Scavenge collector is similar to the ParNew collector. So what’s so special about it?

-xx :+UseParallelOldGC :+UseParallelOldGC :+UseParallelOldGCCopy the code

The Parallel Scavenge collector focuses on throughput (efficient CPU utilization). Garbage collectors such as CMS focus more on the pause times of user threads (improving user experience). Throughput is the ratio of the CPU time spent running user code to total CPU consumption. The Parallel Scavenge collector provides a number of parameters to find the most appropriate pause times or maximum throughput, and it is a good option to hand off memory management optimization to the virtual machine if manual optimization exists.

The new generation adopts copy algorithm and the old generation adopts mark-collation algorithm.

4.4.Serial Old collector

An older version of the Serial collector, which is also a single-threaded collector. It is used primarily for two purposes: as a companion to the Parallel Scavenge collector in JDK1.5 and earlier releases, and as a fallback to the CMS collector.

4.5 Parallel Old Collector

An older version of the Parallel Exploiter. Use multithreading and mark-tidy algorithms. The Parallel Avenge and Parallel Old collectors are preferred in applications where throughput and CPU resources are important.

4.6 CMS Collector

The CMS (Concurrent Mark Sweep) collector is a collector whose goal is to obtain the shortest collection pause time. It is well suited for use in ux applications.

The CMS (Concurrent Mark Sweep) collector was the HotSpot VIRTUAL machine’s first truly Concurrent collector, enabling the garbage collector thread to work (basically) at the same time as the user thread.

As the word Mark Sweep in its name implies, the CMS collector is implemented as a mark-and-sweep algorithm, which is a bit more complex than the previous garbage collectors. The whole process is divided into four steps:

  • Initial flag: Suspend all other threads and record objects directly connected to root, which is fast;
  • Concurrent marking: Enable both GC and user threads, using a closure structure to record reachable objects. At the end of this phase, however, the closure structure is not guaranteed to contain all currently reachable objects. Because the user thread may be constantly updating the reference field, the GC thread cannot guarantee real-time accessibility analysis. So the algorithm keeps track of where these reference updates happen.
  • Relabelling: The relabelling phase is to correct the mark record of the part of the object that the mark changes because the user program continues to run during the concurrent marking phase. The pause time of this phase is usually slightly longer than the initial marking phase, and much shorter than the concurrent marking phase
  • Concurrent cleanup: The user thread is started and the GC thread begins to clean the marked area.

As its name suggests, it is an excellent garbage collector with its main advantages: concurrent collection and low pauses. But it has the following three obvious disadvantages:

  • Sensitive to CPU resources;
  • Unable to handle floating garbage;
  • The collection algorithm it uses – the “mark-sweep” algorithm – results in a large amount of space debris at the end of the collection.

4.7 G1 Collector

G1 (garbage-First) is a server-based Garbage collector, mainly for machines equipped with multiple processors and large memory capacity. High throughput performance characteristics while meeting the GC pause time requirements with extremely high probability.

Is seen as an important evolutionary feature of the HotSpot virtual machine in JDK1.7. It has the following characteristics:

  • Parallelism and concurrency: The G1 takes full advantage of CPU, multi-core hardware, and uses multiple cpus (cpus or CPU cores) to reduce stop-the-world pause times. While other collectors would have paused GC actions performed by Java threads, the G1 collector can still allow Java programs to continue executing concurrently.
  • Generational collection: Although G1 can manage the entire GC heap independently without the cooperation of other collectors, the concept of generational collection is retained.
  • Spatial consolidation: Different from CMS’s “mark-clean” algorithm, G1 is a collector based on the “mark-clean” algorithm as a whole; Locally, it is based on a “copy” algorithm.
  • Predictable pauses: This is another big advantage G1 has over CMS. Reducing pause times is a common concern of BOTH G1 and CMS, but G1 also models predictable pause times, allowing users to explicitly specify a time segment of M milliseconds in length.

The G1 collector operates in the following steps:

  • Initial tag
  • Concurrent tags
  • In the end tag
  • Screening of recycling

The G1 collector maintains a priority list behind the scenes, prioritizing the Region with the greatest collection value (hence its name garbage-first) based on the allowed collection time each time. This method of using regions to divide memory space and prioritized Region collection ensures that THE GF collector can collect memory as efficiently as possible within a limited time.

Reference:

  • Understanding the Java Virtual Machine in Depth: Advanced JVM Features and Best Practices (2nd Edition)
  • My.oschina.net/hosee/blog/…

If you are in full bloom, breeze. Welcome to follow my wechat official account: “Java Interview Clearance Manual”, a wechat official account with temperature. Public number background reply keyword “1”, you may see what you want oh!