At the end of the year, XJJDog decided to write a useful hardcore article. This article contains 38 interview questions, covering all aspects of the JVM, all of which are common. If recite memory down, enter dachang very easy.

Interview questions should not deceive people, so the content of this article has been polished many times, and now for you.

Some interview questions are open-ended and some are informative. Pay attention to the difference. There are no standard answers, especially for open-ended questions. You need to write in plain English to present yourself as best you can.

If you describe something in your answer that you are not familiar with, you may be pressed. So, according to the problem, sort out a suitable for their own, this is more impressive than taking doctrine.

What memory regions does the JVM have? (What is the memory layout of the JVM?)

The JVM contains memory areas such as the heap, meta-space, Java virtual machine stack, local method stack, program counters, and so on. The heap is the largest chunk of memory. The usual -xmx, -xms parameters are designed for the heap.

  • Heap: The data in the JVM heap, which is shared, is the largest area of memory

  • Virtual machine stack: The Java virtual machine stack, which is thread-based and serves the execution of bytecode instructions

  • Program counter: Line number indicator of bytecode executed by the current thread

  • Meta space: the method area is here, non-heap local memory: other memory footprint

2. What is Java’s memory model? (What is JMM?)

The JVM attempts to define a unified memory model that encapsulates the differences in memory access between the underlying hardware and operating systems so that Java programs can achieve the same concurrency on different hardware and operating systems. It is divided into working memory and main memory, threads can not operate directly on the main memory, a thread to communicate with another thread, can only be exchanged through main memory.

The JMM is the foundation of Concurrency in Java, and its definition will directly affect the implementation mechanism of multithreading. If you want to understand the problems and phenomena related to multithreading concurrency, a thorough study of the JMM is essential.

The above two questions are often confusing, but their content is completely different.

3. How to determine garbage when collecting JVM garbage? What are GC Roots?

The JVM uses a reachability analysis algorithm. The JVM determines the survival of an object through GC Roots. Tracing and searching down from GC Roots produces a Chain called the Reference Chain. When an object cannot relate to either GC Root, it is considered garbage.

GC Roots generally include:

  • Various references related to active threads, such as references in stack frames in the virtual machine stack.

  • Class reference to a static variable.

  • JNI citation, etc.

Of course, there are more detailed answers. Personally, I think these are enough. The detailed version is as follows:

  1. In a Java thread, reference type parameters, local variables, temporary values, and so on for all methods currently being called. That is, the various references associated with our stack frame.

  2. All currently loaded Java classes.

  3. A Java class reference type static variable.

  4. A reference type constant (String or Class) in the runtime constant pool.

  5. The JVM internal data structures of some references, such as the sun. The JVM. Hotspot. The memory. The Universe.

  6. Monitor objects for synchronization, such as objects whose wait() method is called.

  7. JNI handles include global handles and Local handles

4. Will objects in Reference Chain survive if they can be found?

It depends on the reference type. Weak references are collected during GC, and soft references are collected when memory runs out. However, objects without Reference Chain will definitely be reclaimed.

5. What are strong references, soft references, weak references and virtual references?

A common object reference is a strong reference.

Soft references are used to maintain unnecessary objects. The system reclaims soft reference objects only when there is insufficient memory. If there is still insufficient memory after reclaiming soft reference objects, an out of memory exception is thrown.

Weak reference objects are more useless than soft references and have a shorter life cycle. When a JVM does garbage collection, objects associated with weak references are reclaimed regardless of whether memory is sufficient.

A virtual reference is a kind of dummy reference that is not used much in real life, and is mainly used to track the activity of an object being garbage collected.

You have done the JVM parameter tuning and parameter configuration. How do YOU check the JVM system default values

Use the -xx :+PrintFlagsFinal argument to see the default value of the argument. The default value is also specific to the garbage collector, such as UseAdaptiveSizePolicy.

7. What are the basic JVM configuration parameters that you have used in your daily work?

Xmx, Xms, Xmn, MetaspaceSize, etc.

You only need to memorize 10 or so for most interviews. It is recommended to memorize only G1 parameters. CMS is a time-consuming, parameterized and obsolete thing. Interview time is limited, so don’t dwell on this, unless you come off too aggressive.

8. Please talk about your understanding of OOM

OOM is a very serious problem, except for the program counter, other memory areas are at risk of overflow. And we work most closely, is the heap overflow. In addition, the meta-space can overflow if the method area is too full. Then there is stack overflow, which is usually less of an issue. There is also the possibility of an overflow outside the heap, which is more difficult to detect.

9. What do you do to check for memory overflow?

(This topic is very large, can be randomly selected from the practice session to summarize, the following example is the most common)

You can give a well-thought-out response:

There are many different scenarios for memory overflow, and the one I encounter most often in my daily work is heap overflow. Once a fault occurred on the line. After the restart, the jstat command was used to find that the Old area kept growing. Using the jmap command, I exported an online stack and analyzed it using MAT. Through the analysis of GC Roots, I found a very large HashMap object, which was originally used by one of my classmates for cache, but it was an unbounded cache, resulting in a continuous increase in heap memory usage. Later, the Cache was changed to Guava’s Cache and weak references were set, and the fault disappeared.

It’s not a brilliant answer, but it’s a common question, and it’s hard to find fault with.

How does the GC garbage collection algorithm relate to the garbage collector?

Commonly used garbage collection algorithms include tag cleaning, tag collation, copy algorithm, etc. Reference counters are one option, but no garbage collector uses this algorithm because of cyclic dependencies.

Many garbage collectors are generational. For the young generation, there are garbage collectors such as Serial and ParNew, and the collection process mainly uses the replication algorithm.

The recycling algorithms of the old era include Serial, CMS, etc., which mainly use the tag clearing and tag sorting algorithm.

The G1, which we use more online, also has the concept of young generation and old generation, but it is a whole heap collector, and its collection objects are small heap areas.

With the G1 in vogue, there’s no need to worry about CMS.

How is the garbage collector configured on production?

The first is the size of memory. Basically, I set an upper limit for each area of memory to avoid overflow problems, such as meta-space. Typically, I set the heap size to about two-thirds of the operating system size (to give other processes and operating systems some time), and G1 is preferred for heaps larger than 8GB.

Next, I’ll do a preliminary optimization of the JVM. For example, adjust the ratio between the younger generation and the older generation according to the rate at which the objects of the older generation ascend.

Next, is the special optimization, the main judgment is based on system capacity, access delay, throughput, etc. Our service is highly concurrent, so time sensitive to STW.

I find the bottleneck by logging detailed GC logs, which are easy to locate using log analysis tools like GCEasy (emphasis). The tool was chosen because GC logging seemed too cumbersome, and GCEasy claims to be a better visualization for AI learning to analyze problems.

How to check the default garbage collector on the server?

This usually takes another argument: -xx :+PrintCommandLineFlags prints all of the arguments, including the garbage collector used.

13. If the CPU usage in the production environment is too high, please talk about your analysis and positioning.

This is a very common wife, but it’s already on the street. If you’re an experienced developer and don’t know, take a look.

First, use the top-h command to get the thread with the highest CPU usage and convert it to hexadecimal.

Then, use the jstack command to get the application’s stack information and search for this hexadecimal. In this way, you can easily find the specific cause of high CPU usage.

If you could, you could just use Arthas instead of having to do all that work.

14. What monitoring and performance analysis tools have you used in JDK?

JPS: Used to display Java processes; Jstat: used to view GC; Jmap: used to dump the heap; Jstack: used to dump stacks; JHSDB: used to view memory information in execution;

Are very common tools, to master. Because the online environment is often too restrictive to use graphical tools. When this happens, the command above can be life-saving.

15. What data does stack frame have?

The JVM operates on a stack, similar to the C stack, where most of the data is stored in the heap and only a small amount of runtime data is stored on the stack.

In the JVM, each thread’s stack element is called a stack frame.

Stack frame includes: local variable table, operand stack, dynamic link, return address and so on.

16. What is JIT?

To improve the efficiency of hot code execution, the virtual machine compiles this code into local platform-specific machine code at runtime and performs various levels of optimization. The Compiler that does this is called a Just In Time Compiler, or JIT Compiler for short.

17. What is the parent delegation mechanism in Java?

It means that all class loaders, except the top level boot class loader, delegate to its parent before loading. And so it goes up one layer at a time until the ancestors are no longer up to it, and it really loads.

Java defaults to this behavior. Of course, there are a lot of nasty operations in Java that break parental behavior, such as SPI (JDBC driver loading), OSGI, etc.

18. What are the cases of broken parental entrustment mechanism?

  1. Tomcat can load class files in its own directory and does not pass them to the parent class loader.

  2. Java SPI, the initiator is BootstrapClassLoader, BootstrapClassLoader is already the top layer. It gets the AppClassLoader directly to load the driver, as opposed to the parent delegate.

19. Briefly describe the garbage collection process

The generation collector has two partitions: old generation and new generation. The default space of the new generation is 1/3 of the total space, and the default space of the old generation is 2/3.

The new generation uses the replication algorithm, and there are three partitions in the new generation: Eden, To Survivor, and From Survivor. Their default ratio is 8:1:1. Its execution process is as follows:

When the Eden area of the young generation is full, the Minor GC (GC) of the young generation is triggered. The specific process is as follows:

  1. After the first GC in Eden, the surviving objects are moved to one of the Survivor partitions (from)

  2. The Eden region will be GC again, and then the replication algorithm will be used to clean Eden and from region together. Surviving objects are copied to the TO section. Next, you just need to clear the FROM section

20. What are the stages of CMS?

CMS has been deprecated. Life is good, time is limited, and further research is not recommended. If you run into problems, go straight to the recycling process.

(1) initial mark (2) concurrent mark (3) concurrent pre-clean (4) concurrent cancelable pre-clean (5) re-mark (6) concurrent clean

Due to the popularity of understanding the Java Virtual Machine, it’s generally ok to skip steps 3 or 4 during an interview.

21. What are the problems with CMS?

(1) Memory fragmentation. The collation phase of the Full GC causes long pauses. (2) Space should be reserved for the allocation of “floating garbage” generated in the collection phase. (3) Use more CPU resources and perform heap scanning while the application is running. (4) Pause times are unpredictable.

These problems led to the use of the more complete G1. Besides, in the era of large memory, the G1 works, so CMS is unnecessary.

22. What are the most important parameters of the G1 garbage collector that you have used?

The most important is MaxGCPauseMillis, which allows you to set G1’s target pause time, which it will try to achieve. G1HeapRegionSize Sets the size of the small heap area. It is usually a power of 2.

InitiatingHeapOccupancyPercent, starts the concurrent GC heap memory usage percentage. G1 uses it to trigger concurrent GC cycles based on the utilization of the entire heap, not just the memory usage of a particular generation, which is 45% by default.

Any more? If you are not an expert, there is no need to expect others to be.

What do GC logs mean by real, user, sys?

Real The actual time spent, which is the time taken from start to finish. For example, if the process is waiting for I/O to complete, this blocking time will also be counted. User refers to the time spent by the process in user Mode. Only the time spent by the process is counted. It refers to multi-core. Sys refers to the amount of CPU time spent by a process in Kernel Mode. Sys refers to the amount of time spent by system calls in the Kernel.

This is for looking at logs. If you don’t read logs, it’s ok not to know. However, the meanings of all three parameters are basically the same wherever you can see them, like the operating system.

24. What can cause metaspace overflow?

Metaspace is uncapped by default, so it is dangerous to leave it unchecked. When there are too many Java classes in an application, such as Spring, a framework that uses dynamic proxies to generate too many classes, metaspace overflows can occur if the footprint exceeds our set point.

So, default is risky, but if you don’t give it enough space, it will overflow.

25. When will out-of-heap memory overflow occur?

The Unsafe class was used to apply for memory, or JNI was used to manipulate memory. This part of memory is not controlled by the JVM and is used without restriction, which is prone to memory overflow.

Does SWAP affect performance?

When the operating system memory is insufficient, some data is written to SWAP. However, SWAP performance is low. If the application has a large number of visits, it needs to apply for and destroy memory frequently, which is prone to stall. In high concurrency scenarios, SWAP is disabled.

What are the troubleshooting ideas of out-of-heap memory?

The memory occupied by the process, you can use the top command to see the value occupied by the RES segment. If this value significantly exceeds the maximum heap memory we set, then there is a large area of out-of-heap memory.

With GDB, you can dump physical memory and usually see its contents. More sophisticated analysis can be done using the Perf tool, or Google’s open source GperfTools. The native functions that require the most memory are easy to find.

Can a key in a HashMap be an ordinary object? What should I pay attention to?

Map keys and values can be of any type. Be careful, however, to overwrite its equals and hashCode methods, otherwise memory leaks will occur.

How to look at deadlocked threads?

Using the jstack command, you can obtain the stack information of the thread. Deadlock messages are prompted in a very obvious place (usually last).

How to write a simple deadlock code?

The frequency of this written test is also quite high (think twice about the company that meets the written test), so here is a direct answer (there are many versions).

public class DeadLockDemo { public static void main(String[] args) { Object object1 = new Object(); Object object2 = new Object(); Thread t1 = new Thread(() -> { synchronized (object1) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (object2) { } } }, "deadlock-demo-1"); t1.start(); Thread t2 = new Thread(() -> { synchronized (object2) { synchronized (object1) { } } }, "deadlock-demo-2"); t2.start(); }}Copy the code

31. What does an Invokedynamic instruction do?

This is a more advanced topic. If you haven’t seen a virtual machine, you generally don’t know. So if you’re not familiar with it, don’t be discouraged, come on! (Little fist hits your chest).

Invokedynamic is a bytecode instruction added to Java7 that allows you to implement some of the functions of dynamically typed languages. The Lambda expressions we use are implemented in bytecode by the Invokedynamic instruction. It functions somewhat like reflection, but is implemented using method handles and is more efficient.

32. What is the mechanism of the volatile keyword? What’s it for?

Variables that use the volatile keyword are immediately synchronized to main memory whenever the value of the variable changes. If a thread wanted to use the variable, it would flush it from main memory to working memory, ensuring that the variable was visible.

A volatile bool variable is used to control the thread’s state.

volatile boolean stop = false; void stop(){ this.stop = true; } void start(){ new Thread(()->{ while (! stop){ //sth } }).start(); }Copy the code

33. What is method inlining?

To reduce the overhead of method calls, you can include short methods, such as getters/setters, in the call scope of the target method. This is the concept of method inlining.

How is the object from the young generation into the old era?

This is an old chestnut. In the following four cases, the object moves from the young generation to the old generation.

  1. If the object is old enough, it is promoted to the old age, usually based on the object’s age.

  2. Dynamic object age determination. Some garbage collection algorithms, such as G1, do not require an age of 15 to advance to the old age, but use dynamic calculations.

  3. Distributive guarantee. When there is not enough Survivor space, it needs to rely on other memory (i.e., older memory) for allocation guarantee. At this point, the object is also assigned directly to the old age.

  4. Objects beyond a certain size will be allocated directly in the old age. However, this value defaults to 0, meaning that all preferred Eden regions are allocated.

35. What is Safepoint?

STW does not only occur during memory reclamation. Now programmers so volume, encountered a few Safepoint problems are relatively large odds.

When GC occurs, the user threads must all stop before garbage collection can take place, which is considered safe for the JVM and stable for the heap.

If a thread is slow to enter SafePoint before GC, the entire JVM waits for the blocked thread, making the overall GC time longer.

36. When do MinorGC, MajorGC and FullGC happen?

MinorGC occurs when there is not enough space in the younger generation. MajorGC refers to the GC of the older generation. MajorGC is usually accompanied by MinorGC.

FullGC has three cases.

  1. When the old age can no longer allocate memory

  2. When you run out of meta space

  3. Displays when system.gc is called. In addition, garbage collectors such as CMS can also have FullGC when promotion failure occurs at MinorGC

37. How many processes are involved in class loading?

Load, validate, prepare, parse, initialize.

38. When does stack overflow occur?

The stack size can be set with the -xss parameter, and stack overflow occurs when the recursion level is too deep. Such as circular calls, recursion and so on.

This article is published by OpenWrite!