1. Basic Introduction:
The runtime data Area usually consists of these five parts: Program Counter Register, Java Stack, Native Method Stack, Method Area, and Heap.
2, program counter: [PC register, each thread has a separate PC register]
- Is a small memory space that can be thought of as a line number indicator of the bytecode being executed by the current thread. The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to execute.
- Bytecode instructions, branches, loops, jumps, exception handling, thread recovery, and other basic functions depend on this counter.
- Program Counter Register (Program Counter Register), also known as PC Register. Presumably learned assembler language friend is no stranger to the program counter this concept, in assembly language, the program counter is to point to the CPU registers, it saved is the address of the currently executing program instructions (also can save the next instruction in the address of the storage unit, once the command is executed, the PC register will be updated to the next instruction address), When the CPU needs to execute instructions, you need the current need to execute instructions from the program counter is the address of the storage unit, then to get the address of the access to the command, after get the instruction, the program counter automatically add 1 or pointer to get the address of the next instruction according to the transfer, so, until after all of the instructions.
- Although a program counter in the JVM is not a physical CPU register, as it is in assembly language, the function of a program counter in the JVM is logically equivalent to that of an assembly language program counter, that is, to indicate which instruction to execute.
- Because in the JVM, multithreading is through the switch in turn to gain the CPU thread of execution time, therefore, in any one specific moment, a CPU kernel will only perform a thread in the instruction, therefore, in order to be able to make after each thread in the thread to restore before the switch position of program execution, each thread needs to have their own independent of the program counter, And can not be interfered with each other, otherwise it will affect the normal execution order of the program. Therefore, it is safe to say that program counters are private to each thread.
- According to the JVM specification, if a thread is executing a non-native method, the program counter stores the address of the instruction that is currently being executed. If the thread executes a native method, the value in the program counter is undefined.
- Because the amount of space stored in the program counter does not change with the execution of the program, there is no OutOfMemory (OutOfMemory) for program counters.
3, Java virtual machine stack: [Each thread has its own Java stack, thread-safe]
- The Java Stack, also known as the Java Vitual Machine Stack, is often referred to as the Stack.
- The Java Stack holds Stack frames, and each Stack Frame corresponds to a method being called, Stack frames include Local Variables, Operand Stack, Reference to runtime constant pool of the class to which the current method belongs, and Return address of the method Address) and some additional information.
- When a thread executes a method, it creates a corresponding stack frame and pushes the stack frame. When the method completes execution, the stack frame is pushed off the stack. Therefore, the stack frame corresponding to the method currently executed by the thread must be at the top of the Java stack.
- At this point, you should be able to understand why when using recursive method is easy to cause the phenomenon of memory and why the stack area space without the programmer to management (of course in Java, programmers need not related to basic memory allocation and release, because the Java garbage collection has its own), This part of the space allocation and release are automatically implemented by the system. For all programming languages, this part of the stack space is opaque to the programmer. The recursion calls its own method before it finishes executing, creating a stack frame again.
- Because each thread may be executing a different method, each thread has its own Java virtual machine stack that does not interfere with each other.
- The following diagram shows a Java stack model:
3.1 Local Variable Table:
- Stores local variables in a method (including non-static variables declared in a method), function parameters. For variables of primitive data types, its value is stored directly, and for variables of reference types, a reference to an object is stored.
- When Java is compiled into a Class file, the maximum size of the table of local variables that this method needs to allocate is determined.
- Variable Slot:
- The capacity of a local variable table is in the smallest unit of variable slots. Each variable slot can store 32 bits of memory, such as Boolean, byte, CHAR, short, int, float, and reference.
- For 64-bit data types (long, double), the virtual machine allocates two contiguous Slot Spaces in high-aligned mode, splitting one long and double read and write into two 32-bit reads and writes.
- For reference object instances, the virtual machine can directly or indirectly look up two points of the object from the reference: the starting address index of the data stored in the Java heap. Owning data Type The type of data stored in the method area.
- When a method is executed, the virtual machine uses the local variable table to pass parameter values to the parameter variable list. If an instance method is executed, the Slot of the zeroth index in the local variable table defaults to a reference to the instance of the object to which the method belongs. (The implied parameter can be queried in the method using the keyword this.) The rest of the parameters are sorted in parameter list order, occupying the local variable Slot starting from 1.
- Variable Slot multiplexing:
- In order to save stack frame space as much as possible, slots in the local variable table can be reused, which means that when the instruction of the PC counter is out of scope for a variable, the Slot corresponding to that variable can be used by other variables.
- Advantages: Saves stack frame space.
- Disadvantages: Affects the garbage collection behavior of the system. (If a large method occupies a large number of slots, the garbage collector cannot reclaim the memory in a timely manner if the Slot is not assigned or null is set after executing the scope of the method.)
3.2 Operand stack:
- If any intermediate operations need to be performed, the operand stack acts as a runtime workspace to execute the instructions.
- Like local variable tables, the maximum size of the operand stack to be allocated by this method is determined at compile time.
- Each element of the operand stack can be any Java data type, including long and double. 32-bit data types occupy 1 stack capacity and 64-bit data types occupy 2 stack capacity.
- When a method is first executed, the operand stack of the method is empty. During the execution of the method, various bytecode instructions are written to and extracted from the operand stack.
- For example, arithmetic operations are performed through the operand stack, or arguments are passed through the operand stack when other methods are called.
3.3 references to runtime constant pools
- A reference to a runtime constant is necessary because it is possible to use a class constant during method execution.
3.4. Method return address:
- When a method is finished, it returns to where it was called before, so a method return address must be stored in the stack frame.
- Once a method is executed, there are only two ways to exit the method:
- Exception exit: An exception encountered during the execution of a method and not handled causes the method to exit.
- Method return instruction: When the execution engine encounters a bytecode instruction returned by a method, there may be a return value passed to the upper method caller. This exit is called the normal completion exit.
- After a method exits, it is necessary to return to the point where the method was called in order for the program to continue executing. The method may need to store some information in the stack frame when it returns.
- In general, when a method exits normally, the value of the caller’s PC counter can be used as the return address, and this counter value is stored in the stack frame.
- When a method exits abnormally, the return address is determined by the exception handler table, which is generally not stored in the stack frame.
4. Local method stack: [Each thread has its own local method stack]
- The native method stack works and works very similar to the Java stack. The difference is simply that the Java stack serves the execution of Java methods, while the Native Method stack serves the execution of Native methods.
- The local method stack holds local method information. For each thread, a separate local method stack is created.
5, heap: [each JVM also has only one heap area, thread sharing, thread unsafe, main memory]
- If they are member variables, or references to objects defined outside the method, they are stored in the heap.
- The Java heap is used to store the objects themselves, their corresponding member variables, and arrays (array references, of course, are stored in the Java stack). Unlike C, however, in Java, programmers don’t have to worry about space release. Java’s garbage collection mechanism takes care of it automatically. Therefore, this space is also the primary area managed by the Java garbage collector. In addition, the heap is shared by all threads, and there is only one heap in the JVM.
- -Xms: indicates the minimum memory size
- -Xmx: indicates the maximum available size of the heap
- -xMN: Size of Cenozoic space
- -xxSURVIVorRatio: defines the ratio of Eden zones and Survivor zones (From survivorzone or To survivorzone) in the new generation. The default value is 8, that is, Eden accounts for 8/10 of the new generation, and From survivorzone and To survivorZone account for 1/10 of the new generation respectively.
Calculation formula: Eden = (R*Y)/(R+1+1) From = Y/(R+1+1) To = Y/(R+1+1) R: SurvivorRatio Y: space size of Cenozoic eraCopy the code
6, method area: [only one method area per JVM, thread sharing, thread unsafe]
- The method area is also a very important area in the JVM, which, like the heap, is shared by threads. In the method area, you store all class-level data (including the name of the class, and so on), static variables, static methods, constants, compiler compiled code, run-time constant pools, and so on.
- In the JVM specification, method areas are not mandated to implement garbage collection. Many people tend to refer to the method area as a “permanent generation” because the HotSpot VIRTUAL machine implements the method area in a permanent generation so that the JVM’s garbage collector can manage this area in the same way as the heap area without having to design a garbage collection mechanism for it. However, since JDK7, the Hotspot VIRTUAL machine has moved the pool of string constants that used to be in the persistent generation to the heap.
- The Runtime Constant Pool is part of the method area.
7, permanent generation: [PermGen]
- The difference between a method area and a “PermGen space” The former is a specification for the JVM, while the latter is an implementation of the JVM specification, and only HotSpot has a “PermGen space”, while there is no “PermGen space” for other types of virtual machines such as JRockit (Oracle), J9 (IBM).
- So the permanent generation is the HotSpt virtual machine’s implementation of the method area.
- -xx :PermSize: indicates the initial size of the permanent generation.
- -xx :MaxPermSize: indicates the maximum space of the permanent generation.
8. Meta-space: 【Metaspace】
- In JDK 1.8, there is no method area (permanent generation). Instead, the method area is placed directly in a region of Native Memory that is not connected to the heap, called the meta-space.
- In JDK 1.8, the string constant pool is still in the heap, and the runtime constant pool is still in the method area, but the method area implementation has been changed from permanent generation to Metaspace.
- Similar in nature to permanent generations, meta-spaces are implementations of method areas in the JVM specification. However, the biggest difference between a meta-space and a permanent generation is that the meta-space is not in the virtual machine, but uses local memory. Therefore, by default, the size of a meta-space is limited only by local memory, but you can specify the size of a meta-space with the following parameters:
- -xx :MetaspaceSize: the initial size of the space. This value triggers garbage collection for type offloading and the GC adjusts the value. If a large amount of space is freed, the value is reduced. If very little space is freed, increase this value appropriately until MaxMetaspaceSize is exceeded.
- -xx :MaxMetaspaceSize: indicates the maximum space. By default, there is no limit.
- -xx :MinMetaspaceFreeRatio: Minimum percentage of Metaspace free space capacity after GC to reduce garbage collection caused by space allocation.
- -xx :MaxMetaspaceFreeRatio: The maximum percentage of Metaspace free space capacity after GC, reduced to garbage collection caused by free space.
9, why remove permanent generation and introduce meta space:
9.1 Disadvantages of permanent generation:
- String to exist permanent generation, real use easy problems, due to the permanent generation memory often enough or memory leaks occur, when abnormal “Java. Lang. OutOfMemoryError: PermGen”
- It is difficult to determine the size of information about classes and methods, so it is difficult to specify the size of permanent generation. If the size is too small, it is easy to overflow the permanent generation, while if the size is too large, it is easy to overflow the old generation.
- Persistent generation introduces unnecessary complexity to the GC and is inefficient for collection.
9.2 Benefits of introducing meta-space:
- Since the metadata of a class is allocated in local memory, the maximum allocatable space of the metadata space is the memory space available to the system. As a result, we don’t experience memory overflow errors with persistent generations, and we don’t have things like leaked data moving to swap. End users can set a maximum amount of free space for the meta-space; if this is not set, the JVM automatically dynamically increases the size of the meta-space based on the metadata size of the class.