JVM knowledge that the interview must ask-JVM feature learning

Runtime data area

During the execution of the Java program, the Java virtual machine divides the memory it manages into several different areas. These areas have their own purposes, as well as the time of creation and destruction, and some areas keep on starting with the virtual machine process. Exist, some areas are created and destroyed depending on the start and end of user threads. The memory managed by the Java virtual machine includes the following areas.

Memory partition

The shaded part is the data area shared by all threads, and the non-shaded part is the thread-isolated data area.

Program counter

The Program Counter Register is a small memory space, which can be regarded as the line number indicator of the bytecode executed by the current thread. The bytecode interpreter is selected by changing the value of this counter when it works. The next bytecode instruction that needs to be executed is an indicator of program control flow. Basic functions such as branching, looping, jumping, exception handling, and thread recovery all need to rely on this counter to complete.

At any point in time, a processor can only execute instructions in one thread. In order to return to the correct position after thread switching, each thread needs to have an independent program counter, and the counters between the threads do not affect each other. Therefore, the memory space of the program counter is private to each thread.

Also note: when the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed; when the thread is executing a native method, the counter value should be empty (Undefined). And this memory area will not appear OutOfMemoryError.

Java virtual machine stack

The Java Virtual Machine Stack is private to the thread, and its life cycle is the same as that of the thread. When each method is executed, the Java virtual machine will synchronously create a stack frame (Stack Frame) to store information such as the local variable table, operand stack, dynamic connection, method exit, etc. The process from when each method is called to the completion of execution corresponds to the process of a stack frame from pushing to popping in the virtual machine stack.

Java virtual machine stack

The local variable table stores the basic data types (boolean, byte, char, short, int, float, long, double) and object references of various Java virtual machines that can be known during compilation.

The memory required by the local variable table is allocated during compilation, and the size of the local variable table will not be changed during the running of the method.

If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError exception will be thrown; if the stack capacity of the Java virtual machine can be dynamically expanded, an OutOfMemoryError exception will be thrown when the stack is expanded and cannot apply for enough memory.

Native method stack

The native method stack serves the native method used by the virtual machine. Its function is similar to the Java virtual machine stack. Of course, the local method stack will also throw StackOverflowError and OutOfMemoryError exceptions respectively when the stack depth overflows or the stack expansion fails.

Java heap

The Java heap is a memory area shared by all threads and is created when the virtual machine starts. The sole purpose of this memory area is to store object instances, and "almost" all object instances in the Java world allocate memory here. Of course, as technology develops, object instances may also be distributed elsewhere.

The purpose of subdividing the Java heap is just to better reclaim memory or allocate memory faster.

The Java heap can be in a physically discontinuous memory space, but logically it should be considered continuous. However, for large objects (typically array objects), most virtual machine implementations are likely to require contiguous memory space due to simple implementation and storage efficiency.

If there is no memory in the Java heap to complete the instance allocation, and the heap can no longer be expanded, the Java virtual machine will throw an OutOfMemoryError exception.

heap

The picture above shows the structure in the Java heap.

On the principle of object allocation in the heap:

Objects are allocated first in the Eden area

Big objects enter the old age directly

Long-lived objects will enter the old age

Method area

The Method Area, like the Java heap, is a memory area shared by each thread. It is used to store data such as type information, constants, static variables, and code cache compiled by the JIT compiler that have been loaded by the virtual machine.

HotSpot of JDK 7 has moved out the string constant pool and static variables that were originally placed in the permanent generation, but in JDK 8, the concept of permanent generation is finally completely abandoned, and it is implemented in local memory like JRockit and J9. Metaspace instead, moved all the remaining content (mainly type information) of the permanent generation in JDK 7 to the metaspace.

Note that in the above paragraph, JDK8 has no permanent generation, but uses metaspace. In JDK7, many people liked to confuse the method area with the permanent generation.

Garbage collection is indeed relatively rare in this area, but it is not that the data enters the method area as "permanent" as the name of the permanent generation. The memory recovery goal of this area is mainly for the recovery of the constant pool and the unloading of types. If the method area cannot meet the new memory allocation requirements, an OutOfMemoryError exception will be thrown.

Runtime constant pool

Runtime Constant Pool is part of the method area. In addition to the description information of the class version, fields, methods, interfaces, etc. in the Class file, there is also a constant pool table (Constant Pool Table), which is used to store various literal and symbolic references generated during compilation. This part The content will be stored in the runtime constant pool in the method area after the class is loaded.

The runtime constant pool is a part of the method area. When the constant pool can no longer apply for memory, an OutOfMemoryError will be thrown.

Direct memory

Direct memory (Direct Memory) is not part of the data area of ​​the virtual machine when it is running, but this part of the memory is also frequently used and may also cause OutOfMemoryError.

Object

In the HotSpot virtual machine, the storage layout of objects in the heap memory can be divided into three parts: object header (Header), instance data (Instance Data) and alignment padding (Padding).

The object header part of the HotSpot virtual machine object includes two types of information. The first type is used to store the runtime data of the object itself, also called "Mark Word". The "Mark Word" information is as follows:

Mark Word

Our Java program will manipulate the specific objects on the heap through the reference data on the stack. The mainstream access methods mainly use handle and direct pointer.

  • Handle access: A piece of memory may be divided into the Java heap as the handle pool. The handle address of the object is stored in the reference, and the handle contains the specific address information of the object instance data and type data.

  • Direct pointer access: The memory layout of the object in the Java heap must consider how to place information related to the access type data. The reference stored in the object is directly the address of the object. If you only access the object itself, there is no need for an additional indirect access overhead

Generally, HotSpot virtual machine uses the second direct pointer access.

Direct pointer

Here I raise a question for everyone to think about:

How to write code for heap overflow exception and stack overflow exception?

This is a frequently asked question in interviews. Regarding heap overflow exceptions, you can think about creating objects so that they can burst the heap memory; regarding stack overflow exceptions, you can think about how to fill the stack with stack frames.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/109173690