Make it a habit!

Hi, everyone, today I have sorted out some interview questions for JVM. The reason is that my junior student scanned numerous interview questions on the Internet, and then went to Ali for an interview with full confidence two days ago. Unexpectedly, HR asked him “How to judge whether a candidate is alive? (or GC object determination method) and then stuck…. I really want to beat him to death, this is not I told him to see the knowledge point?

Then my junior told me that he had brushed many interview questions on the Internet, but did not see this one. At that time my heart ten thousand grass mud horse ran through… Well, in order to make sure you don’t get rejected by the JVM, I’ve looked at all the interview questions from the big companies this year and compiled these must-ask questions for the JVM

As a reading benefit, xiaobi also organized all the questions + solutions into a PDF document, if you need to download by yourself.

1. Memory model and partition, need to detail what to put in each partition.

The JVM is divided into a heap and a stack, and a method section. The initialized objects are placed in the heap, the references are placed in the stack, and the class constant pool (static constants and static variables) is placed in the method section.

  • Method area: mainly stores class information, constant pool (static constants and static variables), compiled code (bytecode) and other data
  • Heap: Initialized objects, member variables (non-static variables), and all object instances and arrays are allocated on the heap
  • Stack: The structure of the stack is composed of stack frames. When a method is called, a frame is pressed into it. The local variable table, operand stack, method exit and other information are stored on the frame
  • Native method stack: Primarily serves Native methods
  • Program counter: Records the line number executed by the current thread

2. Partitions in the heap: Eden, Survival (from+ to), old age, their respective characteristics.

The heap is divided into the new generation and the old generation (Java8 uses Metaspace instead of permanent generation). The new generation contains Eden+Survivor zone, which is divided into FROM and to zones. After one or more GCS, surviving objects are moved to the old area. When the JVM runs out of memory, the Full GC is triggered to clean up the old area of the JVM. When the new area of the JVM is Full, the YGC is triggered. Since cleaning only the objects that need to be deleted will result in memory fragmentation, Eden is generally cleaned completely and then the memory is decluttered. The next GC, the next Survive is used and recycled. If there is an object too big for the new generation to put, they will use the guarantee of the old age and put it directly into the old age. Because the JVM assumes that large objects tend to live for a long time.

3. Object creation method, object memory allocation, object access location.

New an object

4. Two determination methods of GC:

  • Reference counting: this means that the object is +1 if it is referenced somewhere, -1 if it fails, and recycled when it is 0. But the JVM doesn’t use this method because it can’t determine whether A refers to B and B refers to A
  • Cherry-by-reference: a chain that can reach the GC ROOT can be reclaimed if it can’t reach the GC ROOT

5. What is SafePoint

For example, the VMThread must wait until all the Java threads enter SafePoint to start GC

  1. The end of the loop (in case of a large loop, it stays out of SafePoint while other threads wait for it to enter SafePoint)

  2. Method before returning

  3. After calling the method’s call

  4. The location where the exception was thrown

6. What are the principles and characteristics of the three GC collection methods: tag clearing, tag sorting and copy algorithm? What are your thoughts on how to optimize the collection method?

Mark first and then remove after marking, which is not efficient and will produce debris

Replication algorithm: Divided into 8:1 Eden region and survivor region, which is YGC mentioned above

Tagging: After tagging, move all surviving objects toward one end

7. What are the GC collectors? Features of CMS collector and G1 collector.

Parallel collector: The serial collector uses a separate thread for collection, and the service has pause time for GC

Serial collector: Multithreading is used for secondary collection

The CMS collector is implemented based on a “mark-clean” algorithm, with multiple marks being cleared

G1 is a collector based on a mark-tidy algorithm as a whole, and a copy algorithm locally (between two regions)

8. When will the Minor and Full GC occur?

MGC, also known as YGC, occurs when the JVM runs out of memory

9. Several common memory debugging tools: Jmap, JStack, JConsole, jhat

Jstack can see the current stack, JMap can see the memory, jhat can dump heap information mat (eclipse also need to know)

10. Several processes of class loading:

Load, validate, prepare, parse, initialize. Then load and unload the generated class object into memory by fully qualified name, and then verify the class file, including file format verification, metadata verification, bytecode verification, etc. Preparation is to allocate memory for this object. Parsing is the conversion of symbolic references to direct references (pointer references), and initialization is the code that starts executing the constructor

##11. What are the partitions of JVM memory, and what does each partition do?

Java virtual machines are divided into the following areas:

Methods area:

  1. Sometimes it’s a permanent generation. Garbage collection rarely happens in this area, but it doesn’t mean that GC doesn’t happen, here

The GC is mainly about unloading constant pools and types in the method area

  1. The method area is used to store information about classes that have been loaded by the virtual machine, constants, static variables, and just-in-time compiler compilations

Code and other data.

  1. This area is shared by threads.

  2. The method area has a pool of runtime constants to store literal and symbolic references generated by static compilation. The constant pool

It is dynamic, which means that constants are not necessarily determined at compile time; constants generated at run time are also stored in the constant pool.

Virtual machine stack:

  1. Virtual machine also is what we usually call stack memory, its service for Java methods, each method at the time of execution will create a stack frames and local variables are used to store, the operand stack, and dynamically linked methods, such as export will create a stack frames and local variables are used to store, the operand stack method, dynamic link and export information, etc.

  2. The virtual stack is thread-private and has the same lifetime as the thread.

  3. The local variable table stores the basic data types, the returnAddress type (the address that points to a bytecode instruction), and the object reference, which may be a pointer to the object’s starting address, a handle to the object, or a location associated with the object. The memory space required for local variables is determined between compilers

4. Operand stack is mainly used to store operation results and operands. It is different from local variable table that is accessed by index, but the way of pushing and pushing

5. Each stack frame contains a reference to the method that the stack frame belongs to in the runtime constant pool, which is held to support dynamic concatenation during method calls. Dynamic linking is the conversion of symbolic references in a constant pool to direct references at run time.

Local method stack

The local method stack is similar to the virtual machine stack, except that the local method stack serves the Native method. Heap The Java heap is a chunk of memory shared by all threads, created at virtual machine startup, where almost all object instances are created, so garbage collection often occurs in this area.

Program counter

Since the memory space is small, the bytecode interpreter can select the next bytecode instruction to be executed by changing the value of this counter. Branches, loops, jumps, exception handling, thread recovery and other functions need to be completed by this counter. This area of memory is the only area where the Java Virtual Machine specification does not specify any OOM condition.

If you find this article helpful, please like + favorites + forward!! Due to the space problem, I will not show one by one here, need JVM must ask the interview questions of friends, can get their own JVM must ask the interview questions