Original link https://www.cnblogs.com/jinyujia/p/15697183.html
Basics
1. What is the relationship between JDK, JRE and JVM?
What is a JVM?
English name (Java Virtual Machine ),namely JAVA Virtual machine, which only recognizes .class Type file, it Be able to class The bytecode instruction in the file is recognized and the upward operation of the operating system is called API Complete the action.
What is JRE?
English name ( Java Runtime Environment ), Java Runtime environment. It mainly consists of two parts: JVM Standard implementation and Java Some basic class libraries. be relative to JVM For example, JRE Some more Java Class library.
What is JDK?
English name ( Java Development Kit ),Java Development kit. JDK It's the whole Java The core of development, it integrates JRE And some handy gadgets. For example: javac.exe,java.exe,jar.exe Wait. The relationship between the three: a layer of nested relationship. JDK > JRE > JVM.
2. Memory model of JVM, partition condition and function
As shown in the figure below:
The yellow part is shared by threads, and the blue part is private to threads.
Method area
It is used to store class information, constants, static variables and other data loaded by the virtual machine.
heap
Store object instances. All objects and arrays should be allocated on the heap. Yes JVM The largest area of memory managed.
Stack
Java Memory model for method execution: Store local variable table, operand stack, dynamic link, method exit and other information. The life cycle is the same as that of thread.
Native Method Stack
The function is similar to that of virtual machine stack. difference The local method stack is native Method execution service The virtual machine stack is executed for the virtual machine Java Method services.
Program counter
The line number indicator executed by the current thread is JVM The smallest area in the memory area. When executing bytecode, it uses the program counter to select the next bytecode instruction to be executed.
3. What is the process of creating JVM objects?
The overall process is shown in the figure below:
- Step 1: when the virtual machine encounters a new instruction, it will first check whether the parameters of this instruction can locate the symbol reference of this class in the constant pool, and check whether the class referenced by this symbol has been loaded, parsed and initialized.
- Step 2: if the class has been loaded, proceed to step 3; If it is not loaded, you need to load the class first.
- Step 3: after the class loading check passes, allocate the memory of the new object.
- Step 4: the memory size required for object generation can be completely determined after the class is loaded. Allocating space for objects is equivalent to dividing a piece of memory with a certain size from the Java heap.
- Step 5: the memory size can be divided into two cases:
- The first case: the memory of the JVM is regular. All used memory is placed on one side, the free memory is on the other side, and a pointer is placed in the middle as an indicator of the dividing point. Then it's easier to allocate memory at this time. Just move the pointer to the free space for a distance equal to the size of the object. This is called "pointer collision".
- The second case: the memory of the JVM is not regular, that is, the used memory and unused memory are staggered with each other. At this time, there is no way to use the pointer collision. At this time, we need to maintain a table to record the available memory. When allocating, we need to find a large enough space in the list to divide it into object instances and update it to the record table.
- Step 6: after the space application is completed, the JVM needs to initialize the memory space to the value of 0. If TLAB is used, this work can be carried out when TLAB is allocated.
- Step 7: the JVM makes the necessary settings for the object. For example, the instance of which class this object is, the hash code of the object, the GC age and other information.
- Step 8: after completing the above steps, an object is basically completed from the JVM, but from the absolute point of view of the Java program code, the object creation has just begun. You need to execute the < init > method and initialize according to the initialization operation set in the program. At this time, a real program object is generated.
4. How many types of garbage collection algorithms are there? What are their corresponding advantages and disadvantages?
Common garbage collection algorithms include:
sign-Clearing algorithm Replication algorithm sign-Sorting algorithm Generational collection algorithm
Mark clear algorithm
sign-The purge algorithm consists of two stages:"sign" and "eliminate". Marking stage: determine all objects to be recycled and mark them. Clear phase: clears objects that are determined to be unavailable in the marking phase. Disadvantages: 1.The efficiency of marking and removal is not high. 2.A large amount of debris will be generated, resulting in frequent recycling.
Replication algorithm
The memory is divided into two blocks of equal size, one of which is used each time. When garbage collection, copy the living objects to the other block, and then clean up the whole memory. Disadvantages: 1.Additional memory needs to be wasted as a copy area. 2.When the survival rate is high, the efficiency of replication algorithm will decrease.
Marking sorting algorithm
sign-The sorting algorithm does not copy the living object to another piece of memory, but moves the living object to one end of memory, and then directly reclaims the memory outside the boundary. Disadvantages: the complexity of the algorithm is large and there are many execution steps
Generational collection algorithm
At present, most JVM The algorithm used by the garbage collector. The memory is divided into several different regions according to the life cycle of the object. Generally, the heap area is divided into Cenozoic( Young Generation) And the elderly generation( Tenured Generation ),Permanent generation( Permanet Generation ). The characteristic of the old age is that only a small number of objects need to be recycled every time garbage collection, The feature of the new generation is that a large number of objects need to be recycled every time garbage is recycled, Then the most suitable collection algorithm can be adopted according to the characteristics of different generations. Young: Store newly created objects. The object life cycle is very short. It can be recycled immediately after almost running out. It is also called Eden Area. Tenured: Young Objects that survive multiple recoveries will be moved to Tenured District, also known as old Area. Perm: Permanent tape, which mainly stores the loaded class information, has a long life cycle and will hardly be recycled. Disadvantages: the algorithm has large complexity and many execution steps.
5. Briefly introduce what is the class loading mechanism?
Class After the file is loaded by the class loader, in JVM A description will be formed Class The meta information object of the structure can be known through the meta information object Class Structure information: such as constructors, properties and methods. The virtual machine transfers the data describing the class from Class The file is loaded into the memory, and the data is verified, transformed, parsed and initialized to form a file that can be directly used by the virtual machine Java Type, which is the class loading mechanism of virtual machine.
6. What is the loading process of the class? Briefly describe each step
The process of class loading includes:
Five stages: load verification prepare analysis initialization
- Step 1: load
Loading is the first stage of the class loading process. In this stage, the virtual machine needs to complete the following three things:- Get the binary byte stream defined by the fully qualified name of the class
- The static storage structure represented by the byte stream is transformed into the runtime data structure of the method area
- Generate a Java. XML file representing this class in the Java heap Lang. class object as the access entry to these data in the method area
- Step 2: Verification
Ensure the correctness of the loaded class.
This stage is to ensure that the information contained in the byte stream of the Class file conforms to the specifications of the current virtual machine and will not damage the security of the virtual machine itself.
It includes four verification actions: file format verification, metadata verification, bytecode verification and symbol reference verification. - Step 3: preparation
Allocate memory for static variables of the class and initialize them to default values.
The preparation stage is the stage of formally allocating memory for class variables and setting the initial value of class variables. These memory will be allocated in the method area. - Step 4: analysis
Converts symbolic references in a class to direct references.
The parsing stage is the process in which the virtual machine replaces the symbol reference in the constant pool with a direct reference. The parsing action is mainly for class or interface, field, class method, interface method, method type, method handle and call point qualifier. - Step 5: initialization
Class variable.
Give the correct initial value to the static variables of the class. The JVM is responsible for initializing the class, mainly initializing the class variables.
7. What are the predefined class loaders for the JVM? What is the role of each?
Start( Bootstrap)Class loader Standard extension( Extension)Class loader Application class loader(Application)
Bootstrap class loader
The boot class loader is a class loader implemented with local code, which is responsible for < JavaRuntimeHome >/lib The following class libraries are loaded into memory. Since the boot class loader involves the local implementation details of the virtual machine, developers cannot directly obtain the reference of the boot class loader.
Standard Extension class loader
The extension class loader is responsible for < Java_Runtime_Home >/lib/ext Or by system variable java.ext.dir The class library in the specified location is loaded into memory. Developers can use the standard extension class loader directly.
Application class loader
Application class loader( Application ClassLoader): Responsible for loading user paths( classpath)Class library on.
8. What is the parental delegation model? What does it do?
Basic definition
The workflow of parent delegation model is: If a class loader receives a class loading request, it will not load the class itself first, Instead, the request is delegated to the parent loader to complete, up in turn, Therefore, all class loading requests should eventually be passed to the top-level startup class loader, Only when the parent loader does not find the required class will the child loader try to load the class.
Parental delegation mechanism:
1.When AppClassLoader Load a class Instead of trying to load the class itself, it delegates the class loading request to the parent class loader ExtClassLoader To finish. 2.When ExtClassLoader Load a class Instead of trying to load the class itself, it delegates the class loading request to the BootStrapClassLoader To finish. 3.If BootStrapClassLoader Loading failed, will use ExtClassLoader To try loading. 4.if ExtClassLoader If the load fails, the AppClassLoader To load, if AppClassLoader If the loading fails, an exception will be reported ClassNotFoundException.
As shown in the figure below:
Parental delegation:
The repeated loading of classes can be avoided through the hierarchical closing with priority. ensure Java The program runs safely and stably, Java core API Definition types will not be arbitrarily replaced.
8. What are the garbage collectors in the JVM? What are their characteristics?
New generation garbage collector
Serial collector
characteristic: Serial The collector can only use one thread for garbage collection, and all working threads need to stop working during garbage collection. After the garbage collection thread is completed, other threads can continue to work. Use algorithm: copy algorithm
ParNew collector
characteristic: ParNew The garbage collector is Serial The multithreaded version of the collector. In order to use CPU The advantages of multi-core and multithreading, ParNew The collector can run multiple collection threads for garbage collection. This can improve the efficiency of the garbage collection process. Use algorithm: copy algorithm
Parallel Scavenge collector
characteristic: Parallel Scavenge The collector is a multithreaded garbage collector, but it is similar to ParNew There are great differences. Parallel Scavenge Collectors have different concerns than other collectors. Other collectors, such as ParNew and CMS These collectors are mainly concerned with how to shorten the time of garbage collection. and Parallel Scavenge The collector focuses on how to control the throughput of the system. Throughput here refers to CPU Time and for running the application CPU Proportion of total time, throughput = Code run time / (Code run time + Garbage collection time). If the total number of virtual machines running CPU If the time is 100 minutes and the time for garbage collection is 1 minute, the throughput is 99%. Use algorithm: copy algorithm
Old age garbage collector
Serial Old collector
characteristic: Serial Old Collector is Serial An older version of the collector. This collector is mainly used as the garbage collector of the older generation in client applications, and can also be used as the garbage collector of server applications. Using algorithms: Tags-arrangement
Parallel Old collector
characteristic: Parallel Old Collector is Parallel Scavenge An old version of the collector. This collector is in JDK1.6 Version, so in JDK1.6 Before, the Cenozoic Parallel Scavenge Only with Serial Old This single threaded old collector is used in conjunction with. Parallel Old Garbage collector and Parallel Scavenge Like the garbage collector, it is also a garbage collector focusing on throughput, and Parallel Scavenge Together with the collector, the Java Heap memory throughput priority garbage collection strategy. Using algorithms: Tags-arrangement
CMS collector
characteristic: CMS Collector is an excellent garbage collector among the old generation collectors at present. CMS yes Concurrent Mark Sweep,As can be seen from the name, this is a use "sign-eliminate" Concurrent collector of algorithm. CMS The garbage collector is a collector that aims to obtain the shortest pause time.
As shown in the figure below:
As can be seen from the figure, the working process of CMS collector can be divided into four stages:
- CMS initial mark stage
- CMS concurrent mark stage
- CMS remark phase
- Concurrent sweep (CMS)
Use algorithm: Copy + mark clear
G1 garbage collector
Features: main steps: initial marking, concurrent marking, re marking, copy and clear. Using algorithms: copying + Marking arrangement
9. What is a Class file? What are the main information structures of Class file?
Class A file is a set of binary streams based on 8-bit bytes. Each data item is arranged in strict order. Class The file format is similar to C The pseudo structure of language structure to store data. Such a pseudo structure has only two data types: Unsigned numbers and tables. Unsigned number: is the basic data type. with u1,u2,u4,u8 An unsigned number representing 1 byte, 2 bytes, 4 bytes and 8 bytes respectively, which can be used to describe narrative numbers, index references, quantitative values or UTF-8 A string value formed by encoding. Table: a composite data type consisting of multiple unsigned numbers or other tables as data items. All tables are habitually _info ending.
10. What is the concept of "object is dead"?
The object can no longer be used in any way, which is called the object is dead. The methods to judge whether the object is dead include reference counting method and reachability analysis algorithm.
Burst stage
11. How does Java language realize cross platform?
We wrote Java Source code, compiled will generate a .class File, called bytecode file. Bytecode cannot be run directly and must be passed JVM Translated into machine code to run, JVM It is a "bridge", a "middleware", and the key to cross platform. Java The code is first compiled into a bytecode file, and then JVM Translate the bytecode file into machine language to achieve operation Java Purpose of the procedure.
12. In the JVM data operation area, what will cause OOM?
In addition to the data operation area, other areas may cause OOM The situation. Heap overflow: java.lang .OutofMemoryError: Java heap space Stack overflow: java.lang.StackoverflowError Permanent generation overflow (memory overflow): java.lang.outofMemoryError: PermGen space
13. Describe in detail the allocation process of objects in the Striped memory area?
1.JVM Will try to Java Object in Eden Initializes a memory area in. 2.When Eden When there is enough space, the memory application ends; Otherwise, go to the next step. 3.JVM Trying to release in Eden All inactive objects in (this is worse than 1 or higher level garbage collection). If after release Eden If the empty question is still not enough to put the new object, try to put the part Eden Put active objects in Survivor Area. 4. Survivor The zone is used as a zone Eden and old Intermediate exchange area, when old When there is enough space in the area, Survivor The objects in the area are moved to old Area, otherwise it will be retained in Survivor Area. 5. When old When there is not enough space in the area, JVM Will be old Complete garbage collection in the area. 6.After complete garbage collection, if survivor and old Still unable to store from Eden Some objects that have been copied in the area, resulting in JVM Cannot be in Eden Area creates a memory area for the new object "out of memory"Wrong.
14. Comparison between G1 and CMS garbage collectors
The details are different
- G1 has advantages in compressing space.
- G1 avoids memory fragmentation by dividing memory space into regions,
- Eden, survivor, the old area is no longer fixed and is more flexible in terms of memory efficiency.
- G1 can control the garbage collection time by setting the expected Pause Time area to avoid application avalanche.
- G1 will merge free memory immediately after reclaiming memory, while CMS will do it at STW (stop the world) by default
- G1 will be used in Young GC, while CMS can only be used in o area.
The overall content is different
- Throughput priority: G1
- Response priority: CMS
- The disadvantage of CMS is that it has high requirements for cpu.
- G1 converts the memory into multiple blocks, and all have great requirements for the size of the inner segment.
- CMS is cleared, so there will be a lot of memory fragments.
- G1 is a defragmentation, so the debris space is small.
15. What are the JVM parameters commonly used online?
Data area setting
- Xms: initial heap size
- Xmx: maximum heap size
- XSS: Stack size of each java thread
- 20: Newsize = n: sets the size of the younger generation.
- 20: Newratio = n: set the generation comparison between the younger generation and the older generation. For example, if it is 3, it means that the ratio between the young generation and the old generation is 1:3, and the young generation accounts for 1 / 4 of the sum of the young generation and the old generation.
- 20: Survivorratio = n: the comparison between Eden district and two survivor districts in the young generation. Note that there are two in the survivor area. For example, 3 means Eden: Survivor = 3:2, and one survivor area accounts for 1 / 5 of the whole young generation.
- 20: Maxpermsize = n: sets the persistent generation size.
Collector settings
- 20: + useserialgc: set serial collector
- 20: + usepasallelgc: set parallel collector
- 20: + usepasalledlold GC: set parallel collector
- 20: + useconcmask sweepgc: set concurrent collector
GC log print settings
- 20: + psintgc: print brief information of GC
- 20: + psintgcdetails: print GC details
- 20: + psintgctimestamps: output GC timestamp
16. When will the target enter the elderly generation?
Object preferentially allocates memory in Eden area
When the object is first created, it will be placed in the new generation eden Area, if not GC Your intervention will always be eden area, GC After that, it is possible to enter susvivos District or old generation
Large objects directly enter the elderly generation
The so-called large objects refer to objects that require a large amount of continuous memory space Java Object, The most typical large objects are long strings and arrays, The memory allocation of large objects to virtual machines is bad news, In particular, some short-lived large objects that live in the morning and die in the evening should be avoided when writing programs.
Long lived objects enter the elderly generation
The so-called large objects refer to objects that require a large amount of continuous memory space Java Object, The most typical large objects are long strings and arrays, The memory allocation of large objects to virtual machines is bad news, In particular, some short-lived large objects that live in the morning and die in the evening should be avoided when writing programs.
17. What is memory overflow, memory leak? What is the difference between them?
out of memory out of memory, It refers to that the program does not have enough memory space for its use when applying for memory out of memosy; Memory leak memory leak, It means that the program cannot release the applied memory space after applying for memory. The harm of a memory leak can be ignored, but the consequences of memory leak are very serious. No matter how much memory, it will be occupied sooner or later.
Memory overflow is that the memory you require to allocate exceeds what the system can give you. The system cannot meet the demand, so overflow occurs.
You may lose the memory you need to allocate to your system (the result is that you can't allocate it to your system again), but you can't use it again.
18. What are the operations that cause class loading?
1. encounter new,getstatic,putstatic or invokestatic These four bytecode instructions. 2. During reflection calls, if the class has not been initialized, its initialization needs to be triggered first. 3. When initializing a subclass, if its parent class has not been initialized, the initialization of its parent class needs to be triggered first. 4. When the virtual machine executes the main class (yes) main ( stringll args)) 5. JDK1.7 Dynamic language support.
19. Introduce the common functions provided by the JVM
- jps: used to display local Java processes. You can view several Java programs running locally and display their process numbers
Command format: ips - jinfo: running environment parameters: Java System properties, JVM command line parameters, Java class path and other information
Command format: info process pid - jstat: a command-line tool for monitoring various running status information of virtual machines
Command format: jstat -gc 123 250 20 - jstack: you can observe the running status and current state of all threads in the JVM.
Command format: istack process pid - jmap: observe the usage of the running JVM's physical memory (e.g. which objects are generated and their number)
Command format: jmap [loption] pid
20. What is the difference between full GC, Major GC and Minor GC?
Minor GC: From Cenozoic space (including Eden and Survivor Area) reclaiming memory is called Minor GC. Major GC: clear Tenured Area, used for recycling the elderly generation, appears Major GC The communication will appear at least once Minor GC. Full GC: Full GC It is aimed at the whole Cenozoic era, old age and meta space( metaspace, java8 Superseded by the above version permgen)Global scope of GC.
21. When will Full GC be triggered?
- Call system When performing GC, the system recommends Full GC, but it is not necessary.
- There is not enough space in the old age.
- Insufficient space in method area.
- After exceeding Minor GC, the average size of entering the old age is larger than the available memory of the old age.
- When copying from Eden area and survivor spacel (From Space) area to survivor space2 (To Space) area, if the size of the object is larger than the available memory of To Space, the object is transferred to the old age, and the available memory of the old age is smaller than the size of the object.
22. Under what circumstances will stack overflow occur
- Method creates a large object, such as list and array.
- Whether loop call and dead loop are generated.
- Whether a large global variable is referenced.
23. Talk about strong reference, soft reference, weak reference, virtual reference and their relationship with GC
- Strong reference: references such as new objects will never be recycled as long as the strong reference is still there.
- Soft reference: refers to but not necessary objects, which are recycled before memory overflow exception.
- Weak reference: an unnecessary object that can survive until the next garbage collection occurs.
- Virtual reference: it has no impact on the lifetime and is notified when garbage collection.
24. What is the proportional distribution of Eden and Survivor? Why?
Default scale 8 : 1 . Most objects live and die day and night. The basic idea of replication algorithm is to divide the memory into two blocks, and only use one of them each time. When this block of memory is used up, it will be deleted Copy the living object onto another piece. The replication algorithm does not generate memory fragmentation.
actual combat
25. CPU resources are too high
- top check the current CPU status and find the process that occupies too much CPU. PID=123.
- top -H -p123 find out the two threads with high CPU consumption, record PID=2345, 3456 and convert them to hexadecimal.
- jstack -l 123 > temp. Txt print out the thread stack of the current process.
- Find the two thread running stacks corresponding to the second step and analyze the code.
26. OOM exception troubleshooting
- Use the top command to query the server system status.
- ps -aux|grep java find the PID of the current java process.
- jmap -histo:dve pid can be used to count the distribution of living objects and view the objects occupying the most memory from high to low.
- jmap -dump:format=b,fice = file name [pid] use Jmap dump.
- There are tools to analyze the performance of MAT dump files.