java based JVM interview questions

Posted by Skudd on Fri, 14 Jan 2022 09:58:24 +0100

1. What is a JVM? What does the java virtual machine include? JVM memory model?

A: JVM:java virtual machine is a virtual computer implemented by hardware or software

java virtual machine includes: stack, processor and register

Program counter: the type indicator of bytecode executed by the current thread. It is used to record the address of virtual machine byte instruction being executed. The thread is private

java virtual stack: store basic data types, object references, method exits, etc. thread private.

Native method stack: similar to virtual stack, but it serves native methods and threads are private.

java heap: the largest piece of java memory. All object instances and data are stored in the java heap, where GC recycles and shared by threads.

Method area: store the loaded class information, constants, static variables, code data after the real-time compiler, etc.

2. Under what circumstances will stack memory overflow occur?

Answer idea: first describe the definition of stack, then describe why it overflows, and then explain the relevant configuration parameters.

A: the stack is thread private. Its life cycle is the same as that of the thread. When each method is executed, a stack frame will be created to store local variable table, operand stack, dynamic link, method exit and other information. The local variable table also contains basic data types and object reference types.

​ 1. If the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine, a StackOverflowError exception will be thrown, and the method recursive call will produce this result.

​ 2. If the JAVA virtual machine stack can be expanded dynamically, and the expansion action has been tried, but enough memory cannot be applied to complete the expansion. Or if there is not enough memory to create the corresponding virtual machine stack when creating a new thread, the JAVA virtual machine will throw an OutOfMemory exception. (too many threads started)

​ 3. Parameter - Xss to adjust the size of JVM stack

3. What kinds of garbage collectors do you know? Focus on cms and G1

Idea: remember the typical garbage collectors, especially cms and G1, their principles and differences, and the garbage collection algorithms involved.

(1) centralized garbage collector:

Serial collector: a single thread collector. When collecting garbage, you must stop the world and use the replication algorithm.
ParNew collector: the multithreaded version of Serial collector also needs to stop the world and copy algorithm.
Parallel Scavenge collector: new generation collector, replication algorithm collector and concurrent multithreading collector. The goal is to achieve a controllable throughput. If the virtual machine runs for 100 minutes in total, and garbage takes 1 minute, the throughput is 99%.
Serial Old collector: it is an old version of serial collector. It is a single thread collector and uses tag collation algorithm.
Parallel Old collector: it is an old version of Parallel Scavenge collector, which uses multithreading and tag collation algorithm.
CMS(Concurrent Mark Sweep) collector: it is a collector aiming at obtaining the shortest recovery pause time. The tag removal algorithm and operation process: initial tag, concurrent tag, re tag and concurrent removal. A large amount of space debris will be generated at the end of collection.
G1 collector: the implementation of tag sorting algorithm. The operation process mainly includes the following: initial tag, concurrent tag, final tag and filter tag. No space debris will be generated, and the pause can be accurately controlled.

(2) difference between CMS collector and G1 collector:

CMS collector is an old collector, which can be used together with the new generation of Serial and ParNew collectors;
The collection range of G1 collector is old age and new generation, and it does not need to be used in combination with other collectors;
CMS collector is a collector aiming at minimum pause time;
G1 collector can predict the pause time of garbage collection
CMS collector is a garbage collection using "mark clear" algorithm, which is prone to memory fragmentation
The G1 collector uses the "mark and tidy" algorithm to integrate space and reduce memory space fragmentation.

4. Briefly talk about the classloader you know. Can you break the parental delegation and how?

**Idea: * * first explain what class loader is, draw a diagram for the interviewer, talk about the significance of class loader, talk about the parent delegation model, and finally explain how to break the parent delegation model.

A: 1) what is a class loader?

The class loader loads the class file into the JVM memory according to the specified permission and name, and converts it into a class object.

1.Start class loader( Bootstrap ClassLoader):from C++Language implementation (for HotSpot),Responsible for storing in<JAVA_HOME>\bin Directory or-Xbootclasspath The class library in the path specified by the parameter is loaded into memory.
2.Other class loaders: java Language implementation, inherited from abstract classes ClassLoader. For example:
	Extended class loader( Extension ClassLoader):Responsible for loading<JAVA_HOME>\bin\ext Directory or java.ext.dirs All class libraries in the path specified by the system variable.
	Application class loader( Application ClassLoader): Responsible for loading user classpath( classpath)For the specified class library on, we can directly use this class loader. Generally, if we do not have a custom class loader, this loader is used by default.

2) Working process of parent delegation model:

If a class loader receives a class loading request, it will not try to load the class itself, but delegate the request to the parent class loader to complete. This is true for each class loader, only if the parent loader cannot find the specified class in its own search scope, (i.e ClassNotFoundException),The child loader will try to load itself.

3) Why do I need a parental delegation model?

If there is no parental delegation, can users define one by themselves java.lang.Object Class with the same name, java.lang.String And put it in ClassPath in,Then the comparison results between classes and the uniqueness of classes cannot be guaranteed. Therefore, the parental delegation model prevents multiple copies of the same bytecode from appearing in memory.

4) How to break the dual delegation model?

Breaking the parental delegation mechanism requires not only inheritance ClassLoader Class, but also override loadClass and findClass method.

5. Talk about some main JVM parameters you know

Idea: we can talk about stack configuration, garbage collector and auxiliary information.

a) Stack configuration related

java -Xmx3550m -Xms3550m -Xmn2g -Xss128k 
-XX:MaxPermSize=16m -XX:NewRatio=4 -XX:SurvivorRatio=4 -XX:MaxTenuringThreshold=0

-Xmx3550m: the maximum heap size is 3550m

-Xms3550m: set the initial heap size to 3550m

-Xmn2g: set the size of the younger generation to 2g

-Xss128k: the stack size of each thread is 128k

-20: MaxPermSize: set the persistent generation size to 16m

-20: Newration = 4: set the ratio of the younger generation (including Eden and two Survivor areas) to the older generation (the younger generation)

-20: Survivorratio = 4: set the size ratio of Eden area and Survivor area in the young generation. If it is set to 4, the ratio of two Survivor areas to one Eden area is 2:4, and one Survivor area accounts for 1 / 6 of the whole young generation

-20: Maxtenuringthreshold = 0: sets the maximum age of garbage. If it is set to 0, the younger generation objects directly enter the older generation without passing through the Survivor area.
b) Garbage collector related

-XX:+UseParallelGC //Select the garbage collector as a parallel collector
-XX:ParallelGCThreads=20 //Configure the number of threads for the parallel collector
-XX:+UseConcMarkSweepGC   // Set generation to concurrent collection
-XX:CMSFullGCsBeforeCompaction=5  //This value sets how many times GC is run to compress and tidy the memory space.
-XX:+UseCMSCompactAtFullCollection: // Turn on compression for older generations. Performance may be affected, but fragmentation can be eliminated

6. How to print thread stack information

**Idea: * * you can use the commands jps, top and jstack to solve online problems

input jps,Get process number
top -Hp pid Gets the of all threads in this process CPU Time consuming performance
jstack pid Command view current java Stack state of the process
 perhaps jstack -l > /tmp/output.txt Stack information to a txt File.
have access to fastthread Stack positioning, fastthread.io/

7. What is the difference between strong reference, soft reference, weak reference and virtual reference?

Idea: first, let's talk about the definitions of the four references, which can be combined with the code, or can be extended to talk about the use of weak references in ThreadLocalMap.

1)Strong reference
	We usually new An object is a strong reference, for example Object obj = new Object();Even when memory is low, JVM Would rather throw OutOfMemory Errors do not recycle such objects.
2)Soft reference
	If an object has only soft references, the memory space is enough, and the garbage collector will not recycle it; If the memory space is insufficient, the memory of these objects will be reclaimed.
	SoftReference<String> softRef=new SoftReference<String>(str);     // Soft reference
 Soft reference has important applications in practice, such as the browser's back button.
3)Weak reference
	Objects with weak references have shorter lifecycles. When the garbage collector thread scans the memory area under its jurisdiction, once it finds an object with only weak references, it will reclaim its memory regardless of whether the current memory space is sufficient or not.
4) Virtual reference
	If an object holds only virtual references, it may be recycled by the garbage collector at any time as if it had no references. Virtual references are mainly used to track the activities of objects collected by the garbage collector.


Topics: Java jvm Interview