Someone finally made it clear what JVM synchronization is, and I benefited a lot!

JVM's memory model and JVM's garbage collection mechanism have always been topics that practitioners in the Java industry can't avoid (actual tuning, interview). Let's learn about the JVM's garbage collection mechanism. As a Java practitioner, if you do not master these knowledge points, it may be difficult to skip the stage of advanced to architect.
Insert picture description here

How do you understand synchronization?
Answer: Synchronization is used to control the access of shared resources between multiple threads to ensure that only one thread can access this resource at the same time. In an asynchronously protected multithreaded program, when a thread is modifying a shared variable, another thread may also be using or updating its value. Synchronization avoids the generation of dirty data.

Synchronize the method:

public synchronized void Method1 () {
    
    
// Appropriate method-related code.
}

Synchronize the code block inside the method:

public myFunction (){
    
    
    synchronized (this) {
    
    
            // Synchronized code here.
         }
}

Sun HotSpot VM is a virtual machine that comes with JDK and Open JDK, and is currently the most widely used Java virtual machine.

JVM memory distribution

Program counter: It is a small memory space and can be regarded as a line number indicator of the bytecode executed by the current thread. Basic functions such as branches, loops, jumps, exception handling, and thread recovery in the program all need to rely on this counter to complete. Since multithreading is realized by the way that threads alternately switch and allocate processor execution time, this area is thread private memory.
Virtual machine stack: describes the memory model of Java method execution, used to store local variable tables, operand stacks, dynamic links, method exits and other
heaps: it is the largest piece of memory managed by the Java virtual machine. The Java heap is A memory area shared by all threads. It is created when the virtual machine starts to store all instances. It is also the main
method area for garbage collector management : it is used to store the class information, constants, static variables, and instant compilation that have been loaded by the virtual machine. Code and other data. HotSVM also performs GC for this area, mainly constant recovery and class

JVM memory allocation strategy
The memory allocation of objects, in the general direction, is allocated on the Java heap.
In most cases, objects are allocated in the young generation Eden area. When the Eden area does not have enough space for allocation, the virtual machine will initiate a Minor GC.
In most cases, large objects directly enter the old age, and the virtual machine provides parameters to define the threshold of the large object. Objects that exceed the threshold will directly enter the old age.
Objects that survive multiple Minor GCs (long-term surviving objects) will enter the old age. The virtual machine provides parameters for setting thresholds.

JVM Garbage Collection Algorithm
Mark-sweep algorithm: first mark all objects that need to be recycled, and collect all marked objects uniformly after marking.
Copy algorithm: Divide the available memory into two pieces of equal size according to capacity, and only use one of them at a time. When one piece of memory is used up, another one will be saved, and the used memory space will be cleaned up at once.
Marking-sorting algorithm: The marking process is the same as the "marking-sweeping" algorithm, but the subsequent steps are not to directly clean up the recyclable objects, but to move one end, and then directly clean up the memory outside the end boundary.
Generational collection algorithm: Generally, the Java heap is divided into the new generation and the old generation, and the most appropriate collection algorithm is adopted according to the characteristics of each generation. The new generation has found that a large number of objects have died, and the replication algorithm is chosen. In the old age, because of the high survival rate of objects, the "mark-clean" or "mark-sort" algorithm must be used for recycling.

Garbage Collector
Serial Collector: It is a single-threaded collector that only uses one CPU or one collection thread to complete the garbage collection work. During garbage collection, all other worker threads must be suspended until the collection ends.
ParNew collector: It is a multi-threaded version of the Serial collector. Except for using multiple threads for garbage collection, the other behaviors are exactly the same as the Serial collector.
CMS collector: It is a collector that aims to obtain the shortest recovery pause time. The process is divided into the following four steps:
initial marking,
concurrent marking,
remarking,
concurrent removal

JVM common startup parameters
-Xms / -Xmx-the initial size of the heap / the maximum size of the
heap
-Xmn- the size of the young generation in the heap -XX:-DisableExplicitGC-let System.gc() have no effect
-XX:+PrintGCDetails- Print the details of GC-
XX:+PrintGCDateStamps — Print the timestamp of the GC operation-
XX:NewSize / XX:MaxNewSize — Set the young generation size/maximum size of the young generation-
XX:NewRatio — You can set the ratio of the old and young generations-
XX :PrintTenuringDistribution — Set the age distribution of objects in the survivor’s paradise after each Cenozoic GC output
-XX:InitialTenuringThreshold / -XX:MaxTenuringThreshold: Set the initial and maximum values ​​of the threshold for the old generation
-XX:TargetSurvivorRatio: Set the target of the survival area Usage rate

JAVA class life cycle The
Java class starts from being loaded into the memory of the virtual machine to being unloaded from the memory. Its entire life cycle includes seven stages: loading, verification, preparation, analysis, initialization, use, and unloading.

JVM class loading
Bootstrap class loader: It is a class loader implemented with native code, which is responsible for loading the class libraries under <Java_Runtime_Home>/lib into memory (such as rt.jar). Since the boot class loader involves the details of the local implementation of the virtual machine, developers cannot directly obtain a reference to the boot class loader, so direct operations by reference are not allowed.
Standard extension (Extension) class loader: It is implemented by Sun's ExtClassLoader (sun.misc.Launcher$ExtClassLoader) that the class library in the location specified by Java_Runtime_Home >/lib/extjava.ext.dir is loaded into memory. Developers can directly use the standard extended class loader.

System class loader: It is implemented by Sun's AppClassLoader (sun.misc.Launcher$AppClassLoader). The class library specified in the CLASSPATH is loaded into the memory. Developers can directly use the system class plus
parent delegation mechanism to describe: when a particular class loader receives a request to load a class, it first delegates the loading task to the parent class loader, recursively, if the parent class loader can complete The class loading task will return successfully; only when the parent class loader cannot complete the loading task, it will load itself.

JVM tuning
View heap space allocation (young generation, old generation, persistent generation allocation)
garbage collection monitoring (long-term monitoring of the collection situation)
Thread information monitoring: the number of system threads
Thread status monitoring: what state each thread is in Next
Thread details: check the internal running status of the thread, deadlock check
CPU hot spots: check which methods of the system take up a lot of CPU time
Memory hot spots: check which objects have the largest number in the system

Off topic

Due to too much information, this article is limited in space and only shares a small part of the information. If you need a full set of real interview questions (including answers to all questions), please click here. Code: qf
Recently, it is the best time to find a job. Everyone finds your favorite job smoothly!
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/w1103576/article/details/108694439
Recommended