So before we start talking about the Java memory area, let’s put up a map of the memory area so we can look at it later.
Note that this article is based on JDK8.
Program counter
First, it is thread private. It is also called the line number indicator of the code. The bytecode interpreter changes the position of the program counter to determine the next line of code to execute.
If a thread is executing a Java method, it records the address of the virtual machine bytecode instruction that is executing, or null if it is a local method.
Java virtual machine stack
It is also thread-private and its declaration cycle is the same as that of the thread. Each thread is created to create a virtual machine stack, which holds a stack frame, and each stack frame corresponds to a method call. Now that you know that the virtual machine stack contains stack frames, it is not difficult to guess what is stored in the virtual machine stack.
The Java virtual machine stack has OOM. If the stack depth requested by the thread is larger than the stack depth or the vm stack can be dynamically expanded, the Java virtual machine stack cannot obtain enough memory during stack expansion, the OOM will be thrown.
- Internal structure of vm stack:
-
Local variation scale:
The main store is the method’s parameters, all primitive types of data and object addresses, and the return address type. It uses a variable slot as the smallest unit of storage. The Java virtual machine does not specify how much memory space a variable slot occupies, but it does specify that a variable slot can hold a data type up to 32 bits. If the data type stored is more than 32 bits, such as long and double, then two variable slots are used for storage.
-
Operand stack:
The operand stack is an advanced and outgoing operand stack. When a method is first executed, a new stack frame is created. This method’s operand stack is empty, which is mainly used to store the intermediate results of the calculation and to temporarily store the variables during the calculation. If the called method has a return value, the return value is pushed into the operand stack of the current stack frame. The operand stack does not use indexes to access data, but pushes and pops to access data.
-
Dynamic linking:
In plain English, a stack frame holds a reference to a method, and when the method is executed, the reference can be taken to the runtime constant pool to find the method.
The purpose of dynamic linking is to convert symbolic references to these methods into direct references to calling methods.
-
Method returns the address:
The value of the program counter is returned at the end of a method’s execution.
So in addition to the normal method exit, there is another case where the method exit is caused by an exception, in which case no value is returned. Exceptions thrown are not logged in the stack frame, but are logged in an exception table.
Local method stack
The Java virtual machine stack serves the Java methods executed by the virtual machine, and the local method stack serves the local methods used by the virtual machine. The JVM, for example, has many methods written in C, which require the native method stack to execute.
The Java heap
The Java heap is the largest chunk of memory in the virtual machine, shared by all threads, and created when the virtual machine is started. Its sole purpose is to hold object instances.
If the interview is asked, do all object instances allocate memory in the heap? This is when you have to answer, no.
With the development of just-in-time compilation technology, especially the increasingly powerful escape analysis technology, allocation on the stack, variable substitution and other optimization methods, let instances in the heap allocation is no longer absolute.
The Java heap is the main area of garbage collection, and there are often new generation, old generation, permanent generation, and so on in the Java heap. It is important to note that this is not a physical memory layout of the Java heap, but a memory layout as a garbage collector.
Methods area
The method area is also shared by threads and is used to store data such as type information loaded by the virtual machine, constants, static variables, and code caches compiled by the just-in-time compiler.
The method area is recyclable by the garbage collector, mainly for type offloading and constant pool collection.
The method area can also generate OOM, and OutOfMemoryError will be raised when the method area cannot meet the new memory allocation requirements.
Run-time constant pool
The runtime constant pool is part of the method area. In addition to the Class version, fields, methods, interfaces, and other information in the Class file, the constant pool table is used to store the various literal and symbolic references generated at compile time.
If you don’t understand the dynamic linking section, look at running the constant pool and scroll back to see if it makes sense.