The JVM has an automatic memory management mechanism, and Java does not need to write paired Delete/free code for each new operation, as in C + +, and is not prone to memory leaks and overflows. The JVM memory area mainly includes the following parts: program counter, Java Virtual machine stack, local method Stack, Java heap, method area.
Program Counter
The program counter can be considered a byte-code line number indicator executed by the current thread, and if the native method is currently executing, the value of the counter is null (Undefined). In the JVM conceptual model, the bytecode interpreter selects the next byte-code instruction that needs to be executed by changing the value of the counter, and the branch, loop, jump, exception handling, thread reply, etc. all depend on the program counter to complete.
Each thread has a separate counter to ensure that the thread switch is restored to the correct location, so the program counter is thread-isolated in this area of memory. The region is the only one that does not specify any outofmemoryerror.
Java Virtual machine stack
The Java Virtual machine stack is also thread-isolated, one thread at a time, and the same life cycle as the thread.
It is composed of stack frames, a stack frame represents a method of invocation, the thread creates a stack frame each time the method call executes and then presses the stack, and the stack frame is used to store information such as local variables, operands, dynamic links, return addresses, and so on. The stack frame out of the stack after the execution of the method is completed. The stack memory we usually refer to is the stack.
Methods in one thread might also invoke other methods, which would constitute a chain of method calls, and the chain could be long and each thread would have a method in its execution state. For the execution engine, only the stack frame at the top of the active line stacks is valid, called the current stack frame, and the stack frame associated method is called the current method.
2 kinds of exceptions to the virtual machine stack:
- Stackoverflowerror: The call chain is too long, and the thread request depth is greater than the JVM allowed depth.
- OutOfMemoryError: Unable to request enough memory when the virtual machine stack dynamically expands.
Local Method Stack
The local method stack is similar to the virtual machine stack, which is the stack that the virtual machine thread invokes when the native method executes. Java can invoke programs written in other languages (such as C) through the Java Local interface Jni (Java Native Interface), where a Native modifier is used in Java to describe a method as a local method.
The local method stack is not mandatory in the virtual machine specification, and the virtual machine can be implemented freely, so it can be not a byte code. If it is implemented in bytecode, the virtual machine stack local method stack can be combined, in fact, OpenJDK and Sunjdk's own hotspot virtual machine is directly the virtual machine stack and the local method stack is merged.
Java Heap
The Java heap is the largest chunk of memory managed by the JVM, shared by threads, and created when the JVM is started. The heap holds all object instances and arrays. However, in the JIT (Just-in-time) and escape scenarios, stack allocation, scalar substitution may allocate object instances on the stack.
A heap is a primary area that is managed by the Java garbage collector (so many times it is called a GC heap). From the point of view of GC recycling, because the GC is basically used in the generation of collection algorithm, so the heap memory structure can also be divided into: The new generation and the old age, the permanent generation, and then a little bit of Eden Space, from Survivor space, to survivor space. It is noteworthy that, starting from JKD1.7, the permanent generation perm is gradually removed, and the latest JDK1.8 has used the meta-space (metaspace) instead of the permanent generation.
The Java heap can be like disk space, allowing logical continuous and physical discontinuities. If there is no memory in the heap to complete the instance assignment and cannot be extended, an OutOfMemoryError exception will be thrown.
Method Area
The Java Virtual machine specification describes the method area as a logical part of the heap, but is called a non-heap (non-heap), and in Sun's hotspot virtual machine, the method area can be understood as a permanent generation (Permanent Generation) in the heap memory block. It is thread-shared.
method is used to store data such as loaded class information, constants, static variables, and JIT-compiled code (class methods) when loading a class file.
basic information about the class
The fully qualified name of each class, the fully qualified name of the direct superclass of each class (which can be constrained type conversions), whether the class is a class or an interface, an access modifier of that type, and an ordered list of fully qualified names for the direct hyper-interface.
run a constant-rate pool
Chang (Constant pool Table), which holds various literals, symbolic references, literal strings, final variable values, class names, and method name constants generated at compile time, which are stored in the run-time pool of the method area after the class is loaded. They are accessed as an array, a bridge to invoke methods, relate to classes, and object to classes.
In addition to the constants of the class file generated during the compilation period, the run-time pool can also store new constants generated during the run of the program, and it is common to add new constant methods with the Internd () method of the String class. String.intern () is a native method that returns a reference to the string in the constant pool if the run-time pool already contains a string equal to the contents of this string object, and if not, creates the same string as the string content in the constant pool and returns a reference to the string created in the constant pool. However, the implementation of the JDK7 intern () method is different, and when the string is not present in the constant pool, instead of creating the same string as this string content in the constant pool, the reference to the string that first appears in the heap is recorded in the constant pool and returned.
However, JDK1.7 before running the constant pool is part of the method area, and the JDK1.7 and later versions have moved the run-time constant pool from the method area and have opened up an area in the heap (heap) to store the running constant pool.
Field Information
Field information holds information about each field (instance variable) declared in a class, including the name, type, and modifier of the field. such as privatestringa= ""; A is the field name, string is the descriptor, and private is the modifier.
method Information
The information for each method declared in the class, including the method name, the return value type, the parameter type, the modifier, the exception, and the method's bytecode. (at compile time, the method's local variable table, operand stack size, etc. have been fully determined and stored in the bytecode, and loaded, with the class loaded into the method area.) At run time, when a virtual machine thread invokes a method, it obtains a symbolic reference from a constant pool, then resolves the actual address of the method at run time, and finally links the code in the current class or interface with the code in the other class or interface through the fully qualified name, method, and field descriptor in the constant pool.
Static Variables
A class variable that is shared by all instance objects of the class. The static area of the method area is dedicated to static variables and static blocks.
A reference to a class ClassLoader
A reference to the class loader for the class.
references to class classes
The virtual machine creates a class instance for each loaded type, which is used to represent the loaded class.
The Java Virtual Machine specification specifies that the method area can throw outofmemoryerror.
Direct Memory
Direct memory is not part of the JVM Runtime data area, nor is it a memory area defined in the Java Virtual Machine specification. But this part of the memory is also used frequently, and it can also cause outofmemoryerror anomalies to occur.
The allocation of native direct memory is not limited by the size of the Java heap, but it is still subject to the size of the native total memory (including RAM and swap or paging files) and the processor addressing space, resulting in a OutOfMemoryError exception when dynamic scaling occurs.
Summary
What happens to classes and objects in memory at run time? And how do various types of variables and methods interact at run time?
- When the program runs, the class is in the method area, and the instance object itself is inside the heap.
- Method byte code in the method area.
- The thread invokes the method when it executes the stack frame and presses the stack, the parameter of the method and local variables in the local variable table of the stack frame.
- An instance variable of an object is in the heap along with an object, so each thread can share an instance variable that accesses the object.
- Static variables in the method area, all objects are shared. Constants such as string constants are run at constant-time pools.
- The methods that each thread calls, through the objects within the heap, the static data of the method area, can share the interaction information.
Reference
1, Zhou Zhiming, in-depth understanding of Java Virtual machines: JVM advanced features and best practices, mechanical industry Press
"In-depth understanding of the JVM": Java Memory Area