Runtime data area

  1. Program counter
  2. The virtual machine stack
  3. Local method stack
  4. The heap
  5. Methods area

The method area stores content

The bytecode for all fields and methods of the class, as well as special methods such as constructors, and the interface code are also defined here. That is, static variables + constants + class information (constructors/interface definitions) + run-time constant pools all reside in this method area.

Permanent generation and meta-space

  • Permanent generation: permanent generation is replaced by metacolor after jdk1.7. Permanent generation logically belongs to the heap, but physically does not belong to the heap.
  • Metadata: The metadata area replaces the permanent generation. The metadata area is a heap in nature and logical structure similar to the permanent generation. The difference is that the metadata area does not reside in the VIRTUAL machine but uses local physical memory.

This eliminates the need to do a single memory management task for the method area.

GC Root’s object

  1. Object referenced in the virtual machine stack
  2. Objects referenced by static properties in the method area
  3. The object referenced by the method area constant
  4. Objects referenced by JNI in the local method stack
  5. Internal references to the Java virtual machine, such as Class objects for basic data types, some resident exception objects, and system Class loaders
  6. All objects held by the synchronization lock
  7. Jmxbeans that reflect Java virtual machine internals, callbacks registered in JVMTI, local code caches, and so on

Common OOM

  • The heap is out of memory. There are outofMemoryErrors.

Solution: The Eclipse Memory Analyzer is used to analyze heap Dump snapshots to determine whether Memory leaks or overflows. If it is a memory leak, you can look at the reference chain to locate the memory leak code. In case of memory overflow, check whether there is room for upward adjustment between heap parameters and machine memory. In addition, check whether there is any place where the object life cycle is too long, the holding time of the state is too long, and the storage structure is not properly designed.

  • The virtual machine stack and the local method stack overflow
    • StackOverflowError is raised if the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine stack.
    • An OutOfMemoryError is raised if the memory of the virtual stack is allowed to scale dynamically, and a StackOverflowError is raised if sufficient memory cannot be allocated to the extended stack.
  • Method area and runtime constant pool overflow
  • Local direct memory overflow

reference

There are four main types of references in Java: strong reference, soft reference, weak reference, and virtual reference. The different reference types are mainly reflected in GC:

  • Strong reference

If an object has a strong reference, it will not be collected by the garbage collector. Even if the current memory space is insufficient, the JVM does not reclaim it. Instead, an OutOfMemoryError is thrown. Cause a program to abort abnormally. If you want to break the association between a strong reference and an object. You can explicitly assign a reference to NULL so that. The JVM reclaims the object at the appropriate time.

  • Soft references

When using a soft reference, if there is enough memory, the soft reference can continue to be used without being collected by the garbage collector, and only when there is insufficient memory will the soft reference be collected by the garbage collector.

  • A weak reference

Have a weak reference object with short life cycle of the more, because when the JVM garbage collection, if the object is only a weak reference association (no strong references associated him), then the object will be recycled, but due to the garbage collector is a lower priority thread, so do not necessarily can quickly find a weak reference object. Weak references are used to reclaim key/value pairs that are no longer in use. As long as the bucket is alive, it will not be reclaimed.

  • Phantom reference

If an object holds only virtual references, it has no references and can be collected by the garbage collector at any time. The only function is to receive a system notification when the object is collected by the collector to determine the frequency of the garbage collector.

The reason for the different references is Java’s uncontrollable compromise with garbage collection.

Heap memory allocation policy

  • The priority for object allocation is Eden. If the Eden area does not have enough space to allocate objects, the VM executes MinorGC.
  • Large objects go straight into the old age (objects that require a large amount of contiguous memory space). The purpose of this is to avoid a large number of memory copies between Eden region and two Survivor regions (the new generation uses a replication algorithm to collect memory).
  • Long-lived objects enter the old age. The virtual machine defines an Age Count for each object. If the object passes one Minor GC, the object enters the Survivor zone. After each Minor GC, the Age of the object increases by one until it reaches the threshold (15 by default) and the object enters the old zone.
  • Dynamically determine the age of an object. If the sum of the size of all objects of the same age in the Survivor zone is greater than half of the Survivor space, objects older than or equal to that age can go straight to the old age.
  • Space allocation guarantee. Before Minor GC, the virtual machine checks whether the maximum available continuous space of the old age is greater than the total space of all objects of the new generation. If so, Minor GC is performed. Otherwise, check whether HandlePromotionFailure is set to allow guarantee failure, if not, perform Full GC directly; Otherwise check if the maximum contiguous space available for the old age is greater than the average size of objects promoted to the old age, and if so try Minor GC; Otherwise, change to Full GC.

FULL GC trigger condition

  1. Cenozoic space shortage
  2. The permanent generation space is full
  3. Promotion failed and Concurrent mode failure occur during CMS GC
  4. The maximum contiguous space available for the old age is less than the average size of objects promoted to the old age over time

Determines whether the object is alive

There will be two tests. Scan and mark unreachable objects for the first time, and filter objects that do not have Finalize () beforehand or have already called Finalize () to die. The second check determines whether the object is necessary to execute the Finalize () method, which is later executed by the lower priority Finalizer thread. If it is re-referenced before execution, death can be avoided.

Three color marking algorithm

It is an effective method to describe the tracer and can be used to deduce the correctness of the collector.

We classify objects into three types:

  1. Black: The root object, or both the object and its children have been scanned (the object is marked and all of its fields have been marked).
  2. Gray: The object itself is scanned, but children of the object have not been scanned (its field has not been marked or marked).
  3. White: unscanned objects. After scanning all objects, the final white objects are unreachable, that is, garbage objects (objects that are not marked).

Object disappearance problem

  • The assignor inserts one or more new references from the black object to the white object
  • The assignor removes all direct or indirect references from the gray object to the white object

Solution: Incremental update, original snapshot.

Incremental updating

When a black object inserts a new reference to a white object, the newly inserted reference record is recorded. After the concurrent scanning, the black object is rescan based on the black object. That is, once a new reference to a white object is inserted, the black object changes back to a gray object.

The original snapshot

Object to be deleted when the gray to white object references, just telling this to delete a reference to the record, at the end of the concurrent scanning, will record the reference object as the root, the gray to scan again, namely, references to delete or not according to the moment just started scanning the object graph snapshot for search.

Garbage collection algorithm

  • Mark clearing algorithm

Disadvantages: Mark and clear the two actions are not efficient; A large amount of discontinuous space debris would be generated.

  • Replication algorithm

Divide the available memory into two equal chunks by capacity, and use only one of them at a time. When one is used up, the surviving objects are copied onto the other.

Advantages: No fragmentation problems.

Cons: High cost, half as much memory.

  • Tag sorting algorithm

According to the characteristics of old age, this algorithm is proposed. The marking process is still the same as the mark-clean algorithm, but instead of cleaning up the recyclable objects directly, the next step is to move all surviving objects to one end and finally clean up the memory beyond the end boundary.

  • Generational collection algorithm

The new generation uses replication algorithms; The old days used the tag sweep/tag collation algorithm.

Garbage collector

Serial collector

New generation collector, it is the most basic, the development of the longest collector. Is a single-threaded collector that must suspend all other worker threads while garbage is collected until the collection is complete. This mode is suitable for VMS running in Client mode.

Serial Old collector

Older versions of the Serial collector, which is also a single-threaded collector, use a mark-tidy algorithm.

ParNew collector

The new generation collector, a multithreaded version of the Serial collector, is the only one that works with the CMS collector.

Parallel avenge

The new generation of collectors, also using the copy algorithm collector, is a parallel multi-threaded collector. It is more concerned with achieving a manageable throughput. Where throughput = run user code time/(run user code time + garbage collection time).

In addition, it has adaptive tuning, where the virtual machine collects performance monitoring information based on the current system performance and adjusts these parameters dynamically to provide the most appropriate pause times or maximum throughput. This is called GC adaptive tuning strategy. It is an important difference from the ParNew collector.

Parallel Old collector

The Parallel Exploiter is an older version of the Parallel Exploiter, using multithreading and a mark-and-collate algorithm.

CMS collector

The old-era collector, the CMS collector, is a collector whose goal is to obtain the shortest collection pause time, using a mark-sweep algorithm. The recycling process is as follows:

  • Initial tag: You need to Stop The World and tag objects that can be directly associated with it.
  • Concurrent markup: Can work with the user for the process of GC Roots Tracing.
  • Re-marking: You need to Stop The World to correct The marking record of The part of The object that was marked during The concurrent marking because The user program continued to work. The pause is slightly longer than The initial marking and much shorter than The concurrent marking.
  • Concurrent cleanup: Can work with the user.

Advantages: Concurrent collection, low pauses.

Disadvantages: Throughput problem, CMS is sensitive to CPU resources, will occupy a part of the thread, resulting in slow application, reduce throughput; Floating garbage problem, the garbage generated in the concurrent cleaning stage is called floating garbage; Debris problem.

G1 collector

Considered commercially viable at 1.7, it is one of the most advanced developments in collector technology. It is a garbage collector for server-side applications.

Features:

  • Parallelism and concurrency

Multiple cpus can be used to shorten stop-the-world times.

  • Generational collection

You can manage the entire GC heap (G1 divides memory into equally-sized regions and maintains a priority list to reclaim the most valuable regions) with different strategies for objects of different ages.

  • Spatial integration

The overall use of “mark finishing”, the partial use of “copy” algorithm, so there is no fragmentation.

  • Predictable pauses

Collection process:

  • Initial tag: Pauses are required to tag objects to which GC Roots can be directly associated.
  • Concurrent marking: Do reachability analysis to find surviving objects.
  • Final markup: Pauses are required, but can be executed in parallel, to correct the portion of the markup record that changes as the user program continues to operate during the concurrent markup period.
  • Filter collection: Pauses are required to sort the value and cost of collection for each Region, and a collection plan is made based on the expected GC pause time of the user.
  • How to solve the problem caused by cross-region reference after multiple regions are divided into different regions?

Bidirectional card table to maintain, causing memory space to consume an additional 10% to 20% of heap memory.

  • How does the concurrent marking phase ensure that the collection thread and the user thread run without interference?

CMS adopts incremental update algorithm and G1 adopts original snapshot algorithm. G1 designs two Pointers named TAMS for each Region to divide part of the Region space for the allocation of new objects in concurrent reclamation. The addresses of newly allocated objects must be above these two Pointers.

  • How to model predictable pauses?

G1 collector pause prediction model based on the theoretical basis of the mean attenuation, in the process of garbage collection, G collector will record each Region’s recovery time, each Region dirty card number of the memory set each measurable steps such as cost, and analysis it is concluded that the average, standard deviation, confidence and other statistics.

Compared with the CMS

Advantages:

  1. Maximum pause times can be specified
  2. Memory layout based on regions
  3. The collection is dynamically determined by revenue
  4. The whole mark-tidy, local mark-copy feature makes it free of memory space fragmentation

Disadvantages:

  1. The memory usage is high. Each Region maintains a card table
  2. The execution load is high, and G1 needs to use asynchronous queues to handle pre-write barriers and post-write barriers, which are used to update card tables when reference relationships change

Shenandoah collector

A collector that does not exist in the OracleJDK but exists in the OpenJDK and has a high degree of similarity to G1. The main differences are as follows:

  1. Supports concurrent collation algorithms
  2. By default, generational collection is not used. That is, no new generation Region or old generation Region exists
  3. Instead of maintaining a memory set, maintain a connection matrix that records references across regions

Working process:

  • Initial tag

Marks objects directly associated with GC Roots.

  • Concurrent tags

Traversing the object graph, marking all reachable objects, concurrent with the user thread.

  • In the end tag

The remaining SATB scans are processed, the regions with the highest recovery value are counted, and these regions are grouped into a collection group.

  • Concurrent cleaning

Clear a Region with no living objects in the entire Region, concurrent with the user thread.

  • Concurrent collector

Make a copy of the live objects in the collection to another unused Region, concurrent with the user thread.

  • Initial reference update

Corrects all references to old objects in the heap to the copied new address. No specific processing is performed, just to ensure that all collector threads working during the backward phase have completed the object movement task assigned to them.

  • Concurrent reference update

Actually start the reference update operation, concurrent with the user thread. Unlike concurrent tagging, there is no need to search along the object graph, only a linear search for reference types in order of the physical address of memory, changing the old value to the new value.

  • Final reference update

After fixing heap references updates, you also need to fix references that exist in GC Roots.

  • Concurrent cleaning

After the concurrent collection and reference update, there are no living objects in the Region collection. The concurrent clearing process is invoked again to reclaim the memory space of these regions for future allocation of new objects and concurrent with the user thread.

Forwarding pointer

In concurrent reclamation, it is possible that the user thread will be constantly reading and writing the object being moved, so there is a concept of a forward pointer where a new reference field is set in front of the object’s head and the reference points to the object itself without being moved. When you have a new copy, you only need to change the reference address above the old object so that access to the new address can be uniformly forwarded.

However, forward Pointers also face thread-safety problems caused by write problems, i.e. the operation of modifying the old value does not apply to the new copy. In fact, the Shenandoah collector uses CAS operations to ensure that objects are properly accessed at the same time.

ZGC collector

ZGC collector is a garbage collector that gives Region memory layout without generation, uses read barrier, dye pointer and memory multiple mapping technology to achieve concurrent mark-collation algorithm, and takes low latency as the primary goal. Except for almost all concurrency associated with GC Roots. Can reduce any heap pauses to less than 10ms.

Regionshifangle

ZGC regions are divided into three categories:

Small Region: the capacity of a Region is fixed at 2MB. It is used to store small objects smaller than 256KB.

Medium Region: with a fixed capacity of 32MB, it is used to place objects larger than or equal to 256KB but smaller than 4MB.

Large Region: the capacity of a large Region is not fixed and can change dynamically. However, the Region must be a multiple of 2MB and is used to store large objects of 4MB or larger. Only one large object is placed in each large Region.

Dyeing pointer

A staining pointer is a technique for storing a small amount of additional information directly on the pointer.

Advantage:

  1. The dye pointer allows a Region to be freed and reused as soon as its live objects are removed, rather than waiting for all references to the Region in the heap to be corrected.
  2. Significantly reduce the number of memory barriers used during garbage collection.
  3. The dye pointer can be used as an extensible storage structure to record more data related to the object marking and relocation process to further improve performance later.
Virtual memory mapping technology

A prerequisite for coloring Pointers is that the operating system needs to support redefining the bits of the pointer. ZGC on Linux/x86-64 platforms uses multiple mapping to map multiple different virtual memory addresses to the same physical memory address, a many-to-one mapping.

Think of the marker bit of the coloring pointer as a segment of the address, so as long as these different address segments are mapped to the same physical memory space, after multiple mapping transformation, you can use the coloring pointer to address normally.

The working process of the
  • Concurrent tags

The reachable analysis also requires a short pause before and after the initial and final marking similar to G1. The only difference is that the marking stage is not carried out on the object, but updates the Marked 0 and Marked 1 flag bits of the dyeing pointer.

  • Concurrent preparatory reallocation

You can obtain the regions to be cleared based on specific query conditions and form the regions into a redistribution set. Unlike the G1, it scans the full heap instead of accrual-first incremental collection, reducing the cost of maintaining the memory set.

  • Concurrent redistribution

Copy the inventory objects in the reallocation set to the new Region, and maintain a forwarding table for each Region in the reallocation set to record the direction relationship from the old object to the new object. When a user thread concurrently accesses an object in the reallocation set, the access is captured by the preset memory barrier and immediately forwarded to the new copied object according to the forwarding table on Region. At the same time, the value of the reference is modified and updated so that it points directly to the new object. This is called pointer self-healing. The advantage is that only the first access to the old object will get caught in forwarding, which is only once slower.

  • Concurrent remapping

Fixed all references throughout the heap to old objects in the redistribution set. Due to the self-healing nature of Pointers, this phase is not urgent and can be merged into the next concurrent marking phase. Once complete, the original forwarding table can be released.

disadvantage

Cannot afford to have an object allocation rate that is too fast because it is not generational.

Epsilon collector

Instead of collecting garbage, it is only responsible for the management and layout of the heap, the allocation of objects, the coordination with the interpreter, the coordination with the compiler, the coordination with the monitoring subsystem, etc., suitable for services that only run for a few minutes or even seconds.

Steps for creating objects

  • check

Check whether the argument of the instruction can locate a symbolic reference to a class in the constant pool. Check whether the class represented by the symbolic reference has been loaded, parsed, and initialized, and perform class loading if not.

  • Allocate memory

After the class is loaded, the memory required for the object is determined.

  • Pointer to the collision

    In the heap, there is a clear distinction between free and non-free areas, with a pointer in the middle as a pointer to the dividing point, and all that is allocated is to move the pointer to the free space by an equal distance to the object. Suitable for Serial, ParNew and other collectors with Compact procedures.

  • The free list

    It refers to the situation where the used memory and the free memory are interlocked, and Pointers cannot be collided. Instead, it is necessary to maintain a list, record the available and unavailable memory, and update the list when allocating memory. It is applicable to the CMS collector based on the Mark-Sweep algorithm.

Synchronizes memory allocation actions – the VIRTUAL machine uses CAS and retry to ensure atomicity of updates.

  • Initialize the

All allocated memory space is initialized to zero (excluding object headers), which can be initialized before TLAB (local allocation cache) allocation. Initialization guarantees direct use without assigning initial values, and the program can access the zero values corresponding to the data types of these fields.

  • Set up the

Set up the object as necessary, such as which class the object is an instance of, how metadata information can be found, the object’s hash code, the object’s GC generation age, and so on.

  • Perform the init

From the above steps, the virtual machine has already generated an object, but from the Java program’s perspective, object creation is just beginning because (), which initializes the object as the programmer wishes, such as the population of properties, has not yet been performed.

Thread-safety issues caused by memory allocation during object creation

  • CAS+ retry mechanism
  • Local Thread Allocation buffer (TLAB)

Each thread allocates a small chunk of memory in the Java heap, called TLAB, which is locked synchronously only when the local buffer is used up and a new cache is allocated.

Object access location

  • handle

An area in the heap is divided as the handle pool. Reference stores the handle address, which contains the specific address information of object instance data and type data.

Advantage: The handle address is stable, and only the instance data pointer in the handle is needed to change, without modifying reference.

  • Direct Pointers

Reference stores the object address, which contains the object type data in the method area.

Advantages: Saves the time of pointer positioning.

Class loading process

  • loading

The binary byte stream that defines this Class is obtained by the fully qualified name of a Class, and the static storage structure represented by this byte stream is converted into the runtime data structure of the meta space. At the same time, a java.lang.Class object representing this Class is generated in memory, which acts as the access point for the various data of the meta space Class.

  • validation

Ensure that the byte stream in the Class file meets the requirements of the current VM and does not compromise VM security.

  • File format validation

  • Metadata validation

  • Bytecode verification

  • Symbolic reference verification

  • To prepare

Allocates memory for class variables and sets their initial values. Memory is allocated in the meta-space (static modified variables, excluding instance variables), and the initial values allocated are the class initialvalues of each object. (Except for final modified static variables, where the corresponding value is assigned)

  • parsing

The process of replacing symbolic references in a constant pool with direct references.

  • Class or interface resolution
  • Field analytical
  • Class method resolution
  • Interface method parsing

Symbolic reference:

A symbolic reference describes the referenced object as a set of symbols, which can be any literal.

Direct quote:

A direct reference can be a pointer directly to the target, a relative offset, or a handle that can be indirectly located to the target.

  • Initialize the

The final step in the class loading process is to actually execute the Java bytecode defined in the class. (The process of executing the () method, assigning a value to a static variable)

Parent delegation mechanism

The working process of the

  1. Receive a class request;
  2. Request a delegate to the parent class;
  3. When it comes time to start a class, if the desired class is not found, it is passed to the child loader.

The benefits of using parental delegation

  1. To ensure stability, classes have a unified priority hierarchy with class loaders;
  2. A virtual machine determines that two classes are a class only if they have the same class name and are loaded by the same loader. Without parental delegation, it is possible for the same class to be loaded by more than one class loader, so that the class will be recognized as two different classes and will have problems assigning values to each other.

A scenario that breaks the parent delegate mechanism

  • The base class calls the user’s code

A typical example is JNDI services. JNDI is now standard in Java. Its code is loaded by the startup class loader, but JNDI’s purpose is to centrally manage and lookup resources. It requires invoking code from a JNDI interface provider implemented by an independent vendor and deployed in the application’s ClassPath, but it is impossible for the startup class loader to “know” this code.

To solve this problem, the Java team introduced the Thread Context ClassLoader. With thread context loader, the JNDI service can use it to load the required SPI code, which is the parent class loader requested subclasses loader to finish class loading action, this kind of behavior is actually created parents delegation model hierarchy to reverse the use of class loaders, actually has violated the parents delegation model of general principles of But there is nothing you can do about it. Virtually all load actions in Java involving SPIs, such as JNDI, JDBC, JCE, JAXB, and JBI, take this approach.

  • Users’ pursuit of program dynamics

The so-called dynamic refers to the hot replacement of code, hot deployment of modules, etc. In short, it means that the machine can be used as long as it is deployed without restarting.

The key to OSGi’s modular hot deployment is the implementation of its custom classloader mechanism. Each program module (Bundle) has its own class loader. When a Bundle needs to be replaced, the Bundle is replaced with the same class loader to achieve hot replacement of the code. In the OSGi environment, class loaders have evolved from a tree structure in the parent delegate model to a more complex network structure.

Ways to break

Overwriting the loadClass() method in the ClassLoader class does not break the findClass () method, which loads classes that cannot be loaded by the parent ClassLoader.

Tomcat’s class loader

  • CommonLoader: The most basic Tomcat class loader. The classes in the loading path can be accessed by the Tomcat container itself and various WebApps.
  • CatalinaLoader: a private class loader for the Tomcat container. Classes in the loading path are invisible to Webapp.
  • SharedLoader: a class loader shared by webApps. The classes in the loading path are visible to all WebApps, but not to the Tomcat container.
  • WebappClassLoader: A private class loader for each Webapp. The classes in the loading path are visible only to the current Webapp.
  • All classes that can be loaded by CommonClassLoader can be used by Catalina ClassLoader and SharedClassLoader to realize the sharing of public class libraries. Classes that CatalinaClassLoader and Shared ClassLoader can load by themselves are isolated from each other.
  • WebAppClassLoader can use classes that SharedClassLoader loads into, but the WebAppClassLoader instances are isolated from each other.
  • The scope of JasperLoader is only the one from which the JSP file is compiled. The Class file, which appears just to be discarded: When the Web container detects that the JSP file has been modified, it replaces the current JasperLoader instance and implements HotSwap for the JSP file by creating a new JSP classloader.

The parent delegate model requires that all class loaders, except the top-level start class loaders, be loaded by their parent class loaders. Tomcat clearly breaks the parent delegate model. To achieve isolation, each webappClassLoader loads a class file in its own directory and does not pass it to the parent class loader. If Tomcat’s Common ClassLoade wants to load classes in WebApp ClassLoader, it can do so using the thread context loader.

JVM performance tuning

Common commands

  • jps

Displays all HotSpot VIRTUAL machine processes in the system.

  • jstat

Monitor vm statistics.

  • jinfo

Java configuration information tool.

  • jmap

Java memory image tools.

  • jhat

Vm heap dump snapshot analysis tool.

  • jstack

Java stack trace tool.

  • HSDIS

JIT generates code disassembly.

Commonly used tools

  • JConsole

Java monitoring and Administration console.

  • VsualVM

All-in-one troubleshooting tool.

Commonly used parameters

Parameters that

  • Standard parameters (-), which must be implemented by all JVM implementations and backward compatible;
  • Nonstandard parameters (-x), which are implemented by the default JVM but are not guaranteed by all JVM implementations and are not guaranteed to be backward compatible;
  • Non-stable parameter (-xx), which may vary from JVM implementation to JVM implementation and may be cancelled at any time in the future, so use with caution.

Use “+” or “-” before the option name to enable or disable a specific option. For example:

-xx :+UseCompressedOops: enables the compression pointer

-xx: -usecompressedoops: disables the compression pointer

Common Parameter Settings

  • -Xms: indicates the initial heap size
  • -Xmx: indicates the maximum heap size
  • -xMN: Cenozoic size
  • -Xss: specifies the vm stack size for each thread
  • -xx :NewSize: indicates the initial size of the new generation
  • -xx :NewSize: indicates the maximum value of the new generation
  • -xx :MetaspaceSize: Sets the initial size of the metadata space (instead of -xx :PermSize)
  • -xx :MaxMetaspaceSize: Set the maximum metadata space (replace previous -xx :MaxPermSize)
  • -xx :+PrintGCDetails: Prints GC information