(Entry note) startup parameters related to JVM heap memory: Memory Allocation for young generation, old generation, and permanent generation
If you want to view the memory occupied by the jvmprocess, you can use the command tool jmap tool jvisualvm.exe. All JVM startup parameters have default values. If you want to know the JVM memory allocation policy, you are advised to manually set these startup parameters. By comparing the statistical results of the tools provided by JDK, it is easier to understand the theoretical knowledge of memory allocation. The operating environment is win7 32-bit and JDK 1.7.0 _ 60.
The test code and JVM startup parameters are as follows:
public class Test{ public static void main(String[] args) { int a = 0; while (true) { a++; } }}
-Xms=200M -Xmx200M -XX:NewSize=100M -Xmn100M -XX:SurvivorRatio=8 -XX:OldSize=60M -XX:PermSize=50M -XX:MaxPermSize=50M
Run the preceding code, obtain the process pid through the jps command, and then use the jmap-heap pid to view the memory allocation and usage.
>jmap -heap 8912Attaching to process ID 8912, please wait...Debugger attached successfully.Client compiler detected.JVM version is 24.60-b09using thread-local object allocation.Mark Sweep Compact GCHeap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 209715200 (200.0MB) NewSize = 104857600 (100.0MB) MaxNewSize = 104857600 (100.0MB) OldSize = 62914560 (60.0MB) NewRatio = 3 SurvivorRatio = 8 PermSize = 52428800 (50.0MB) MaxPermSize = 52428800 (50.0MB)
The heap configuration parameters shown here can be set through JVM startup parameters. The following describes the meanings of several important parameters:
-Xms and-Xmx (-XX: InitialHeapSize and-XX: MaxHeapSize ):Specifies the initial heap memory and maximum size occupied by JVM.Heap memory. JVM is also a software and must obtain the physical
Then, the JVM is responsible for managing the memory resources applied to the operating system. When JVM is started, it will apply to the operating system for memory set by Xms. After JVM is started, it will run for a period of time. If memory space is found
Insufficient. The system will apply for memory again. The maximum heap memory that the JVM can obtain is the value set by-Xmx.
-XX: NewSize and-Xmn (-XX: MaxNewSize ):Specify the new generation memory andNew GenerationMaximum memory.
-XX: Invalid vorratio:Set the ratio of the size of one Eden zone to one vor zone in the new generation. In the hotspot virtual machine, the new generation = 1 Eden + 2 dedicated vor. If the new generation memory is
10 M, Region vorratio = 8, So Eden accounts for 8 M, and 2 region vor accounts for 1 M.
-XX: NewRatio:Specify the heap memory ratio of the old generation/new generation. In the hotspot virtual machine, heap memory = new generation + old generation. If-XX: NewRatio = 4, the ratio of the young generation to the old generation is, and the young generation accounts for 1/5 of the total heap memory.After-XX: MaxNewSize, the value of-XX: NewRatio is ignored., Old memory = heap memory-New Generation memory. The largest memory in the old age = heap memory-the largest memory in the new generation.
-XX: OldSize:Set the memory size of the old generation allocated by JVM startup, similar to the initial size of the new generation memory-XX: NewSize.
-XX: PermSize and-XX: MaxPermSize:SpecifyPermanent generation (Method Area). You can see:Permanent generation does not belong to heap memory. heap memory only contains the new generation and old generation.
We can find that the heap memory, new generation memory, old generation memory, and permanent generation memory all have an initial memory and a maximum memory. The following uses the initial memory and the maximum memory in the old age as an example to check the effect of Memory changes. Others should be similar. The test code is as follows:
public class TurnedTest{ private static List
list = new ArrayList
(); public static void main(String[] args) { int a = 0; while (true) { a++; list.add("demo"); } }}
Obviously, this program has memory leakage, which will eventually occupy the entire heap memory and throw out OOM. To understand this evolution, we add a breakpoint in the while LOOP, set "hit count" in breakpoint properties to 100000, and run the above program in debug mode, then use jmap to observe memory usage.
tenured generation: capacity = 62914560 (60.0MB) used = 0 (0.0MB) free = 62914560 (60.0MB) 0.0% used tenured generation: capacity = 62914560 (60.0MB) used = 16409080 (15.648918151855469MB) free = 46505480 (44.35108184814453MB) 26.08153025309245% used tenured generation: capacity = 62914560 (60.0MB) used = 53329496 (50.858970642089844MB) free = 9585064 (9.141029357910156MB) 84.76495107014973% used tenured generation: capacity = 104857600 (100.0MB) used = 84217880 (80.3164291381836MB) free = 20639720 (19.683570861816406MB) 80.3164291381836% used
We can find that the memory in the old age has increased from the first 60 M to the maximum of 100 M.