The most complete JVM in history. After reading the interview, the big factory offer is in hand

Posted May 25, 202022 min read

Java memory area

Talk about the main components of the JVM and their role?

\ * \ *

\ * \ *

The JVM contains two subsystems and two components. The two subsystems are Class loader and Execution engine; the two components are Runtime data area and Native Interface.

  • Class loader(class loading):according to the given fully qualified class name(such as:java.lang.Object) to load the class file to the method area in the Runtime data area.
  • Execution engine(execution engine):execute instructions in classes.
  • Native Interface(local interface):interact with native libraries, is an interface for other programming languages.
  • Runtime data area(runtime data area):This is what we often say the JVM memory.

Function:First, the Java code is converted into byte code by the compiler, and then the class loader(ClassLoader) loads the byte code into the memory and places it in the runtime data area(Runtime data area) In the area, the bytecode file is just a set of instruction set specifications of the JVM, and cannot be directly handed over to the underlying operating system for execution. Therefore, a specific command parser execution engine(Execution Engine) is required to translate the bytecode into the underlying system The instructions are then handed over to the CPU for execution. In this process, the Native Interface of other languages needs to be called to implement the functions of the entire program.

The following is a detailed description of the Java program operating mechanism

Java program operation mechanism steps

  • First, use the IDE integrated development tools to write Java source code, the source file suffix is .java;
  • Then use the compiler(javac command) to compile the source code into a bytecode file, the suffix of the bytecode file is .class;
  • The work of running bytecode is done by the interpreter(java command).

As you can see from the picture above, the java file is turned into a .class file by the compiler, and then the class loader loads these .class files into the JVM.

In fact, it can be explained in one sentence:class loading refers to reading the binary data in the class's .class file into memory, placing it in the method area of the runtime data area, and then creating a java in the heap area. The lang.Class object is used to encapsulate the data structure of the class in the method area.

Talk about the JVM runtime data area

The Java virtual machine divides the memory area it manages into several different data areas during the execution of the Java program.

These areas have their own purposes, as well as the time of creation and destruction. Some areas exist with the start of the virtual machine process, and some areas are created and destroyed depending on the start and end of the thread. The memory managed by the Java virtual machine is divided into the following areas:

The runtime data area of different virtual machines may be slightly different, but all will comply with the Java Virtual Machine Specification. The area specified by the Java Virtual Machine Specification is divided into the following five parts:

  • Program Counter Register:The line number indicator of the bytecode executed by the current thread. The job of the bytecode parser is to select the next bytecode instruction to be executed by changing the value of this counter. Basic functions such as branching, looping, jumping, exception handling, and thread recovery all need to rely on this counter to complete;
  • Java Virtual Machine Stack(Java Virtual Machine Stacks):used to store local variable table, operand stack, dynamic link, method exit and other information;
  • Native method stack(Native Method Stack):The role is the same as the virtual machine stack, except that the virtual machine stack serves Java methods, and the local method stack is for the virtual machine to call the Native method service;
  • Java Heap(Java Heap):The largest piece of memory in the Java virtual machine is shared by all threads, and almost all object instances are allocated memory here;
  • Method area(Methed Area):used to store data such as class information, constants, static variables, and just-in-time compiled code that have been loaded by the virtual machine.

Deep copy and shallow copy

Shallow Copy(shallowCopy) just adds a pointer to an existing memory address,

Deep Copy(deepCopy) is to add a pointer and apply for a new memory, so that the increased pointer points to the new memory,

When using deep copy, the memory will not be released because of the error that the same memory is released when the shallow copy occurs.

Shallow copy:just point to the memory address to be copied, if the original address changes, then the object copied from the shallow copy will also change accordingly.

Deep copy:open up a new memory address in the computer to store the copied object.

Talk about the difference between stacks?

Physical address

The physical address allocation of the heap is not continuous for the object. So the performance is slower. In GC, discontinuous allocation should also be considered, so there are various algorithms. For example, mark-eliminate, copy, mark-compress, and generation(that is, the new generation uses the copy algorithm, the old generation uses mark-compress)

The stack uses the stack in the data structure. The principle of first-in, first-out, physical address allocation is continuous. So performance is fast.

Memory separately

Because the heap is not continuous, the allocated memory is confirmed at runtime, so the size is not fixed. Generally the heap size is much larger than the stack.

The stack is continuous, so the size of the allocated memory must be confirmed at compile time, and the size is fixed.

Store contents

The heap stores instances and arrays of objects. Therefore, the area is more concerned about data storage

Stack storage:local variables, operand stack, and return result. This area is more concerned with the execution of procedural methods.

PS:

  • Static variables are placed in the method area
  • Static objects are still placed on the heap.

Visibility of the program

The heap is shared and visible to the entire application.

The stack is only visible to threads. Therefore, the thread is private. His life cycle is the same as the thread.

What are queues and stacks? What's the difference?

Both queues and stacks are used to pre-store data.

  • The name of the operation is different. The insertion of the queue is called enqueue, and the deletion of the queue is called dequeue. The insertion of the stack is called pushing into the stack, and the deletion of the stack is called pulling out of the stack.
  • The operable method is different. The queue enters the team at the end of the team and leaves the team at the head, that is, both sides can operate. The stack is pushed into and out of the stack at the top of the stack, and the bottom of the stack cannot be directly manipulated.
  • The method of operation is different. The queue is first-in-first-out(FIFO), that is, the modification of the queue is based on the first-in first-out principle. New members always join the tail of the team(cannot be inserted from the middle), and the members who leave each time are always on the head of the queue(no team leave is allowed). The stack is last in first out(LIFO), that is, each time you delete(out of the stack) is always the latest element in the current stack, that is, the last element inserted(into the stack), and the first inserted is placed at the bottom of the stack, It cannot be deleted until the end.

HotSpot Virtual Machine Object Quest

Creation of objects

Speaking of object creation, let us first look at several object creation methods provided in Java:

The following is the main process of object creation:

When the virtual machine encounters a new instruction, it first checks whether the constant pool has loaded the corresponding class. If not, it must first perform the corresponding class loading. After the class is loaded, memory is allocated next. If the memory in the Java heap is absolutely regular, use the "pointer collision" method to allocate memory; if it is not regular, it is allocated from the free list, called the "free list" method. There is one more problem to consider when dividing memory-concurrency, there are also two ways:CAS synchronization processing, or local thread allocation buffer(Thread Local Allocation Buffer, TLAB). Then initialize the memory space, then do some necessary object settings(meta information, hash code ...), and finally execute the method.

allocating memory for objects

After the class loading is completed, a memory is allocated to the object in the Java heap. There are two ways to allocate memory according to whether the Java heap is regular:

  • Pointer collision:If the memory of the Java heap is regular, that is, all used memory is placed on one side, and the free one is placed on the other side. When allocating memory, move the pointer indicator in the middle to the free memory for a distance equal to the size of the object, so that the work of allocating memory is completed.
  • Free list:If the memory of the Java heap is not regular, you need to maintain a list by the virtual machine to record which memory is available, so that you can query from the list when the allocation is large enough to allocate to the object, and in Update list records after allocation.

The choice of allocation method is determined by whether the Java heap is regular, and whether the Java heap is regular is determined by whether the garbage collector used has a compression and collation function.

Handling concurrent security issues

Object creation is a very frequent behavior in a virtual machine. Even if only the location pointed to by a pointer is modified, it is not safe in the case of concurrency. It may appear that memory is being allocated to object A. The pointer has not yet been modified. At the same time, the original pointer is used to allocate memory. There are two solutions to this problem:

  • Synchronize the actions of allocating memory space(using CAS + failed retry to ensure the atomicity of the update operation);
  • The memory allocation is performed in different spaces according to thread division, that is, each thread is pre-allocated a small piece of memory in the Java heap, called local thread allocation buffer(Thread Local Allocation Buffer, TLAB). Which thread needs to allocate memory is allocated on the TLAB of which thread. Only when TLAB is used up and a new TLAB is allocated, a synchronization lock is required. Use the -XX:+/- UserTLAB parameter to set whether the virtual machine uses TLAB.

Access to the object

Java programs need to access specific objects in the heap through references on the JVM stack. The access method of the object depends on the implementation of the JVM virtual machine. At present, the mainstream access methods are handle and direct pointer.

  1. Pointer:Point to an object, representing the starting address of an object in memory.
  2. Handle:It can be understood as a pointer to a pointer, which maintains the pointer of the object. The handle does not directly point to the object, but a pointer to the object(the handle does not change, pointing to a fixed memory address), and then the object's pointer points to the real memory address of the object.

Handle access

The Java heap is divided into a memory as a handle pool, the handle address of the object stored in the reference, and the handle contains the specific address information of the object instance data and object type data, the specific structure is shown in the following figure:

Advantages:The reference stores a stable handle address. When the object is moved(moving an object during garbage collection is a very common behavior), only the instance data pointer in the handle is changed, and the reference itself does not need to be modified.

Direct pointer

If you use direct pointer access, the address stored directly in the reference is the object address, then the layout inside the Java heap object must consider how to place relevant information about the access type data.

Advantages:It is faster and saves the time overhead of pointer positioning. Since object access is very frequent in Java, this type of overhead product is also a considerable execution cost. This is the method used in HotSpot.

Memory overflow exception

Will there be a memory leak in Java? Please describe briefly.

Memory leak means that objects or variables that are no longer in use are always occupied in memory. In theory, Java has a GC garbage collection mechanism, that is, objects that are no longer used will be automatically collected by GC and automatically removed from memory.

However, even so, Java still has a memory leak. The reason for java's memory leak is very clear:a long-lived object holding a reference to a short-lived object is likely to cause a memory leak, although short-lived objects have No longer needed, but because the long-life object holds its reference and cannot be recycled, this is the scene of memory leaks in java.

Garbage Collector

A brief description of the Java garbage collection mechanism

In Java, the programmer does not need to explicitly release the memory of an object, but is executed by the virtual machine itself. In the JVM, there is a garbage collection thread, which is of low priority and will not be executed under normal circumstances. Only when the virtual machine is idle or the current heap memory is insufficient, the execution will be triggered. Reference the objects and add them to the collection to be recycled for recycling.

What is GC? Why GC

GC means garbage collection(Gabage Collection), memory processing is where programmers are prone to problems, forgotten or wrong memory

Recycling can cause instability or even crash of the program or system. The GC function provided by Java can automatically monitor whether the object exceeds the scope to achieve automatic

For the purpose of reclaiming memory, the Java language does not provide a display operation method for freeing allocated memory.

Advantages and principles of garbage collection. And consider 2 recycling mechanisms

The most remarkable feature of java language is the introduction of garbage collection mechanism, which makes java programmers no longer consider the problem of memory management when writing programs.

Because of this garbage collection mechanism, objects in java no longer have the concept of "scope", only objects that are referenced have "scope".

The garbage collection mechanism effectively prevents memory leaks and can effectively use available memory.

The garbage collector usually runs as a single low-level thread, and unpredictably cleans and recycles objects in the memory heap that have died or have not been used for a long time.

Programmers cannot call the garbage collector on an object or all objects for garbage collection in real time.

Garbage collection includes generational copying garbage collection, marked garbage collection, and incremental garbage collection.

What is the basic principle of garbage collector? Can the garbage collector reclaim memory immediately? Is there any way to actively notify the virtual machine for garbage collection?

For GC, when a programmer creates an object, the GC begins to monitor the address, size, and usage of the object.

Generally, GC uses a directed graph to record and manage all objects in the heap. In this way, determine which objects are "reachable" and which objects are "unreachable." When the GC determines that some objects are "unreachable", the GC is responsible for reclaiming these memory spaces.

can. The programmer can manually execute System.gc() to inform the GC to run, but the Java language specification does not guarantee that the GC will be executed.

What reference types are available in Java?

  • Strong reference:It will not be recycled when gc occurs.
  • Soft reference:useful but not necessary objects will be collected before memory overflow occurs.
  • Weak reference:useful but not necessary objects, will be recycled in the next GC.
  • Virtual reference(ghost reference/phantom reference):The object cannot be obtained through the virtual reference. Use PhantomReference to implement the virtual reference. The purpose of the virtual reference is to return a notification when gc.

How to judge whether the object can be recycled?

When the garbage collector is doing garbage collection, the first thing to determine is which memory needs to be recycled, and which objects are "live" and cannot be recycled; which objects have "dead" and need to be recycled .

There are generally two ways to judge:

  • Reference counter method:Create a reference count for each object. When there is an object reference, the counter is +1. When the reference is released, the counter is -1. When the counter is 0, it can be recycled. It has a disadvantage that it cannot solve the problem of circular references;
  • Reachability analysis algorithm:search down from GC Roots, and the path that the search traverses is called the reference chain. When an object is connected to GC Roots without any reference chain, it proves that the object can be recycled.

In Java, when can an object be garbage collected

When the object becomes inaccessible to the application that currently uses the object, the object can be recycled.

Garbage collection does not occur in the permanent generation. If the permanent generation is full or exceeds the threshold, a full garbage collection(Full GC) will be triggered. If you look closely at the output of the garbage collector, you will find that the permanent generation is also recycled. This is why the correct permanent generation size is very important to avoid Full GC.

Will garbage collection occur in the permanent generation in the JVM

Garbage collection does not occur in the permanent generation. If the permanent generation is full or exceeds the threshold, a full garbage collection(Full GC) will be triggered. If you look closely at the output of the garbage collector, you will find that the permanent generation is also recycled. This is why the correct permanent generation size is very important to avoid Full GC. Please refer to Java8:From Permanent Generation to Metadata Area

(Translator's Note:The permanent generation has been removed in Java 8 and a native memory area called the metadata area has been added)

Talk about what garbage collection algorithms the JVM has?

  1. Mark-sweep algorithm:mark useless objects, then clear and recycle. Disadvantages:the efficiency is not high, and garbage debris cannot be removed.
  2. Copy algorithm:divide two memory areas of equal size according to the capacity, when a block is used up, copy the live objects to another block, and then clean up the used memory space once. Disadvantages:memory usage is not high, only half of the original.
  3. Marking-sorting algorithm:mark useless objects, let all surviving objects move to one end, and then directly clear the memory beyond the end boundary.
  4. Generational algorithm:divide the memory into several blocks according to the different life cycle of the object, generally the new generation and the old generation, the new generation basically adopts the replication algorithm, and the old generation adopts the tag sorting algorithm.

Mark-Clear Algorithm

Mark useless objects, then clear and recycle.

Mark-sweep algorithm(Mark-Sweep) is a common basic garbage collection algorithm, it divides garbage collection into two stages:

Marking stage:mark the objects that can be recycled.

Cleaning phase:Reclaim the space occupied by the marked objects.

The mark-and-sweep algorithm is basic because the garbage collection algorithms described later are improved on the basis of this algorithm.

Advantages:simple implementation, no need to move objects.

Disadvantages:The efficiency of the marking and cleaning process is low, a large number of discontinuous memory fragments are generated, and the frequency of garbage collection is increased.

The execution process of the mark-clear algorithm is shown in the figure below

Copy Algorithm

In order to solve the problem of the inefficiency of the mark-and-sweep algorithm, a copy algorithm was produced. It divides the memory space into two equal areas, using only one area at a time. During garbage collection, traverse the currently used area, copy the surviving objects to another area, and finally collect the recyclable objects of the currently used area.

Advantages:Allocate memory in sequence, simple to implement, efficient to run, without considering memory fragmentation.

Disadvantages:The available memory size is reduced to half of the original, and the object is frequently copied when the survival rate is high.

The execution process of the copy algorithm is shown in the figure below

Mark-Organize Algorithm

The replication algorithm can be used in the new generation, but the replication algorithm cannot be selected in the old generation, because the survival rate of objects in the old generation will be higher, so there will be more copy operations, resulting in lower efficiency. The mark-sweep algorithm can be applied in the old generation, but it is not efficient, and it is easy to generate a large amount of memory fragments after the memory is reclaimed. Therefore, there is a Mark-Compact algorithm, which is different from the Mark-Compact algorithm in that after the recyclable objects are marked, all surviving objects are compressed to one end of the memory, so that they are arranged compactly. Together, and then reclaim the memory beyond the end boundary. After recycling, the used and unused memory are on each side.

Advantages:Solved the problem of memory fragmentation in the mark-clean algorithm.

Disadvantages:The local object still needs to be moved, which reduces the efficiency to a certain extent.

The execution process of the mark-sort algorithm is shown in the following figure

Generation collection algorithm

The current commercial virtual machines all use the garbage collection algorithm of generational collection. Generational collection algorithm, as the name suggests, divides the memory into several blocks according to the life cycle of the object. Generally includes young generation, old generation and permanent generation, as shown in the figure:

Talk about what garbage collectors the JVM has?

If the garbage collection algorithm is a method of memory recycling, then the garbage collector is the specific implementation of memory recycling. The following figure shows 7 kinds of collectors that work on different generations. Among them, the collectors used to recycle the new generation include Serial, PraNew, and Parallel Scavenge. The collectors used to recycle the old generation include Serial Old, Parallel Old, CMS, and G1 collector that recycles the entire Java heap. The connection between different collectors means that they can be used together.

Serial collector(replication algorithm):The new generation of single-threaded collector, marking and cleaning are single-threaded, the advantage is simple and efficient;

  • ParNew collector(replication algorithm):The new generation collection parallel collector is actually a multi-threaded version of the Serial collector, which has better performance than Serial in a multi-core CPU environment;
  • Parallel Scavenge collector(replication algorithm):A new generation of parallel collector, pursuing high throughput and efficient use of CPU. Throughput = user thread time/(user thread time + GC thread time), high throughput can efficiently use CPU time, complete the computing task of the program as soon as possible, suitable for background applications and other scenarios that do not require high interaction;
  • Serial Old collector(marker-sorting algorithm):old-generation single-threaded collector, old-generation version of Serial collector;
  • Parallel Old collector(marker-sorting algorithm):old generation parallel collector, throughput priority, old generation version of Parallel Scavenge collector;
  • CMS(Concurrent Mark Sweep) collector(mark-sweep algorithm):the old generation parallel collector, the collector that aims to obtain the shortest recovery pause time, has the characteristics of high concurrency and low pause, and pursues the shortest GC recovery pause time.
  • G1(Garbage First) collector(marker-sorting algorithm):Java heap parallel collector, G1 collector is a new collector provided by JDK1.7, G1 collector is based on the "marker-sort" algorithm, that is to say No memory fragmentation will occur. In addition, an important feature of the G1 collector is different from the previous collector:the scope of G1 collection is the entire Java heap(including the new generation, the old generation), and the scope of the first six collectors collection is limited to the new generation or old generation .

Introduce the CMS garbage collector in detail?

CMS is the abbreviation of English Concurrent Mark-Sweep. It is a garbage collector with the shortest collection pause time at the expense of throughput. For applications that require server response speed, this garbage collector is very suitable. Add "-XX:+ UseConcMarkSweepGC" to the parameter of starting JVM to specify the use of CMS garbage collector

CMS uses the mark-and-clear algorithm to achieve, so a large amount of memory fragments are generated during gc. When the remaining memory cannot meet the program running requirements, the system will have Concurrent Mode Failure, and the temporary CMS will use the Serial Old collector With garbage removal, performance at this time will be reduced.

What are the new generation garbage collector and the old generation garbage collector? What's the difference?

New Generation Recyclers:Serial, ParNew, Parallel Scavenge

Old generation recycler:Serial Old, Parallel Old, CMS

The whole heap collector:G1

The new generation garbage collector generally adopts the replication algorithm. The advantage of the replication algorithm is high efficiency, and the disadvantage is the low memory utilization; the old generation collector generally adopts the mark-sort algorithm for garbage collection.

A brief description of how the generational garbage collector works?

The generational recycler has two partitions:the old generation and the new generation. The default space of the new generation accounts for 1/3 of the total space, and the default ratio of the old generation is 2/3.

The new generation uses a replication algorithm. There are 3 partitions in the new generation:Eden, To Survivor, From Survivor, and their default ratio is 8:1:1. Its execution process is as follows:

  • Put the surviving objects of Eden + From Survivor into the To Survivor area;
  • Clear the Eden and From Survivor partitions;
  • From Survivor and To Survivor partition exchange, From Survivor becomes To Survivor, To Survivor becomes From Survivor.

For objects that survive each time you move from From Survivor to To Survivor, the age is +1. When the age reaches 15(the default configuration is 15), it is upgraded to the old generation. Large objects will also enter the old generation directly.

In the old generation, when the space occupation reaches a certain value, global garbage collection will be triggered. Generally, the execution algorithm of mark arrangement is used. The above loops constitute the overall execution process of the entire generational garbage collection

Memory Allocation Strategy

A brief introduction to java memory allocation and recovery strategy and Minor GC and Major GC

The so-called automatic memory management, the final solution is the two problems of memory allocation and memory recycling. Earlier we introduced memory recycling, here we talk about memory allocation again

The memory allocation of objects is usually allocated on the Java heap(with the birth of virtual machine optimization technology, it will also be allocated on the stack in some scenarios, which will be described in detail later). The local thread buffer will be allocated on TLAB according to the thread priority. In a few cases, it will be distributed directly in the old generation. In general, the allocation rules are not 100%fixed, and the details depend on which combination of garbage collector and virtual machine related parameters, but the allocation of memory by the virtual machine will still follow the following "universal" rules:

Objects are preferentially allocated in the Eden area

In most cases, objects are allocated in the new generation Eden area. When there is not enough space for allocation in the Eden area, the virtual machine will initiate a Minor GC. If there is still not enough space after this GC, the allocation guarantee mechanism will be enabled to allocate memory in the old generation.

Here we mentioned Minor GC. If you have carefully observed the daily routine of GC, usually we can also find Major GC/Full GC from the log.

  1. Minor GC refers to the GC that occurs in the new generation, because most of Java objects are dying, all Minor GC is very frequent, and the general recovery speed is also very fast;
  2. Major GC/Full GC refers to the GC that occurred in the old generation. Major GC is usually accompanied by at least one Minor GC. Major GC is usually more than 10 times slower than Minor GC.

Large objects go directly into the old generation

The so-called large object refers to an object that requires a lot of continuous memory space. Frequent occurrence of large objects is fatal, which will cause the GC to be triggered in advance to obtain enough continuous space to place new objects when there is still a lot of space in the memory.

Earlier we introduced that the new generation uses a mark-sweep algorithm to handle garbage collection. If large objects are allocated directly in the new generation, it will cause a large amount of memory replication between the Eden area and the two Survivor areas. Therefore, large objects will be allocated directly in the old generation.

Long-term survival objects will enter the old generation

The virtual machine uses the idea of generational collection to manage memory, so when reclaiming memory, you must determine which objects should be placed in the new generation and which objects should be placed in the old generation. Therefore, the virtual machine defines an object age counter for each object. If the object is born in the Eden area and can be accommodated by Survivor, it will be moved to the Survivor space. At this time, the object age is set to 1. Each time the subject "survives" Minor GC in the Survivor area, the age increases by 1. When the age reaches a certain level(default 15), the subject will be promoted to the old age.

Virtual machine class loading mechanism

A brief description of java class loading mechanism?

The virtual machine loads the data describing the class from the Class file into memory, and verifies, parses, and initializes the data, and finally forms a java type that can be directly used by the virtual machine.

Describe the principle and mechanism of the JVM loading Class files

All classes in Java need to be loaded into the JVM by the class loader to run. The class loader itself is also a class, and its job is to read the class file from the hard disk into memory. When writing programs, we hardly need to care about class loading, because these are implicitly loaded, unless we have special usage, such as reflection, we need to explicitly load the required classes.

There are two types of class loading methods:

  1. Implicit loading, when the program encounters an object generated by new and other methods during operation, the class loader is implicitly called to load the corresponding class into the jvm,

  2. Explicit loading, through class.forname() and other methods, explicitly load the required

Java class loading is dynamic. It does not load all classes at once and then runs, but ensures that the basic class(such as the base class) of the program is fully loaded into the jvm. As for other classes, you need to Before loading. This is of course to save memory overhead.

What are class loaders and what are the class loaders?

The code block that realizes the binary byte stream of the class through the name of the class is called the class loader

There are four main types of loader:

  1. The boot class loader(Bootstrap ClassLoader) is used to load the java core class library and cannot be directly referenced by the java program.
  2. Extensions class loader(extensions class loader):It is used to load Java extension libraries. The implementation of the Java Virtual Machine will provide an extended library directory. The class loader finds and loads Java classes in this directory.
  3. System class loader:It loads Java classes according to the classpath(CLASSPATH) of Java applications. In general, Java application classes are loaded by it. It can be obtained by ClassLoader.getSystemClassLoader().
  4. The user-defined class loader is implemented by inheriting the java.lang.ClassLoader class.

Talk about the execution process of class loading?

Class loading is divided into the following 5 steps:

  1. Load:find the corresponding class file according to the search path and import it;
  2. Verification:Check the correctness of the loaded class file;
  3. Preparation:Allocate memory space for static variables in the class;
  4. Resolution:The process in which the virtual machine replaces symbol references in the constant pool with direct references. A symbolic reference is understood as a label, and a direct reference directly points to an address in memory;
  5. Initialization:Initialize static variables and static code blocks.

What is the parent delegation model?

Before introducing the parent delegation model, let's talk about the class loader. For any class, it is necessary to establish the uniqueness in the JVM by the class loader that loads it and the class itself. Each class loader has an independent class namespace. The class loader is to load the class file into the JVM memory according to the specified fully qualified name, and then convert it into a class object.

Class loader classification:

  1. The Bootstrap ClassLoader is a part of the virtual machine itself, used to load the class library in the Java \ _HOME/lib/directory or the path specified by the -Xbootclasspath parameter and recognized by the virtual machine;
  2. Other class loaders:
  3. Extension class loader(Extension ClassLoader):responsible for loading all the libraries in the path specified by the \ lib \ ext directory or the Java. Ext. Dirs system variable;
  4. Application ClassLoader. Responsible for loading the specified class library on the user classpath(classpath), we can use this class loader directly. In general, if we do not have a custom class loader, this loader is used by default.

Parent delegation model:If a class loader receives a class loading request, it will not first load the class itself, but delegates the request to the parent class loader to complete, each class loading This is true for all loaders, so that all load requests will be transferred to the top-level startup class loader. Only when the parent load cannot complete the load request(the required class is not found in its search scope), the child loader will Try to load the class.

When a class receives a class loading request, it will not load this class first, but will delegate it to the parent class, and the parent class will load it. To complete the class loading.

JVM tuning

Talk about JVM tuning tools

JDK comes with a lot of monitoring tools, which are located in the bin directory of JDK. Among them, the most commonly used view monitoring tools are jconsole and jvisualvm.

  1. jconsole:used to monitor the memory, threads and classes in the JVM;
  2. jvisualvm:JDK's all-round analysis tool that can analyze:memory snapshots, thread snapshots, program deadlocks, monitor memory changes, gc changes, etc.

What are the commonly used JVM tuning parameters?

  • \ -Xms2g:the initial push size is 2g;
  • \ -Xmx2g:the maximum heap memory is 2g;
  • \ -XX:NewRatio = 4:Set the memory ratio of young and old generation to 1:4;
  • -XX:SurvivorRatio=8 Eden Survivor 8:2
  • XX:+UseParNewGC ParNew + Serial Old
  • -XX:+UseParallelOldGC ParNew + ParNew Old
  • -XX:+UseConcMarkSweepGC CMS + Serial Old
  • -XX:+PrintGC gc
  • -XX:+PrintGCDetails gc

gif5  (1).gif