Chapter 1 JVM memory structure and chapter 1 jvm Structure
Note: this series of blogs mainly refer to the following three books
Distributed Java applications: basics and practices, a deep understanding of Java virtual machines (version 2), and an in-depth analysis of Java web technology insider
1. Why should we understand the JVM memory management mechanism?
- The JVM automatically manages memory allocation and recovery, which will waste a lot of memory without knowing it, causing the JVM to spend a lot of time for garbage collection (GC)
- Memory leakage leads to insufficient JVM memory.
2. JVM Memory Structure
The JVM memory structure includes:
- Method Area (that is, "permanent generation ")
- Heap
- Stack (in the hotspot JVM, the JVM method stack-Java virtual stack, which is the same as the local method stack)
- PC register (program counter)
Note:
- The heap is the main area of GC, and GC occurs in the method area.
- Stack and PC register are private regions created by each thread
2.1. Method Area
- Storage content (class information, class attributes, methods, constant pools)
- Information of loaded classes (names, modifiers, etc)
- Static variables in the class
- Class is defined as final constant
- Attribute Field information in the class
- Method information in the class
- Constant pool: Various literal and symbolic references generated by the compiler (during compilation ).Runtime constant pool
- Example: reflection. When the program calls getName or other methods to obtain information data through the Class object, the information data comes from the method area.
- Adjustment Parameters
- -XX: PermSize: specifies the minimum value of the method area. The default value is 16 Mb.
- -XX: MaxPermSize: specifies the maximum value of the method area. The default value is 64 MB.
- Thrown errors
- If the memory used in the method area exceeds the permitted size, an OutOfMemoryError is thrown.
- Main goals of memory recovery
- Detach a class (this is one of the reasons why many enterprises use templates such as velocity as the front-end instead of jsp)
- Collection of constant pools
- Summary
- In general, in enterprise development,-XX: PermSize =-XX: MaxPermSize
- Generally, this size is set to MB, so it is okay. Of course, you need to estimate it based on your own program and adjust it during the running process, here, the configuration in the Resin server is used as an example <jvm-arg>-XX: PermSize = 256 M </jvm-arg> <jvm-arg>-XX: maxPermSize = 256 M </jvm-arg>View Code
2.2. Heap
- Store content
- Object instance
- Array value
- Use instance
- All objects created through new are allocated in the memory. The specific allocation to the young or the old generation depends on the configuration parameters. (There are two situations where the new object is directly allocated to the old generation, see below)
- Adjustment Parameters
- -Xmx: Maximum heap memory. The default value is 1/4 of the physical memory but less than 1 GB.
- -Xms: Minimum heap memory. The default value is 1/64 of the physical memory but less than 1 GB.
- -XX: MinHeapFreeRatio. By default, when the idle heap memory is less than 40% of the minimum heap memory, the heap memory increases to-Xmx.
- -XX: MaxHeapFreeRatio. When the free heap memory is greater than 70% of the maximum heap memory, the heap memory is reduced to-Xms.
- Notes
- In actual use,-Xmx and-Xms are configured to be equal, so that the heap memory will not be adjusted frequently.
- Throw an error
- OutOfMemoryError: The instance is not allocated in the heap memory (for instance memory allocation, I will discuss it later). When the heap memory reaches the maximum limit, it cannot be expanded.
- Heap memory Division
- New Generation
- Composition: Eden + From (S0) + To (S1)
- -Xmn: size of the entire New Generation
- -XX: Adjust vorratio: Adjust the ratio of Eden: From (To). The default value is.
- Elder Generation
- The new object is directly allocated to the old generation.
- Large Object:-XX: PretenureSizeThreshold (unit: bytes) parameter to specify the standard for large objects, which is invalid in Parallel Scavenge GC
- Large Array: Elements in the array do not reference any external objects.
Summary
- In enterprise development,-Xmx =-Xms
- Generally, it is okay to set-Xmx to 2048m. Of course, you need to estimate it based on your own program and adjust it during running, here, the configuration in the Resin server is used as an example <jvm-arg>-Xms2048m </jvm-arg> <jvm-arg>-Xmx2048m </jvm-arg> <jvm-arg>- xmn512m </jvm-arg> <jvm-arg>-XX: export vorratio = 8 </jvm-arg> <jvm-arg>-XX: MaxTenuringThreshold = 15 </jvm-arg>View Code
We can see that-Xms =-Xmx = 2048 m, the young generation size-Xmn = 512 m, so that the old generation size is 2048-512 = 1536 m, this ratio is worth remembering that in enterprise development, the young generation: the old generation =, and at this time, we configure-XX: MaxTenuringThreshold = 15 (this is also the default value ), the young generation object enters the old generation after 15 copies (this will be described in later GC mechanisms ),
- -XX: MaxTenuringThreshold is different from-XX: PretenureSizeThreshold.
2.3 Stack
- Notes
- Each thread allocates a stack. Each stack has multiple stack frames (each method corresponds to one stack frame)
- Each method creates a stack frame during execution. Each stack frame is used to store the local variable table and the operand stack of the current method. For details, refer to the first figure in this article, the process from calling to execution of each method corresponds to the process of loading a stack frame into the VM stack to the output stack, the stack frame is created when the method is executed, and the memory occupied by the stack frame is released at the end of the method.
- Store content
- Local variable table: eight basic data types and object references. The space has been allocated by the compiler and remains unchanged during runtime.
- Adjustment Parameters
- -Xss: Set the stack size. Generally, set it to 1 MB. <jvm-arg>-Xss1m </jvm-arg>View Code
- Supports native METHOD execution (local method stack)
- Thrown errors
- StackOverFlowError: the stack depth requested by the thread is greater than the depth allowed by the virtual machine.
- Typical scenario: recursion without termination conditions (stack-based recursion ).
- OutOfMemoryError: The Virtual Machine stack can be dynamically scaled. If the VM stack cannot be scaled, enough memory is applied.
- Note that the stack can be dynamically expanded, but the local variable table in the stack cannot.
2.4 PC register (program counter)
- Concept: the row number indicator of the bytecode executed by the current thread. It is used for the execution of the bytecode instruction by the bytecode interpreter.
- Multithreading: This is implemented by switching threads in turn and allocating the execution time of the processor. At any time, a single processor (that is, one core) can only execute the commands in one thread, in order to restore the thread to the correct execution position after switching, each thread must have an independent program counter. The counters of each thread do not affect each other and are stored independently.