1. Memory allocation strategy
Common parameters:
-verbose:gc / / print the GC
-XX:+PrintGCDetails // Print the GC details
-Xms20M // The minimum of the heap
-Xmx20M // The maximum value of the heap
-Xmn10M // The size of the Cenozoic
-XX:+UseSerialGC The Serial collector is used by the JVM on the client side and the Parallel collector is used on the server side by default. You can use java-version to determine whether it is the client or server side. The client with less than 1G of memory, but now basically does not exist, so it is basically Parallel collector, as shown below
// -xx :+UseParallelGC // Specify the Parallel Scavenge avenge
-XX:SurvivorRatio=8 // Defines the ratio of Eden to Survivor, default is 8
-XX:+PrintTenuringDistribution // Prints the age of the object
Copy the code
1. Eden will be allocated first
When an object is allocated, if there is continuous space allocated to the object in Eden area, it will be allocated in Eden area first.
Case study:
2, large objects are directly assigned to the old age
The threshold of a large object is specified with a parameter:
-XX:PretenureSizeThreshold=5M // all objects >=7M will be allocated to the old age
Copy the code
This also has one advantage: because the new generation adopts the replication algorithm, if there are large objects, the replication efficiency will be very low;
Case study:
3. Long-term survival objects are assigned to the old age
How does the JVM determine the age of an object in this case?
In JDK6 and before, it is considered that in the case of YOUNG GC, surviving 15 times, it enters the old age; But after JDK6, it is not strictly after reaching this age to enter the old age;
The so-called AGE calculation is that every time the YOUNG GC is triggered, a copy is made, then AGE+1. When AGE reaches the threshold, it will enter the old AGE.
An age threshold can also be specified with a parameter:
-XX:MaxTenuringThreshold=2 // If age>=2
Copy the code
Case study:
4. Guarantee of space allocation
The so-called space allocation guarantee is that after the YOUNG GC is triggered, Survivor can not release the surviving objects, and then applies for space from the old generation.
Before JDK7, before the Young GC occurs, the virtual machine checks whether the maximum available contiguous space of the old generation is greater than the total space of all objects of the new generation:
-
If greater, Young GC normally fires;
-
If less than, the virtual opportunities through the parameter – XX: HandlePromotionFailure determine whether to allow the memory guarantees failure
- If so, it will continue to judge whether the maximum available continuous space in the old age is greater than the platform size of the objects promoted to the old age;
- If it is greater than, Young GC will be performed, but this Young GC is risky and may lead to OOM.
- If less, the Full GC is triggered, followed by the Young GC;
- If not, go straight to Full GC;
+XX:HandlePromotionFailure //+ : enable, - : disable Copy the code
- If so, it will continue to judge whether the maximum available continuous space in the old age is greater than the platform size of the objects promoted to the old age;
Parameters after the JDK7 – XX: HandlePromotionFailure is invalid, as long as the continuous space of the old s total size is greater than the Cenozoic object or the average of the previous promotion size will be Young, GC, otherwise Full GC, is actually less judgment step parameters, The overall process is still the same as before JDK7;
Case study:
5, dynamic object age judgment
The virtual machine does not always require that the object age must reach MaxTenuringThreshold to be promoted to the old age. If the total size of all objects of the same age in the Survivor zone is greater than half of the size of the Survivor space, the objects older than or equal to this age are directly aged. Do not wait for the age threshold set by MaxTenuringThreshold;
Case study: