Essential skills for java architect-JVM interview questions

JVM interview questions

1. What is the class loading mechanism of jvm? How many types of loaders are there?

2. JVM provides 3 types of loaders

3. What does MetaSpace stand for in JDK8?

4. What is the JVM memory structure?

5. What are the methods of garbage collection in Java?

6. What is the difference between MinorGC and FullGC?

7. Please write down several commonly used garbage collectors and enable parameters

The answer is as follows:

1. What is the class loading mechanism of jvm? How many types of loaders are there?

JVM uses the parent delegation model to load classes, that is, when a class loader receives a request to load a class, it first delegates the loading task to the parent class loader, and recursively, if the parent class loader can complete the class loading task , It returns successfully; only when the parent class loader cannot complete the loading task, it loads by itself.

2. JVM provides 3 types of loaders

(1) Bootstrap ClassLoader: Responsible for loading classes in the JAVA_HOME\lib directory or in the path specified by the -Xbootclasspath parameter and recognized by the virtual machine (identified by file name, such as rt.jar). (2) Extension ClassLoader: Responsible for loading the class library in the JAVA_HOME\lib\ext directory or in the path specified by the java.ext.dirs system variable. (3) Application ClassLoader: Responsible for loading the class library on the user path (classpath).

3. What does MetaSpace stand for in JDK8?

MetaqSpace is a term that was born in JDK8. It is a new memory space. It is translated into Chinese as: Metaspace; PermGen Space is removed from JDK8 HotSpot and MetaSpace is used instead. MetaSpace uses local memory to store classes. Metadata information. The memory capacity depends on the virtual memory size of the operating system, and the size of MetaSpace is limited by the parameter MaxMetaspaceSize.

4. What is the JVM memory structure?

JVM memory is mainly divided into five areas: method area, virtual machine stack, local method stack, heap, and program counter; the characteristics of each area are as follows: 1 "method area

JVM memory is mainly divided into five areas: method area, virtual machine stack, local method stack, heap, and program counter; the characteristics of each area are as follows: 1 "method area

1. Sometimes called permanent generation, garbage collection rarely occurs in this area, but it does not mean that GC does not occur. The GC performed here is mainly to unload the constant pool in the method area and the type

2. The method area is mainly used to store data such as information, constants, static variables and codes compiled by the JIT compiler that have been loaded by the virtual machine.

3. This area is shared by threads.

4. There is a runtime constant pool in the method area, which is used to store literals and symbol references generated by static compilation. The constant pool is dynamic, which means that constants are not necessarily determined at compile time, and constants generated at runtime will also be stored in this constant pool.

2》Virtual Machine Stack

1. The virtual machine stack is what we usually call the stack memory. It serves java methods. When each method is executed, a stack frame is created to store the local variable table, operand stack, dynamic link and method exit And other information.

2. The virtual machine stack is private to the thread, and its life cycle is the same as that of the thread.

3 "Native method stack The native method stack is similar to the virtual machine stack, except that the native method stack serves the Native method. 4 "Heap The java heap is a piece of memory shared by all threads. It is created when the virtual machine starts. Almost all object instances are created here. Therefore, garbage collection operations often occur in this area. 5 "The program counter has a small memory space. The bytecode interpreter can select the next bytecode instruction to be executed by changing this count value when it is working. Branches, loops, jumps, exception handling, and thread recovery all need to rely on this The counter is complete. This memory area is the only area where the Java virtual machine specification does not specify any OOM conditions.

5. What are the methods of garbage collection in Java?

1 "Mark-Remove: This is the most basic of the garbage collection algorithm. According to the name, you can know that 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. It is not efficient, 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. 2 "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 object is copied to the second Block memory, 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 three parts of 8:1:1, 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 copied into the old age. (The java heap is divided into the new generation and the old generation) 3. "Mark-Organize This algorithm is mainly to solve the problem of mark-clear and generate 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 first moved to one end, and then the object outside the end boundary is cleared, so that no memory fragmentation occurs. 4" Generational collection Most of the current virtual machine garbage collection adopts this method. It divides the heap into the new 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 each 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.

6. What is the difference between MinorGC and FullGC?

1 "Minor GC usually occurs in the Cenozoic Eden area. Objects in this area have a short life span. GC tends to occur more frequently and the recovery speed is faster. Generally, a copy-recovery algorithm is used. 2 "Full GC/Major GC occurs in In the old age, under normal circumstances, Minor GC will not be triggered when the old GC is triggered. The mark-sweep algorithm is used to generate concurrent-mode-failure. During the CMSGC process, the remaining space in the old age cannot be stored due to the need to allocate. Object, leading to the above reasons.

7. Please write down several commonly used garbage collectors and enable parameters

1 "Serial collector: Pause all threads, it belongs to single-threaded work, enable command: -XX:+UseSerialGC2" Parallel collector (default): Pause all threads, multi-threaded work, enable command: -XX:+UseParNewGC3" G1 Collector: This is mainly to partition the heap memory and recycle it concurrently. Enable: -XX:+UseG1GC4 "CMS Collector: Multi-threaded scanning. The algorithm used is the mark-sweep algorithm, which marks the objects that need to be recycled and recycles them. Start command: -XX:+UseConcMarkSweepGC

Guess you like

Origin blog.51cto.com/14993817/2547951