JVM common interview questions. question answering ideas

Idea: Draw a JVM memory model diagram for the interviewer, and describe the definition, function, and possible problems of each module, such as stack overflow.

Answer:

  • JVM memory structure

insert image description here

  1. Program counter: The line number indicator of the bytecode executed by the current thread, used to record the byte instruction address of the virtual machine being executed, private to the thread.

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

  3. Native method stack: Similar to the virtual stack, except that it serves the Native method, and the thread is private.

  4. Java heap: The largest piece of java memory, all object instances and arrays are stored in the java heap, where the GC reclaims, shared by threads.

  5. Method area: store loaded class information, constants, static variables, code data compiled by the real-time compiler, etc. (That is, the permanent belt), the recycling target is mainly the recycling of the constant pool and the unloading of the type, shared by each thread.


[](() 3. Why is the JVM memory divided into the new generation, the old generation, and the permanent generation. Why is the new generation divided into Eden and Survivor?

Idea: Let’s talk about the JAVA heap and the division of the new generation first, then talk about the transformation between them, and the configuration of some parameters between them (such as: –XX:NewRatio, –XX:SurvivorRatio, etc.), and then explain why this division is necessary , it is best to add a little bit of your own understanding.

Answer:

Shared memory area division

  • Shared memory area = persistent zone + heap

  • Persistence zone = method area + other

  • Java heap = old generation + new generation

  • New generation = Eden + S0 + S1

Configuration of some parameters

  • By default, the ratio of the young generation ( Young ) to the old generation ( Old ) is 1:2, which can be –XX:NewRatioconfigured through parameters.

  • By default, Edem : from : to = 8 : 1 : 1 (can be set by parameter –XX:SurvivorRatio).

  • Objects in the Survivor area are copied 15 times (corresponding to the virtual machine parameter -XX:+MaxTenuringThreshold).

Why is it divided into Eden and Survivor? Why are there two Survivor areas?

  • If there is no Survivor, every time a Minor GC is performed in the Eden area, the surviving objects will be sent to the old generation. the old age is soon to be

Fill up and trigger Major GC. The memory space of the old generation is much larger than that of the new generation, and it takes more time to perform a Full GC than Minor GC

Much longer, so it needs to be divided into Eden and Survivor.

  • The significance of Survivor is to reduce the number of objects sent to the old age, thereby reducing the occurrence of Full GC. Survivor's pre-screening

It is guaranteed that only objects that can survive 16 Minor GCs in the new generation will be sent to the old generation.

  • The biggest advantage of setting up two Survivor areas is to solve the fragmentation. The newly created object is in Eden and has experienced Minor once.

GC, the surviving objects in Eden will be moved to the first survivor space S0, and Eden will be emptied; wait until the Eden area is full again

Then, Minor GC will be triggered again, and the surviving objects in Eden and S0 will be copied and sent to the second survivor space

S1 (this process is very important, because this copy algorithm ensures that the surviving objects from S0 and Eden in S1 occupy continuous

memory space to avoid fragmentation).


[](() 4. What is a complete GC process in the JVM, and how objects are promoted to the old generation

Idea: First describe the Java heap memory division, then explain Minor GC, Major GC, full GC, and describe the conversion process between them.

Answer:

  • Ava heap = old generation + new generation

  • New generation = Eden + S0 + S1

  • When the space in the Eden area is full, the Java virtual machine triggers a Minor GC to collect garbage in the new generation, and the surviving objects will be transferred to the Survivor area.

  • Large objects (Java objects that require a large amount of continuous memory space, such as very long strings) directly enter the old state; if the object is born in Eden, survives after the first Minor GC, and is accommodated by Survivor, the age If it is set to 1, every time a Minor GC is passed, the age will be +1, and if the age exceeds a certain limit (15) , it will be promoted to the old age. That is, long-lived objects enter the old state .

  • The old age is full and cannot accommodate more objects . After Minor GC, Full GC is usually performed, and Full GC cleans up the entire memory

Heap – includes Young Generation and Old Generation .

  • Major GC occurs in the GC of the old age, cleans the old area , and is often accompanied by at least one Minor GC, **10 slower than Minor GC

more than double**.


[](()5. What kinds of garbage collectors do you know, their respective advantages and disadvantages, focus on cms and G1, including principles, processes, advantages and disadvantages.

Ideas: Be sure to remember typical garbage collectors, especially cms and G1, their principles and differences, and the garbage collection algorithms involved.

my answer:

Several Garbage Collectors

  • Serial collector: Single-threaded collector, when collecting garbage, must stop the world and use the copy algorithm.

  • ParNew collector: The multi-threaded version of the Serial collector also needs to stop the world and copy the algorithm.

  • Parallel Scavenge collector: The new generation collector, the collector of the replication algorithm, the concurrent multi-threaded collector, the goal is to achieve

to a manageable throughput. If the virtual machine runs for a total of 100 minutes in "Analysis of Java Interview Questions in First-tier Manufacturers + Back-end Development Study Notes + Latest Architecture Explanation Video + Practical Project Source Code Handouts" free open source prestige search official account [Programming Advanced Road], 1 of which will be spent on garbage minutes, the throughput is 99%.

  • Serial Old Collector: It is an old generation version of the Serial Collector, a single-threaded collector, using a markup algorithm.

  • Parallel Old collector: It is an old version of the Parallel Scavenge collector, using multi-threaded, mark-sort algorithm.

  • CMS (Concurrent Mark Sweep) collector: It is a collector that aims to obtain the shortest recovery pause time, marked

Note the clearing algorithm, the operation process: initial marking, concurrent marking, re-marking, concurrent clearing, and a large amount of space debris will be generated at the end of the collection

piece.

  • G1 Collector: Implementation of mark finishing algorithm, the operation process mainly includes the following: initial mark, concurrent mark, final mark, screening

mark. No space debris is generated, and the pause can be precisely controlled.

Difference between CMS collector and G1 collector

  • The CMS collector is the collector of the old generation and can be used together with the Serial and ParNew collectors of the new generation;

  • The collection range of the G1 collector is the old generation and the new generation, and does not need to be used in combination with other collectors; the CMS collector is a collector that aims at the minimum pause time;

  • The G1 collector predicts garbage collection pause times

  • The CMS collector uses the "mark-clear" algorithm for garbage collection, which is prone to memory fragmentation

  • The G1 collector uses the "mark-sort" algorithm to perform space integration and reduce memory space fragmentation.


[](() 6. How much do you know about the JVM memory model, such as reordering, memory barriers, happen-before, main memory, and working memory.

Idea: First draw the Java memory model diagram, combined with the example of volatile, to explain what reordering and memory barriers are. It is best to write the following demo description to the interviewer.

Answer:

1. Java memory model diagram

insert image description here

  • The Java memory model stipulates that all variables are stored in the main memory, and each thread has its own working memory. The working memory of the thread stores the copy of the main memory copy of the variables used in the thread. All operations must be performed in working memory, and cannot directly read and write main memory. Different threads cannot directly access variables in each other's working memory, and the transfer of variables between threads requires data synchronization between their own working memory and main memory.

2. Instruction reordering

Guess you like

Origin blog.csdn.net/AK774S/article/details/124685675