Tang’s monk: dragon is not you so ride

\

Want to improve the internal work of the programmer is nothing more than data structure and algorithm + operating system + network + bottom layer, and all the Java code is running on the JVM, understand the BENEFITS of the JVM is: \

Write better and more robust code.

Improve Java performance and troubleshoot problems.

Interview must ask, to have a certain depth of knowledge.

1. Briefly describe the JVM memory model

At a macro level, the JVM memory area is divided into three parts: thread shared area, thread private area, and direct memory area. \

1.1. Thread Sharing area

1.1.1, heap area

The Heap is the largest area of memory in the JVM, and almost all object instances are allocated space on the Heap. The heap area is subdivided into the young generation and the old generation. The young generation is divided into Eden, S0 and S1, and their default ratio is 8:1:1.

1.1.1 meta space

Methods area:

  1. The Java Virtual Machine Specification only specifies that there areMethods areaSuch aconceptWith itsrole.HotSpotDid it before JDK8The permanent generationI realized this concept. Used primarily to store data such as class information, constant pools, static variables, JIT-compiled code, etc.
  2. Metadata information for classes in PermGen may be collected at each FullGC, but the results are hardly satisfactory. And how much space to allocate to PermGen because it’s hard to determine the size for storing all of this data. So the official removes permanent generation in JDK8.

Official explanation for removing permanent generation:

  1. This is part of the JRockit and Hotspot convergence effort. JRockit customers do not need to configure the permanent generation (since JRockit does not have a permanent generation) and are accustomed to not configuring the permanent generation.
  2. That is, removing the persistent generation is an effort to merge HotSpot JVM with JRockit VM, as JRockit does not have persistent generations and does not need to be configured.

Dimension:

In Java, using persistent generations to store class information, constants, static variables, etc., is not a good idea because it can easily run out of memory. At the same time, it is also difficult to tune the performance of permanent generation, so in JDK8, permanent generation is removed and metaspace is introduced. The original class, field and other variables are put into metaspace.

Conclusion:

Similar in nature to permanent generations, meta-spaces are implementations of method areas in the JVM specification. However, the biggest difference between a meta-space and a permanent generation is that the meta-space is not in the virtual machine, but uses local memory. Therefore, by default, the size of the meta-space is limited only by local memory, but you can specify the size of the meta-space with parameters.

1.2. Direct memory area

Direct memory:

In general, Native functions are used to manipulate C++ code to achieve direct allocation of out-of-heap memory, which is not part of the virtual machine runtime data area, nor the memory area defined in the Java virtual machine specification. This block of memory is not limited by the size of the Java heap, but is limited by the total size of the native memory so an OOM exception will occur. After space allocation, data is not copied back and forth between the Java heap and Native heap, which can effectively improve the read and write efficiency. However, the creation and destruction of Buffer is slower than that of ordinary Buffer.

PS: If NIO is used, the local memory area will be used frequently. In this case, JVM memory ≈ method area + heap + stack + direct memory

1.3. Thread private area

The program counter, virtual machine stack, local method stack and thread declaration cycle are the same.

1.3.1 Program counter

In class, for example, when you are reading the novel “Jade Dynasty” and you see chapter 1412, the teacher asks you to answer a question. At this time, you must first answer the teacher’s question, and then continue to read. At this time, you can use bookmarks or remember your reading position by memory, so as to continue to read.

As with code runtime, the program counter is used to record the instruction address of the bytecode being executed by the virtual machine under the current thread. It has the following characteristics:

  1. Thread private

In the case of multithreading, each thread needs to have its own program counter in order for it to switch at the same time and still recover.

  1. No OutOfMemoryError is specified

The program counter stores the line number of the bytecode file, and this range is known to allocate memory that will never overflow when it is first allocated.

  1. The value is null when Native methods are executed

Native methods are mostly implemented through C and are not compiled into bytecode instructions that need to be executed, so there is no need to store the line number of the bytecode file.

1.3.2 Virtual Machine stack

Method in and out of the stack: called methods are packaged into frames, and a frame must contain at least one local variable table, operand stack, frame data area, and dynamic linkage.

Dynamic linking: \

When the stack frame contains a reference to the run-time constant pool, class loading is performed with symbolic reference to direct reference resolution and link replacement.

Local variation scale:

The local variable table is one of the important groups of stack frames. It mainly saves function parameters and local variable information. The scope of the variables in the local variable table is the function currently called. After the function call, the stack frame is destroyed. Local variables are also destroyed, freeing up space.

Operand stack:

Stores data during the execution of the Java virtual machine

Method returns the address:

The position at which the method is called, when the method exits, is actually the same as when the current stack frame exits.

For example, performing simple addition and subtraction:

public class ShowByteCode {
    private String xx;
    private static final int TEST = 1;
    public ShowByteCode() {
    }
    public int calc() {
        int a = 100;
        int b = 200;
        int c = 300;
        return(a + b) * c; }}Copy the code

performjavap -c *.class:

1.3.3. Local method Stack

Similar to the virtual stack, it only serves the Native methods used.

2. Determine whether the object is alive

Garbage Collection is required when the JVM runs out of space, and generally shared areas such as heap and method areas are reclaimed. The only thing you need to do before doing a reclamation is determine which objects are dead and which are alive. There are two common methods of reference counting and reachability analysis.

2.1 reference counting method

The idea is to add a reference counter to a Java object that is +1 every time it is referenced somewhere; If the counter is not 0, the object is judged to be alive. Otherwise, death is judged (counter = 0). Advantages:

Simple implementation and efficient judgment.

Disadvantages:

Cannot solve the problem of objects referring to each other circularly

class GcObject {
    public Object instance = null;
}
public class GcDemo {
    public static void main(String[] args) {
        GcObject object1 = new GcObject(); // step 1
        GcObject object2 = new GcObject(); // step 2
        
        object1.instance = object2 ;//step 3
        object2.instance = object1; //step 4
        
        object1 = null; //step 5
        object2 = null; // step 6}}Copy the code

Step1: Reference count of GcObject instance 1 +1, instance 1 reference count = 1

Step2: The reference count of GcObject instance 2 is +1, and the reference count of instance 2 is = 1

Step3: Reference count of GcObject instance 2 +1, reference count of instance 2 = 2

Step4: The reference count of GcObject instance 1 is +1, and the reference count of instance 1 is = 2

Step5: The reference count of GcObject instance 1 is -1, and the result is 1

Step6: The reference count of GcObject instance 2 is -1, and the result is 1

According to the above analysis, the number of references of instance 1 and instance 2 is not 0, but they refer to each other. Therefore, the memory occupied by these two instances cannot be released.

2.2 Accessibility analysis

Many major commercial languages (such as Java, C#) use the chain of references method to determine whether an object is alive or not. The general idea is to take a series of GC Roots objects as a starting point from which to search down. In the Java language, objects that can be used as GC Roots include the following:

  1. The first is a virtual machine reference objects in the stack, the application to the normal to create an object, the object will be opening a space on the heap, at the same time, the space will be address as a reference to the virtual machine in the stack, if the object life cycle is over, then reference will be from a virtual machine in the stack, so if you have any references in the virtual machine stack, means that the object is useful, This situation is the most common.
  2. The second is usA global static object defined in a class, that is, usestaticSince the virtual stack is thread private, references to this object are kept in the common method area, so it is obviously necessary to have static references in the method area as GC Roots.
  3. The third isConst reference, is usedstatic finalBecause such references are not modified after initialization, objects referenced in the method area constant pool should also be GC Roots.
  4. Fourth, when using JNI technology, sometimes simple Java code cannot meet our needs. We may need to call C or C++ code in Java, so we will use Native method. There is a special local method stack in THE JVM memory to store references to these objects. So objects referenced in the local method stack are also treated as GC Roots.

The GC Root step consists of the following three steps:

2.1.1 Accessibility analysis

When an object is not connected to GC Roots by any reference chain, the object is judged unreachable. Note: Reachability analysis only determines whether an object is reachable, but it is not sufficient to determine whether an object is alive/dead. \

2.1.2 Mark & filter the first time

If the filtered condition object does not rewrite Finalize or call Finalize, the object will be added to the F-queue

2.1.3 Second mark & filter

When an object is marked & filtered the first time, it is marked & ready to be filtered the second time. If the object has not been referenced by GC Root after f-queue filtering, it will be reclaimed, giving a second chance.

2.3 Four reference types

2.3.1 strong reference

Strong references are the most commonly used references. The garbage collector will never collect it, and would rather throw OOM when out of memory to cause an exception in the program.

2.3.2 soft references

The garbage collector does not reclaim a SoftReference object when there is enough memory, but it does when there is not, which is especially good for creating a cache.

2.3.3 weak references

WeakReference is that when the object is scanned, the object will be reclaimed regardless of whether the memory is sufficient. A ThreadLocal Key is a weak reference.

2.3.4 virtual reference

If an object has only phantom references, then any fit can be reclaimed just as if there were no references at all. Activities that are collected primarily with trace objects and garbage collectors.

3. Garbage collection algorithm

In order to wave garbage collection, the operating system will generally use three algorithms, namely, tag clearing, copy algorithm and tag sorting. These three algorithms have their own advantages and disadvantages. Here is a brief introduction:

3.1 mark clearing

Principle: \

The algorithm is divided into two stages: marking and clearing: marking all the objects to be recycled, after the completion of marking all the marked objects.

Disadvantages:

A large number of discrete memory fragments are generated after the tag is cleared, causing GC to be triggered.

3.2. Tag replication

Principle: \

Divide the available memory into two equally sized pieces by capacity and use only one piece at a time. When this area of memory is used up, the surviving objects are copied to the other area, and the used memory space is cleaned up again.

Disadvantages:

The cost of this algorithm is to reduce memory by half and move data back and forth.

3.3. Mark sorting

Principle: \

The first step is to mark all objects that need to be reclaimed. After the mark is complete, the next step is to move all surviving objects toward one end and then clean up memory directly beyond the end boundary.

Disadvantages:

It involves moving a large number of objects, which is inefficient.

Conclusion:

indicators Mark sweep Tag to sort out Mark copy
speed medium The slowest fast
The space overhead Less (but will pile up debris) Less (do not accumulate debris) Usually twice the size of a live object (no shards)
Moving objects no is is

3.4. Tricolor markers and read-write barriers

All three of the previous collection algorithms have been mentionedtagHow is the problem marked?In the middle of a sentence, be careful not to have a wife!

I feel that the interview should not ask so deep, but in order to pretend forced must Mark! Three-color labeling method is generally used for CMS and G1 labeling. According to the accessibility analysis, GC Roots are used for traversal visits, and the reachable objects are the living objects, while the final unreachable objects are the objects to be GC. The general process is to mark the objects encountered in the process of traversing the object graph in the following three colors according to whether they have been visited:

White: not yet visited.

Black: This object has been accessed, and all other objects referenced by this object have also been accessed. Gray: This object has been accessed, but other objects referenced by this object have not been accessed. It will turn black after all access.

Suppose you now have white, gray, and black (representing the color of the current object), traverse the access process: \

1. All objects are initially in the white set.

2. Move objects directly referenced by GC Roots to the grey collection.

3, from the gray set of objects: the first step to reference the object to all other objects in the gray set, the second step to move the object to the black set.

4. Repeat Step 3 until the grey set is empty.

5. Objects still in the white set after the end are unreachable by GC Roots and can be recycled.

References between objects do not change when STW is used, and markup can be done easily. When concurrent tokens are supported, references between objects can change, and multiple tokens and missing tokens can occur.

3.4. 1.Floating garbage

Condition: the GC thread traverses E(E is gray), a business thread executes D.E = null, at which point E should be collected. But the GC thread already thinks E is gray and continues to iterate, resulting in E not being collected.

2, 3.4.The leakage

The GC thread traverses to E(grayed). The business thread performed E–>G disconnect and D–>G link. The GC thread finds that E cannot reach G because it is black and will no longer traverse the mark. The final result is missing G. There are two necessary conditions for missing mark: gray to white disconnect, black to white build.

Object G = E.G;    // Step 1: Read
Object E.G = null; // Step 2: Write
Object D.G = G;   // Step 3: Write
Copy the code

Missed mark solution:

The object G is stored in a specific collection, and the objects in the collection are re-marked after the concurrent tag traversal is complete.

3.4.2.1 CMS Scheme

So let’s say B points to C, but then B doesn’t point to C, A points to D, and the easiest way to do that is to turn A gray and wait for the next iteration. ABA problems may occur in CMS:

1, collecting thread M1 is marking A, attribute A.1 is marking finished, attribute A.2 is marking.

2. Business thread M2 points the attribute 1 to C, and thread M3 marks A bit gray at this time in CMS scheme.

3. Recycling thread M1 considers that all attributes are marked, sets A to black, and C misses the mark. So the CMS phase needs to be re-marked.

3.4.2.2 Read/write Barriers

Missing flags are implemented in three steps, with the JVM adding a read barrier to block the first step and a write barrier to block the second and third steps.

Write barrier + SATB(raw snapshot) to break the gray-to-white disconnect.

Write barriers + incremental updates to destroy black to white builds.

Read barrier a conservative way to destroy the white storage after the grey to white disconnect, while using read barrier OK.

Modern garbage collectors that use reachability analysis almost all borrow the algorithmic idea of tricolor tagging, although they implement it in different ways. For read/write barriers, using Java HotSpot VM as an example, the following is the solution to handle missing flags when marking concurrently:

CMS: Write barrier + incremental update

G1: Write barrier + SATB

ZGC: Read barrier

Incremental updates used in CMS require a rescan of GC Roots in the relabelling phase, in addition to traversing the write barrier records, because CMS does not add write barriers for instructions such as astore_x.

4. GC process

The core idea is to choose different garbage collection algorithms according to the characteristics of different generations.

  1. The young generationUse:Replication algorithm
  2. The old sUse:Tag to sort outorMark clearAlgorithm.

Why there is a younger generation:

The benefit of generational is to optimize GC performance, because without generational scanning of all regions every time, GC can be exhausted. Since a lot of objects are almost dead, if we are generational, we can make a lot of space by putting the newly created objects in one place and recycling the area where the objects are dead (80% +) first.

4.1. Young Generation

The HotSpot JVM divides the young generation into three parts: one Eden zone and two Survivor zones (called from and to). The default ratio is 8:1:1. In general, newly created objects are allocated to the Eden zone (with special treatment for large objects), and if they are still alive after the first Minor GC, they are moved to the Survivor zone. Each time an object survives a Minor GC in a Survivor zone, its age increases by one year, and when it ages a certain number of times (15 by default), it is moved to the tenured generation. The garbage collection algorithm of the younger generation uses the replication algorithm.

Young generation GC process: Before GC starts, young generation objects only exist in Eden and Survivor zone named From, Survivor zone named To is always empty. GC will result if the newly allocated object is found to be insufficient in Eden’s requested space. \

Yang GC: All living objects in Eden will be copied To To, whereas in From, living objects will be chosen based on their age. Objects whose age reaches a certain value (the age threshold can be set by -xx :MaxTenuringThreshold) are moved To the aged generation, and objects that do not reach the threshold are copied To the To region. After this GC, the Eden and From areas have been emptied. At this point, From and To switch roles, so the new To is the From before the last GC, and the new From is the To before the last GC. Either way, the Survivor region named To is guaranteed To be empty. The Minor GC repeats this process until the To region is filled, and when the To region is filled, all objects are moved To the aged generation. Note that if the Yang GC still runs out of space, the space guarantee mechanism will send data to the Old section

Card Table:

  1. To support high frequency generation recycling, the virtual machine uses a type calledCard table(Card Table) data structure, Card Table as a set of bits,Each bit can be used to indicate whether all objects in a region of the old generation hold references to the new generation object.
  2. The generation GENERATION GC does not spend a lot of time scanning all the aged generation objects to determine the reference relationship of each object. The card table is scanned first. Only when the label bit of the card table is 1, the aged generation objects in a given area need to be scanned. The old generation object in the region where the card table is 0 must contain no reference to the new generation.

4.2. The old days

Old GC process:

Objects stored in the old age are objects older than 15 that have survived for a long time or large objects that have triggered the allocation guarantee mechanism for the old age. The gc that fired in the old days was called a Major GC or a full GC. The full GC will contain the gc of the young generation. Full GC uses mark-sweep or tag collation. In the case of full GC, it blocks the normal operation of the program. The gc of the older generation is more than 10 times slower than that of the younger generation. It has a big impact on efficiency. So try to avoid old GC!

4.3 meta space

The collection of the permanent generation moves along with the Full GC, consuming performance. Each type of garbage collection requires special handling of metadata. Stripping metadata simplifies garbage collection and improves efficiency.

-xx :MetaspaceSize Indicates the size of the initial space. Reaching this value triggers garbage collection for type offloading, and the GC adjusts the value:

If you free up a lot of space, lower the value appropriately; If very little space is freed, increase this value appropriately until MaxMetaspaceSize is exceeded.

– XX: MaxMetaspaceSize:

Maximum space, default is unlimited.

4.4 summary of garbage recycling process

The general GC collection process is shown in the figure above, and there is another setting where large objects go straight to the old age: \

  1. If allocation fails in the new generation and the object is a large array without any object references, it can be allocated directly to the old generation. Avoid a garbage collection in the new generation by distributing in the old.
  2. Set up – XX: PretenureSizeThreshold value, any object is greater than this value will not try in the new generation distribution, will allocate memory in old s.

Memory reclamation and allocation policies

  1. Objects are allocated on Eden first, and garbage collection in this area is frequent and fast.
  2. Large objects go directly to old generation.
  3. The elder (default for long-term survival: 15 times) is followed by the old generation.
  4. The sum of the size of all objects of the same age in Survivor space is greater than half of that in Survivor space, and the objects whose age is greater than or equal to that age will enter the old generation.
  5. Space allocation guarantee (guarantee minorGC) : if there are no new generation surviving objects in Survivor zone after Minor GC, the objects that Suvivor can’t accommodate will be directly entered into old age.

Garbage collector

5.1. Garbage collector

The heap is the focus area of the garbage collection mechanism. We know that there are three types of garbage collection mechanisms: Minor, Major, and Full. The first two are for the heap. The younger generation is called the Minor GC and the older generation is called the Major GC.

  1. JDK7, JDK8 default garbage collectorThe Insane + Parallel Old.
  2. JDK9 default garbage collectorG1
  3. A common combination of server-side development isParNew + CMS

Garbage collector: Garbage collector: garbage collector: garbage collector: garbage collector

STW

The Stop-the-world mechanism in Java, known simply as STW, is when a garbage collection algorithm is executed and all other threads of The Java application are suspended (except for The garbage collection helper). All Java code stops. Native code can execute but cannot interact with the JVM. If STW occurs, it is probably caused by GC.

throughput

Throughput = run user code time/(run user code time + garbage collection time). For example, if the virtual machine runs for 100 minutes and the garbage collector takes 1 minute, the throughput is 99%

Garbage collection time

Garbage collection frequency * Single garbage collection time

Parallel collection

Refers to multiple garbage collection threads working in parallel while the user thread is still waiting.

Concurrent collection

Refers to the user thread and the garbage collector thread working at the same time (not necessarily in parallel and may be executed alternately). The user program continues to run, while the garbage collector runs on another CPU.

5.2 The New Generation

The New generation are the Serial, ParNew, and Parallel Scavenge.

The name of the Serial/parallel/concurrent Recovery algorithm Usage scenarios You can cooperate with CMS
Serial serial copy VMS in Client mode with only one CPU is
ParNew Parallel (parallel version of Serial) copy Multiple cpus, often in Server mode is
Parallel Scavenge parallel copy Multi-cpu and throughput focused no

5.3. The old days

Serial Old, Parallel Old, CMS three garbage collector.

The name of the Serial/parallel/concurrent Recovery algorithm Usage scenarios Combinatorial youth
Serial Old serial Tag to sort out A single CPU Serial, ParNew, Parallel Avenge
Parallel Old parallel Tag to sort out More CPU Parallel Scavenge
CMS concurrent Mark clear Multi-cpu and concerned about throughput, commonly used Server side Serial 、ParNew
5.3.1, CMS

CMS(Concurrent Mark Sweep) is important.

Original intention and purpose of CMS:

To eliminate long pauses in the Full GC cycle between the Throught and Serial collectors. Is a collector to obtain the shortest recovery pause time as the goal, with adaptive adjustment strategy, suitable for Internet sites and B/S service applications.

Applicable scenarios of CMS:

If your application needs faster responses, you don’t want long pauses, and you have a lot of CPU resources, the CMS collector is a good fit. For example, common server-side tasks.

Advantages:

Concurrent collection, low pause.

Disadvantages:

  1. The CMS collector is very sensitive to CPU resources: In the concurrent phase, although it does not cause user threads to pause, it consumes CPU resources, resulting in slower referencing programs and reduced overall throughput.
  2. Unable to handle floating garbage: Because the CMS concurrent cleanup phase user threads are still running, new garbage is constantly generated along with the program running. This part of garbage appears after the marking process, and CMS cannot process it in this collection, so it has to be cleaned up in the next GC. This part of the garbage is calledFloating garbage.If there is no more floating garbage in the JVM, then the JVM starts Serial Old instead of CMS.
  3. Space debris: CMS is based onMark-clearAlgorithm implementation of the collector, usingMark-clearAfter the algorithm is collected, it will generateA large number of pieces.

CMS recycling process:

  1. Initial tag:Trigger STW, just marks objects that GC ROOTS can directly associate with, fast.
  2. Concurrent tags:Without causing STW, whether all Old objects can be chained to GC Roots
  3. To mark:Trigger STWTo fix a tag that changes during concurrent tagging because the user program continues to run. The pause time in this phase is slightly longer than in the initial tagging phase, but shorter than in the concurrent tagging phase.
  4. Concurrent remove:Without causing STW, normal operation, the mark clearing algorithm to clean and delete the marked phase judged by the dead object.

Conclusion:

  1. Concurrent tagsandConcurrent removeIs the most time-consuming but does not require stopping the user thread.Initial tagandTo markIt takes less time, but the user thread needs to be stopped, so the entire GC process has a shorter pause and can work with the user thread most of the time.

5.4, the G1

The previous GC collector split Heap:The garbage collector used to beNew generation + old ageIn order to reduce the impact of STW on the system, Garbage Collector G1(garbage-First Garbage Collector) is introduced. G1 is a Garbage Collector for server-side applications with the following features:

1. Parallelism and concurrency: G1 makes full use of the hardware advantages of multi-CPU and multi-core environments, allowing Java programs to continue to execute in a concurrent manner.

Generational collection: The concept of generational collection is still alive in G1, which allows for different ways of handling newly created objects and old objects that have survived for a while and have survived multiple GC’s for better collection results.

3. Spatial integration: G1 is based on the mark-collation algorithm as a whole, and on the replication algorithm locally (between two regions). There will be no memory space fragmentation during G1 operation.

4. Predictable pauses: G1 is better than CMS in establishing predictable pause time models, allowing users to specify that no more than N milliseconds should be spent on garbage collection within a time segment of M milliseconds in length.

G1, as the default collector on the server after JDK9, no longer distinguishes between the young generation and the old generation for garbage collection. By default, G1 divides the heap memory into N partitions, each 1~32M(always a power of 2). Four different Region labels are provided: Eden, Survivor, Old, and Humongous. H area can be considered as a special column in Old area for storing big data. The data storage type of H area generally conforms to the following conditions:

When 0.5 Region <= When the object size <= 1 Region, the data is stored in Region H

If the object size is greater than 1 Region, it is stored in a contiguous H Region.

Also introduced in G1RememberSets,CollectionSetsHelp to better perform GC.

Rememberremembersets — RememberSets — RememberSets — RememberSets — RememberSets — RememberSets — RememberSets — RememberSets

2. CollectionSets: Csets are regions that need to be cleaned in a GC. Note that G1 does not clean all regions in each GC, and only a few regions may be cleaned. For cross-generation object references of old -> young and old -> old, only rsets in the corresponding CSet can be scanned during GC.

G1 GC is generally divided into Yang GC and Mixed GC.

Young GC: CSet is the Region of all Young generations

Mixed GC: CSet is all the regions in the young generation plus the high-yield regions marked in the global concurrent marking phase

5.4.1, Yang GC

Standard young generation GC algorithm, the overall idea is similar to CMS.

5.4.2, Mixed GC

There is no Old GC in G1. There is a Mixed GC that simultaneously GC the Old age and the new generation. Its recycling process is as follows:

1. Initial tag: is the STW event, whose job is to tag objects directly reachable by GC ROOTS. Tag bit RootRegion.

2. RootRegion scan: instead of STW event, take RootRegion and scan all regions in the entire Old Region to see if RootRegion exists in the Rset of each Region. Some of them are marked.

3, concurrent mark: with CMS concurrent mark does not need STW, traversal scope is reduced, here only need to traverse the second step is marked to reference the old object RSet.

4, final marking: with CMS re-marking will STW, using SATB operation, faster.

5. Clear: STW operation: use the copy cleaning algorithm to check the regions with and without viable objects and update the Rset. Collect Empty regions into the allocatable Region queue. \

Summary of recycling:

1. After global Concurrent marking, the Collector knows which regions have live objects. In addition, fully recyclable regions (with no living objects) are collected and added to the allocatable Region queue to reclaim the memory of these regions. For regions with living objects, G1 retrieves objects based on the statistical model from regions with the highest revenue and the cost less than the upper limit specified by the user. The collection of selected regions is called a collection set.

2. Cset in MIX GC = regions of all young generations + Old regions with high returns are collected according to global Concurrent marking statistics.

3. Cset in YGC = regions in all young generations + Controls the overhead of young GC by controlling the number of regions in the young generation.

4. Both YGC and MIXGC adopt multithreaded copy cleaning, and the whole process will STW. The low latency of G1 is due to the fact that the reclaim area becomes more precise and the range is smaller.

G1 Speed up point:

1 The X area is directly deleted during re-marking.

2. Rset reduces the range of scanning.

3. SATB is faster than CMS in the re-marking stage.

4 Cleaning process Some regions with low survival rate are selected for cleaning, but not all of them, which improves the cleaning efficiency.

Conclusion:

Just like your mom told you to clean up your bedroom, you may have only picked up the big, obvious pieces of trash, leaving the nooks and crannies behind. There are many details about the G1 that I haven’t seen yet.

To sum up G1 thinking in a word: selectively remove most of the garbage each time to ensure timeliness and normal operation of the system.

6, New object

A Java class consists of two main processes, compilation and execution, from coding to execution.

Compile: compile the. Java file we have written into a. Class file using the Javac command.

Run: Executes the compiled. Class files into the JVM.

When the Jvm runs a class class, instead of loading all the classes into memory at once, it loads as needed and only once.

6.1. Class lifecycle

6.1.1, load,

Loading refers to loading class bytecode files into memory from various sources through the class loader, with two key points:

  1. Bytecode sources: Common loading sources include.class files compiled from local paths,.class files compiled from JAR packages, real-time compilation from remote networks, and dynamic proxies
  2. Class loaders: Generally include startup class loaders, extension class loaders, application class loaders, and user custom class loaders (encryption and decryption types).
6.1.2, validation,

The main purpose is to ensure that the loaded byte stream conforms to the virtual machine specification and does not cause security errors. File format verification, metadata verification, bytecode verification, symbol reference verification.

Also 6.1.3, preparation,

Public static int age = 14, age = 0 after preparation, age = 14 during initialization, and 14 if final is added at this stage.

6.1.4, parsing,

Replaces symbolic references in the constant pool with direct references.

6.1.5. Initialization

The user application can participate in the class loading stage through the custom class loader before the rest of the action is completely dominated and controlled by the virtual machine. This is where the actual execution of the code defined in the class begins: initialize the static block and, if there is a parent class, initialize the parent class first.

6.1.6, use,

After the class is loaded, we allocate memory and initialize the object:

  1. Allocate the appropriate amount of memory for objects
  2. Assign default values to instance variables
  3. Set the header information, hash code, GC generation age, and metadata information of the object
  4. Perform constructor (init) initialization.
6.1.7, unloading

In the end, the GC algorithm is used to collect objects.

6.2. Objects occupy bytes

An object header consists of three parts: object header (MarkWord, classPointer), Instance Data, align Padding, To see the memory usage details, IDEA calls the jol-core package.

How many bytes does the new Object() take

  1. Markword 8 bytes + CLASspointer 4 bytes (default calssPointer compression) + PADDING 4 bytes = 16 bytes
  2. If classpointer compression is not enabled: MarkWord 8 bytes + CLASspointer 8 bytes = 16 bytes

User(int id,String name) User u = new User(1,” li Si “)

Markword 8 bytes + Enable classPointer 4 bytes + Instance data int 4 bytes + Enable common object pointer compression String4 bytes + PADDING 4 = 24 bytes

6.3 Object access mode

Use the handle: \

The biggest benefit of using handles for access is that reference stores a stable handle address, which only changes the instance data pointer in the handle when the object is moved (a very common behavior in garbage collection). Reference itself does not need to be modified.

Direct pointer:

The object address is stored directly in reference. The biggest benefit is that it is faster, and it saves the time overhead of a pointer location, which can add up to a significant execution cost because objects are accessed very frequently in Java.

Sun HotSpot uses direct pointer access for object access.

7. Must objects be created on the heap

Conclusion: The JVM optimizes automatically to create an object on the stack, not necessarily if the object is used only in the method area after escape analysis.

7.1 escape analysis

Escape Analysis is simply: The Java Hotspot VIRTUAL machine analyzes the scope of use of newly created objects and decides whether or not to allocate memory on the Java heap.

7.2. Scalar Substitution

Scalar substitution: The JVM determines that the object will not be accessed externally through escape analysis. This reduces the burden of garbage collection by allocating memory on the stack by scalar substitution decomposition of the object, so that the memory space occupied by the object can be destroyed as the stack frame goes off the stack.

Scalars: Quantities that cannot be further decomposed, and JAVA’s basic data types are scalars

Aggregate amount: In JAVA, an object is the amount of aggregate that can be further decomposed.

7.3. Stack Allocation

JVM objects are allocated in the heap, and when the objects are not referenced, the GC is used to reclaim the memory. If the number of objects is large, the GC will be under great pressure, which also indirectly affects the application performance.

To reduce the number of temporary objects allocated in the heap, the JVM uses escape analysis to determine that the object will not be accessed externally. This reduces the burden of garbage collection by allocating memory on the stack by scalar substitution decomposition of the object, so that the memory space occupied by the object can be destroyed as the stack frame goes off the stack.

7.4. Synchronization elimination

Synchronous elimination is an optimization technique provided by the Java Virtual Machine. Escape analysis can determine whether an object will be accessed by other threads. If there is no thread escape, there will be no resource contention for reading and writing of the object. If there is no resource contention, the synchronization lock on the object can be eliminated. For example, a StringBuffer is called inside a method.

Escape analysis conclusion:

Scalar substitution, stack allocation, and lock elimination can be done after escape analysis. However, escape analysis itself also requires a series of complex analysis, which is actually a relatively time-consuming process. If the object cannot be optimized by escape analysis after layers of analysis, it will be time-consuming, so use with caution.

Class loaders

Generally, it cannot be interfered in the connection stage, but most of the interference in the class loading stage. For any class, its uniqueness in the Java VIRTUAL machine needs to be established by the class loader and the class itself. There are three important methods during class loading:

1, loadClass() : the entry to load the target class. It first checks the current ClassLoader and its parents to see if the target class has been loaded

FindClass () : If not found, the parent will try to load the class. If neither parent can load the class, the custom loader will call findClass() to load the target class itself

DefineClass () : Get this bytecode and call defineClass() to convert the bytecode into a Class object.

8.1. Parent delegation mechanism

Definition: \

When a classloader needs to load a.class file, it first delegates the task to its parent classloader, recurses the operation, and only loads the class if the parent classloader does not load it.

Function:

1. It can prevent the same. Class from being loaded repeatedly. You can ask the delegate up there, so once it’s loaded, you don’t have to load it again. Ensure data security.

2, ensure that the core.class can not be tampered *, through the delegation, will not tamper with the core.class. ‘

Class loaders:

1, BootstrapClassLoader (start class loader) : c++ written, loaded Java core library Java.*, JAVA_HOME/lib

2, ExtClassLoader (standard extension class loader) : Java written extension library, JAVA_HOME/lib/ext

AppClassLoader: the directory where the loader resides, such as the ClassPath of the user. Dir location

4. CustomClassLoader: user-defined class loader that loads class files in a specified path

8.2 about the loading mechanism

The parent delegate mechanism is just one common pattern for Java class loading. There are other loading mechanisms, such as Tomcat, which always tries to load a class first, and if there is no loader at the next level, the parent loader order is reversed. For example, when using a third-party framework JDBC and implementation, errors are raised because the JDK’s built-in JDBC interface is loaded by the startup class, while the third-party implementation interface is loaded by the application class. That’s why the JDK introduced the SPI mechanism thread context loader for loading (unlike Dubbo’s SPI).

9, OOM, CPU100%

Common instructions for system performance analysis:

tool use
jps Prints information about the status of the processes running in the JVM
jstack Generate a thread snapshot of the VM at the current time
jstat Vm statistics monitoring tool
jinfo View and adjust VM parameters in real time
jmap The heapdump file is generated for vm memory dump snapshots
JConsole Visual management tool, commonly used

9.1, OOM

9.1.1 Why OOM

OOM occurrence can be summarized into two reasons:

  1. Insufficient memory allocated to the JVM.
  2. The allocated memory is sufficient, but the code is not written well, and the excess memory is not freed, resulting in insufficient memory.
9.1.2 Three Types OOM
9.2.1 Overflow of heap memory

This situation is most common with Java Heap Space. A memory mapping tool is used to take a snapshot of a heap Dump and determine whether it is leaking or running out of memory. A memory leak

Check the leak object’s chain of references to GC Roots with the tool. Find out what path the leaking object is associated with GC Roots, so that the garbage collection mechanism cannot recover it, and finally locate the leaking code more accurately.

No leakage

Objects in memory must be alive, and the heap parameters of the virtual machine are used to check the code for objects that are too long alive or held to try to reduce runtime memory consumption.

9.2.2 Virtual machine stack and local method stack overflow

In the HotSpot virtual machine there is no distinction between the virtual machine stack and the local method stack, so the stack size can only be set by the ** -xSS ** parameter. Two types of exceptions are described in the Java Virtual Machine specification:

StackOverflowError: This exception is thrown when the stack depth of thread requests exceeds the maximum depth allowed by the virtual machine. OutOfMemoryError: This exception is thrown when the virtual machine cannot obtain sufficient space while expanding the stack.

In a single-threaded environment, a StackOverflowError is thrown when memory cannot be allocated, either because the stack frame is too large or the vm stack size is too small.

In multi-threaded environment, the larger the memory allocated to the stack of each thread, the larger the space obtained by each thread, the smaller the number of threads that can be created, the more likely it is to produce OOM exceptions. Therefore, it is generally necessary to reduce the maximum heap and reduce the stack capacity to obtain more threads.

9.2.3 Overflow of permanent generation:

The PermGen space, or method area, overflowed. The method area is used to store information about the Class, such as the Class name, access modifiers, constant pools, field descriptions, method descriptions, and so on. Some mainstream frameworks, such as Spring and Hibernate, use bytecode technology like CGLib when enhancing classes. The more classes are enhanced, the larger the method area is needed to ensure that dynamically generated classes can be loaded into memory. In this case, an OOM exception may occur in the method area.

9.2.4 OOM Viewing command
  1. Run the following command to view the process ID:

Such as: the JPS or ps – ef | grep to task

  1. Enter the following command to check the GC status:

Jstat -gcutil Number of milliseconds in which the process ID is refreshed Indicates the number of records displayed

Example: jstat -gcutil 1412 1000 10

  1. Check the specific occupation:

Command: jmap – histo process | more (the default display to the console)

Such as: jmap histo – 1412 | more view specific classname, whether to have the developer’s class, can also be output to a specific file analysis

9.3 100% CPU

Online application leads to 100% CPU usage. In general, the code enters an infinite loop. The analysis steps are as follows:

  1. Find the server process id:

Use ps – ef | grep operation service name, direct top command can also see the CPU usage of each process.

  1. Query the running status of all threads in the target process:

Top-hp pid, -h indicates that the display is displayed in the thread dimension. By default, the display is displayed in the process dimension.

  1. Convert the target thread from base 10 to base 16:

Printf ‘%x\n’ Thread PID

  1. Use jstack process id | grep hexadecimal thread id find thread information, specific analysis:

Jstack process ID | grep – A 20 hexadecimal thread ID

GC tuning

It is sufficient to add XMS and XMX parameters to the general project. Tuning is blind tuning without fully monitoring and collecting performance data. If there is a problem, see if your code or parameters are not reasonable. After all, not everyone can write the underlying code of JVM. GC optimization is usually a last resort to reduce the number of objects created and the use of global variables and large objects. There is much more to tuning code than tuning GC parameters for daily GC analysis. Generally, tuning is not required in the following cases:

1. Minor GC takes less than 50ms and the frequency is more than 10 seconds. So the young generation is OK.

2. If the single time of Full GC is less than 1 second and the frequency is more than 10 minutes, it indicates that the old generation is OK.

The purpose of GC tuning: enough GC time, enough GC frequency.

Tuning suggestions:

  1. -xms5m sets the JVM initial heap to 5M, and -xmx5m sets the JVM maximum heap to 5M. -xms with the same value as -xmx prevents the JVM from reallocating memory after each garbage collection.
  2. -Xmn2g: Sets the size of the young generation to 2G. The default value is 1/3 to 1/4 of the entire heap. -Xss Set stack space per thread.
  3. -xx :SurvivorRatio: sets the ratio of Eden zones to Survivor zones in the young generation. The default value is 8 and the ratio is 8:1:1.
  4. – XX: + HeapDumpOnOutOfMemoryError occurred when the JVM OOM, automatically generate the DUMP file.
  5. – XX: PretenureSizeThreshold when objects created by more than a specified size, the object directly to the distribution in old age.
  6. -xx :MaxTenuringThreshold Sets the maximum age threshold of an object in the Survivor zone. If the age threshold exceeds the threshold, the object will be transferred to the old age. The default value is 15.
  7. Enabling GC logging has little impact on performance and can help us locate problems. -xx :+PrintGCTimeStamps -xx :+PrintGCDetails -xloggc: gC.log