first, the Java memory composition is introduced:
堆(Heap)
And
非堆(Non-heap)
Memory
According to the official statement, "Java virtual machines have a heap, the heap is a runtime data region, and all class instances and arrays of memory are allocated from here." The heap is created when the Java virtual machine is Started. "" the memory outside the heap in the JVM is called Non-heap (non-heap Memory) ". You can see that the JVM primarily manages two types of memory: heap and non-heap. In a nutshell, a heap is a Java code-readable memory that is left to the developer, not a heap, which is left to itself by the jvm, so the method area, the JVM internally processes or optimizes the required memory (such as the jit-compiled code cache), each class structure (such as running a constant pool, field, and method Data) And the code for methods and construction methods are in Non-heap memory.
second, JVM Memory Area model
1. The method area is also called "permanent generation", "non-heap", which is used to store the virtual machine loaded class information, constants, static variables, is the memory area shared by each thread. The default minimum value is 16MB and the maximum value is 64MB, -XX:PermSize
and -XX:MaxPermSize
the size of the method area can be limited by and Parameters.
Run a constant pool: is a part of the method area, in addition to the class file has a version of the classes, fields, methods, interfaces, and other descriptive information, there is also a const pool, used to hold the compiler generated a variety of symbol references, which will be placed in the method area after the class loaded in the run constant Pool.
2. Virtual Machine stack
Describes the memory model that is executed by the Java Method: each method is executed with a "stack frame" that stores information such as the local variable table (including parameters), The Operation stack, the method exit, and so On. Each method is called to the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the STACK. The declaration period is the same as the thread and is Thread-private.
The local variable table holds the various basic data types known to the compiler (,,,,,,, boolean
byte
char
short
int
float
long
double
), Object references (reference pointers, not the object itself), where 64-bit long and double-type data takes up 2 local variables, the remaining data types are only 1. The memory space required for a local variable table is allocated during compilation, and when entering a method, this method requires that the local variable allocated in the stack frame is fully deterministic, and the stack frame does not change the size space of the local variable table during the run Time.
3. Local Method Stack
Similar to the virtual machine stack, The difference lies in the Java method service that the virtual machine stack executes for the virtual machine, while the local method stack serves the native method.
4. Heap
Also called the Java heap, the GC heap is the largest memory area managed by the Java Virtual machine, and is a memory area shared by each thread that is created when the JVM is Started. This memory area holds the object instances and arrays (all new objects). Its size is -Xms
set by the (minimum) and -Xmx
(maximum) PARAMETERS,-XMS is the minimum memory requested when the JVM starts, defaults to 1/64 of the operating system physical memory, but less than 1G, the -Xmx
maximum memory that the JVM can request, the default is 1/4 of physical memory, but less than 1G, By default, when the free heap memory is less than 40%, the JVM increases the heap to the -Xmx
specified size, which can be -XX:MinHeapFreeRation=
specified by specifying the column, and when the free heap memory is greater than 70%, the JVM reduces the size of the heap to the size specified by-xms, which can be XX:MaxHeapFreeRation=
specified by the column, For running systems, to avoid frequent resizing of the heap at run time, The-xms is usually set to the same value as-xmx.
Since collectors are now using generational collection algorithms, The heap is divided into the new generation and the old Age. The new generation primarily stores newly created objects and objects that have not yet entered the old Age. The old age stores the objects that survived by several new generation GC (Minor gc).
New Generation: The newly created object of the program is to allocate memory from the cenozoic, the Cenozoic is composed of Eden Space
two blocks of the same size Survivor Space
(usually called S0 and S1 or from and to), can be specified by the-xmn parameter to the size of the cenozoic, can also be -XX:SurvivorRation
adjusted Eden Space
and Survivor Space
The Size. Old age: used to store after a number of new generation GC still surviving objects, such as cache objects, the newly created objects may also be directly into the old age, there are two main cases: ①. large objects, which can be set by the start parameter -XX:PretenureSizeThreshold=1024
(in bytes, by default, 0) to represent the time when the generation is not allocated in the cenozoic, But directly in the old age Distribution. ②. a large Array object that has no reference to the outer object in the tangent array. Older generations accounted for the memory size OF-XMX corresponding to the value minus-xmn corresponding Value.
Young Generation That is, in the diagramEden + From Space + To SpaceEden Storage of new objectsSurvivor space There are two objects that live after each garbage collection old generationtenured Generation The old space Main store life-cycle long live objects in applications
5. Program Counter
is the smallest piece of memory area, its role is the current thread executes the byte code of the line number indicator, in the virtual machine model, The bytecode interpreter works by changing the value of this counter to select the next need to execute the byte code instruction, branch, loop, Exception handling, Basic functions such as thread recovery need to rely on counters to Complete.
third, Direct Memory
Direct memory is not part of the virtual machine memory, nor is it a memory area defined in the Java Virtual machine Specification. The newly added NiO in jdk1.4, which introduces the IO mode of channel and buffer, can call the native method to allocate out-of-heap memory directly, which is the native memory and does not affect the size of heap memory.
Iv. 10 Essentials of Java Heap memory
- Java heap Memory is part of the memory that the operating system allocates to the JVM.
- When we create objects, they are stored in the Java heap memory.
- To facilitate garbage collection, the Java heap space is divided into three zones, called New Generation, old Generation or tenured Generation, and perm space.
- You can resize the Java heap space by using the Jvm's command-line Options-xms,-xmx,-xmn. Don't forget to add "M" or "G" after the size to indicate the Unit. For example, you can use-xmx256m to set the maximum heap memory size to 256MB.
- You can use Jconsole or runtime.maxmemory (), runtime.totalmemory (), runtime.freememory () to see the size of the heap memory in Java.
- You can use the command "jmap" to get the heap dump and use "jhat" to analyze the heap Dump.
- Java heap space is different from the stack space, and the stack space is used to store the call stack and local Variables.
- The Java garbage collector is used to reclaim memory consumed by dead objects (objects that are no longer in Use) and then release them into the Java heap space.
- When you encounter java.lang.outOfMemoryError, don't be nervous, sometimes just increase the heap space can be, but if often appear, it is necessary to see whether there is a memory leak in the Java Program.
- Use the profiler and heap dump analysis tools to view the Java heap space to see how much memory is allocated to each object.
Ext.: http://www.hollischuang.com/archives/80
Memory composition of the Java Virtual machine and introduction to heap memory