Cross-platform features of the Java language
Different operating systems have different JDK versions that implement JVMS, which dynamically allocate memory for JAVA programs.
The memory model of the JVM
Class-loading subsystem: the part of the JVM that loads. Class files, written in C++
Bytecode execution engine: constantly reading instructions and executing them
Runtime data area
Stack: The thread is private and has the same life cycle as the thread. The virtual machine stack describes the execution model of Java methods, and a method call corresponds to the process of a stack frame off the stack. The stack frame that is at the top of the stack is valid in an active thread and is called the current stack frame (all commands executed by the execution engine are directed against the current stack frame). The method associated with it is called the current method. Two exceptions are specified for the virtual stack: a StackOverFlow exception is raised if the stack depth requested by the thread is greater than the depth allowed by the virtual machine, and an OOM exception is raised if the virtual machine stack cannot allocate sufficient memory space. -xss specifies the stack size for each thread. The stack frame consists of several parts: local variable table, operand stack, dynamic link, method exit.
Local variable table: A local variable table is a storage space for a set of variable values used to store method parameters and local variables defined within a method. The local variable table is based on the variable slot. The VM uses the local variable table to complete the process of transferring parameter values to the parameter variable list. If the instance method is non-static, the first part of the local variable table stores the reference of the owning object instance, which is referenced by this in the method. The system does not set initial values for local variables.
Example code:
Local variation scale:
Bytecode instructions:
Iconst_1: Push int (1) to the top of the stack
Istore_1: Stores the top int value into the second local variable.
Iload_1: Load int value from local variable 1 (onto operand stack)
Operand stack: The virtual machine uses the operand stack as its workspace — most instructions pop data from here, perform operations and then push back into the operand stack.
Dynamic linking: As the virtual machine runs, the runtime constant pool holds a large number of symbolic references that can be viewed as indirect references to each method. If representatives to act on behalf of stack frames A method to call the method that represents the stack frame B, with B symbols refer to the virtual machine instruction will be as A parameter, but symbols refer to memory locations is not directly represent the B method, so the call before the need to convert B symbol references to direct reference, and then through the direct reference B can access method. If symbolic references are converted to direct references during class loading or the first time they are used, they are called static links. If this transformation occurs while the method is running, it is called dynamic linking.
Method exit: Records information about where the method was called.
Program counters: Program counters are a small area of memory that is thread private. Used to record the number of lines corresponding to bytecode instructions executed by the current thread. Branch, loop, exception, exception handling, thread recovery, and so on all depend on program counters. Java virtual machine multithreading is completed by switching threads in turn, and only one instruction of a thread will be executed at certain time. Therefore, in order to return to the correct execution position after switching, each thread needs to have an independent area to record the current execution of bytecode instructions. This counter records the line number of bytecode instructions executed by the current thread. If the current thread executes native methods, the program technician stores the value as null. The only memory region in the virtual machine that does not throw exceptions.
Heap: The heap is the largest area of memory that Java virtualizes and manages, a data area shared by all threads. The main purpose of this area is to store object instances, and it is also the main area for garbage collector to collect. The heap has Eden, Survivor (Survivor0, Survivor1), Olde zone composition [default size ratio: Eden + Survivor = 2/3 Olde 1/3, Eden (8/10) Survivor0 (1/10) Survivor (1/10) 】 -xms: specifies the minimum heap value and -xmx: specifies the maximum heap value. If the heap does not have enough memory to allocate, an OOM exception will be raised.
Method area: method area is mainly used to store constants, static variables, class information, method area is shared between threads. Class information includes: the permission name of the Class, the superclass permission name of the Class, access modifiers, field information, method information (dynamic links), and other information (Pointers to ClassLoader, references to the corresponding Class instance. After loading a Class into the method area, the Class loader creates an object instance of the corresponding Class type on the heap, through which the developer can access the definition of the object in the method area. –XX: MaxMetaspaceSize: sets the maximum value of the method area. The default value is -1. – XX: MetaspaceSize: Specifies the initial size of the method area, in bytes. The default size is 21M. When the MetaspaceSize size is changed, the full GC will be used to unload the class. The MetaspaceSize value is generally set to the same as the MaxMetaspaceSize value (256M for 8GB of memory). If there is not enough memory to allocate, an OOM exception is raised.