Interview question 1-jvm

1.jvm

Jvm is the Java virtual machine, which is the interface between the compiled .class file and the hardware system. Its main feature is platform independence.

principle:

The compiled program is not executed directly on the cpu of the hardware system, but executed on the jvm. It shields the relevant information of the specific platform. It only needs the Java program to generate bytecode files and can run directly on the jvm without modifying anything. .

JVM = classloader classloader + execution engine execution engine + runtime data area runtime data area

Class loader

The classloader loads the class file on the hard disk into the runtime data area in the JVM, but it is not responsible for whether this class file can be executed, but this is the
responsibility of the execution engine . Loading a class into a jvm generally requires a keyword called classLoader. The class loader
is used to load the .class file. The loading process is to check whether the class is loaded and whether it is loaded in the class that has been loaded. When loaded, return to the previously loaded class
directly . If not, load it directly and put it in the cache.

There are two ways to display and implicit

The implicit method is: during the running process, when encountering the object generated by the new method, implicitly call classLoader to JVM.
Explicit: dynamically load through class.forname()

Class loaders are divided into 4 categories

  1. Bootstrap Classloader   :   启动类加载器,用来加载 %JAVA_HOME%/jre/lib 下的, 如 rt.jar中的class文件 或者 xbootclasspath选项指定的jar包
  2. Extension Classloader :     扩展类加载器 , 用来加载 %JAVA_HOME%/jre/ext 中的class文件 或者 -Djava.ext.dirs指定目录下的jar包
  3. Application Classloader  :  应用类加载器 , 用来加载classpath下的class文件
  4. Custom  Classloader : 用户自定义类加载器,用来加载自定义内容.此加载器需要用户自己继承Classloader类

The role of the execution engine is to execute bytecode or execute local methods

Class life cycle: loading connection initialization using unloading

1. Loading: It means to find the information of the class in the source file's class file and load it into the method area, and then instantiate a java.lang.Class object in the heap area as the entry of the information of this class in the method area.
2. Connection: divided into verification preparation analysis
2.1 verification is to ensure that jvm can execute
2.2 preparation: allocate memory for static variables of the class, and initialize it to the default value
2.3 resolution: convert symbol references in the class into direct references
3. Initialization: (giving the correct initial value to the static variable of the class), and only execute it once
4. Use: object instantiation, garbage collection, object termination
5. Class unloading: there is no longer a reference to the class in the program, and the class is also It will be garbage collected by the JVM, and life ends...

The runtime data area refers to the division and allocation of jvm memory space with 5 areas

1. Program counter (thread private):

An indicator of the line number executed by the current thread. Determine the next instruction by changing the value of the counter, such as loops, branches, jumps, exception handling, thread recovery, etc., all rely on the counter to complete.
Java virtual machine multi-threading is realized by the way that threads alternately switch and allocate processor execution time. In order for the thread switch to be restored to the correct position, each thread needs an independent program counter, so it is thread-private.

2.Heap:

The heap is a shared area by all threads, created when the virtual machine starts, and has the sole purpose of storing object instances.
The heap area is the main area of ​​gc, which is usually divided into two blocks: the young generation and the old generation. The younger generation is more detailed and divided into the Eden area. The most important thing is to put newly created objects. From survivor and
To survivor save the surviving objects after gc. By default, they each account for 8:1:1.

3. Local method stack (thread private):

The native method stack is similar to the virtual machine stack, except that the native method stack serves the native method.

4. Virtual machine stack (thread private)

The virtual machine stack is what we usually call the stack memory. It serves Java methods. The Java virtual machine stack is private to the thread and has the same life cycle as the thread.
Each method execution will create a stack frame, which is used to store the local variable table, operation stack, dynamic link, method exit, etc. Each method is called until it is executed. Corresponds to the process of a stack frame from stacking to stacking in the virtual machine

5. Method area:

The area shared by all threads is used to store data such as class information, constants, and static variables that have been loaded by the virtual machine. It is described as a logical part of the heap by the Java virtual machine. The habit is also called permanent generation (permanment
generation). Garbage collection rarely visits this area, but it also needs to be collected, mainly for constant pool collection and type unloading.
The constant pool is used to store various bytecodes and symbol references generated during compilation. The constant pool has a certain degree of dynamics, which can store constants generated during compilation; constants during runtime can also be added to the constant pool, such as string intern() method.

Parental delegation

The parent delegation model requires that in addition to the top-level startup class loader, all other class loaders should have their own parent class loaders. The parent-child relationship between the class loaders here is generally not realized in the inheritance relationship, but the composition relationship is used to reuse the code of the parent loader.

The parent delegation model of the class loader was introduced during JDK1.2 and is widely used in almost all Java programs afterwards, but it is not a mandatory constraint model, but a kind of class loading recommended to developers by Java designers. Method to realize.

The working process of the parent delegation model is: if a class loader receives a class loading request, it will not first try to load the class by itself, but will delegate the request to the parent class loader to complete. This is true for class loaders, so all load requests should eventually be transmitted to the top-level startup class loader, only when the parent loader reports that it cannot complete the load request (the required class is not found in its search range) At that time, the child loader will try to load it by itself.

Advantages of the Parental Delegation Model

A Java class has a priority hierarchical relationship along with its class loader. For example, the class java.lang.Object, which exists in rt.jar, no matter which class loader wants to load this class, eventually The startup class loader given at the top of the model will be delegated to load, so the Object class is the same class in the various class loader environments of the program. On the contrary, if the parent delegation model is not used and each class loader loads it by itself, if the user writes a class called java.lang.Object (this class has the same functions as the Object class of the system, but in a certain The function is slightly modified. For example, the equals function, this function is often used, if in this function, hackers add some "virus code". And added to the JVM through a custom class loader, then it will be lively), and put In the ClassPath of the program, there will be multiple different Object classes in the system, the most basic behavior in the java type system cannot be guaranteed, and the application will become chaotic.

GC

The garbage collection mechanism is a low-priority thread in the jvm, which is generally not executed
. It will only be triggered when the virtual machine is idle or the current heap memory is insufficient. It scans those objects that are not referenced and will They are added to the collection to be recycled for recycling.

GC commonly used algorithms

Reference counter algorithm

The reference counting algorithm is very simple, it actually saves the number of times the object has been referenced by allocating a space in the object header. If the object is referenced by other objects, its reference count is increased by one, if the reference to the object is deleted, then its reference count is decreased by one, when the object's reference count is 0, then the object will be recycled .

The mechanism of reference counting garbage collection is different, it only occurs when the reference count changes to 0, and only for a certain object and other objects it depends on. Therefore, we generally call reference counting garbage collection as a direct garbage collection mechanism

But this kind of reference counting algorithm has a bigger problem, that is, it cannot handle circular data-
that is, if two objects refer to each other, then these two objects cannot be recycled, because their reference count is always 1. This is what we often call the "memory leak" problem

Algorithm characteristics

  1. A separate field is required to store the counter, which increases the overhead of storage space;

  2. Every assignment needs to update the counter, which increases the time overhead;

  3. Garbage objects are easy to identify, as long as the counter is 0, they can be collected as garbage;

  4. Recycle garbage in time, without delay;

  5. Can not solve the problem of circular references;

Reachability analysis algorithm

The basic idea of ​​the root search algorithm is to use a series
of objects named "GC Roots" as the starting point, starting from these nodes and searching downwards. The path taken by the search is called the Reference Chain. When an object reaches the GC
When Roots is not connected by any reference chain, it proves that the object is unusable.

The basic idea of ​​this algorithm is to use a series of objects called "GC Roots" as the starting point, and search downward from these nodes. The path traversed by the search is called the reference chain. When an object reaches the GC
Roots without any reference chain ( That is, when the GC Roots reach the object is unreachable), it proves that the object is unavailable.

So the question is again, how to select the GCRoots object? In the Java language, the objects that can be used as GCRoots include the following:

(1). Objects referenced in the virtual machine stack (local variable area in the stack frame, also called the local variable table).

(2). The object referenced by the class static property in the method area.

(3). Objects referenced by constants in the method area.

(4). Objects referenced by JNI (Native method) in the native method stack.

Garbage collection in java

Mark-sweep:
This is the most basic of the garbage collection algorithm. According to the name, it can be known. Its idea is to mark which objects to be recycled, and then collect them uniformly. This method is very simple, but there are two main problems: 1. Inefficient, marking and clearing are very low; 2. A large number of discontinuous memory fragments will be generated, which will cause the program to allocate larger objects in the future , Because there is not enough continuous memory to trigger a GC action in advance.

Copy algorithm:
In order to solve the problem of efficiency, the copy algorithm divides the available memory into two equal parts according to the capacity, and then only uses one of them at a time. When one memory is used up, the surviving objects are copied to the second memory. , And then clear the first block of memory at one time, and then copy the objects on the second block to the first block. But in this way, the cost of memory is too high, and general memory is basically wasted every time. So the algorithm is improved, the memory area is no longer divided according to 1:1, but the memory is divided into 8:1:1 three parts, the larger one is given to the Eden area, and the rest are two smaller ones The memory area is called Survior area. The Eden area will be used first every time. If the Eden area is full, the objects will be copied to the second memory area, and then the Eden area will be cleared. If there are too many surviving objects at this time that Survivor is not enough, these objects will be passed through The distribution guarantee mechanism is replicated in the old age. (Java heap is divided into new generation and old generation)

Marking-sorting
This algorithm is mainly to solve the problem of marking-clearing and generating a large amount of memory fragmentation; when the object survival rate is high, it also solves the efficiency problem of the replication algorithm. The difference is that when the object is cleared, the recyclable object is now moved to one end, and then the object outside the end boundary is cleared, so that memory fragmentation will not occur.

Generational collection
Most of the current virtual machine garbage collection uses this method. It divides the heap into the young generation and the old generation according to the life cycle of the object. In the new generation, due to the short lifetime of objects, a large number of objects will die every time they are recycled, so the replication algorithm is used at this time. Objects in the old age have a higher survival rate, and there is no extra space for allocation guarantee, so you can use mark-organize
or mark-clear.

Four kinds of references exist in java

1. Strong reference: For example, as long as string s=”abc” exists, garbage collection will not occur
. 2. Soft reference: Reclaim when there is insufficient memory , but not when there is enough memory. It is usually used together with the reference queue. If the object is soft referenced After being garbage collected, jvm will add this soft reference object to the reference queue associated with it

Insert picture description here

What are the GC collectors

Serial Garbage Collector, Parallel Garbage Collector, Concurrent Mark Scanning Garbage Collector, G1 Garbage Collector

Jvm tuning

The main purpose of system-level tuning of JVM memory is to reduce the frequency of GC and the number of Full GC. Excessive GC and Full
GC will take up a lot of system resources (mainly CPU) and affect system throughput. Pay special attention to Full GC, because it will organize the entire heap.
1. Set the young generation size not to be too large or too small. Too small GC will be very frequent and too large will cause the old generation to be too small, which will induce Full GC. Generally speaking The young generation occupies 1/3 of the entire heap
. 2. Set the size of the heap memory
3. Set the garbage collector, the young generation -xx:+UserparNewGc The old generation -XX:+UserConCMarkSweepGc

Insert picture description here

Guess you like

Origin blog.csdn.net/zyf_fly66/article/details/114011267