Four ways objects enter the old age

  • After minor GC, survivor space cannot hold all surviving objects
  • The age threshold of a viable object has been reached. Procedure Such as 15
  • The big object
  • Dynamic age judgment

In this section, live objects reach the age threshold. For example, 15.

Of course, I’m definitely not going to set the age threshold to 15, which is a lot of trouble. I’m just going to set a 2.

Code first:

public static void _2_year(){ byte[] array1 = new byte[2*_1MB]; array1 = new byte[2*_1MB]; array1 = new byte[2*_1MB]; byte[] array2 = new byte[128*1024]; Array1 = null; array1 = null; byte[] array3 = new byte[2*_1MB]; Array3 = new byte[2*_1MB]; array3 = new byte[2*_1MB]; array3 = null; byte[] array4 = new byte[2*_1MB]; Array4 = new byte[2*_1MB]; array4 = new byte[2*_1MB]; array4 = null; byte[] array5 = new byte[2*_1MB]; // The third minor gc, when the object pointed to by array2 is 2 years old}Copy the code

The JVM parameters:

-XX:NewSize=10m -XX:MaxNewSize=10m -XX:InitialHeapSize=20m -XX:MaxHeapSize=20m -XX:SurvivorRatio=8 -XX:PretenureSizeThreshold=10m -XX:MaxTenuringThreshold=2 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:_2_year_old.log

After running the code, the resulting log file:

Java HotSpot(TM) 64-bit Server VM (25.261-B12) for BSD-AMD64 JRE (1.8.0_261-B12) Built on Jun 18 2020 06:38:55 by "java_re" with GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5) Memory: 4k page, physical 33554432k(1209116k free) /proc/meminfo: CommandLine flags: -XX:InitialHeapSize=20971520 -XX:InitialTenuringThreshold=2 -XX:MaxHeapSize=20971520 -XX:MaxNewSize=10485760 -XX:MaxTenuringThreshold=2 -XX:NewSize=10485760 -XX:OldPLABSize=16 -XX:PretenureSizeThreshold=10485760 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:SurvivorRatio=8 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -xx :+UseConcMarkSweepGC -xx :+UseParNewGC 0.073: [Allocation Failure (GC) 0.073: [ParNew: 7144K-> 103K (10216k), 0.0001195secs] [Times: 103k -> 103k (10216k), 0.0001195secs] Sys =0.00, real=0.00 secs] 0.074: [Allocation Failure (GC) 0.074: [ParNew: [Times: 0.00118k, 0.00118k] [Times: 0.00118k, 0.00118k] [Times: 0.00118k, 0.00118k] User =0.01 sys=0.00, real=0.00 secs] 0.075: [GC (Allocation Failure) 0.075: [ParNew: Secs] [Times: 1] [Times: 1] [Times: 1] [Times: 1] User =0.00 sys=0.00, real=0.00 SECS] Heap PAR new Generation Total 9216K, Used 2212K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000) eden space 8192K, 27% used [0x00000007bec00000, 0x00000007bee290e0, 0x00000007bf400000) from space 1024K, 0% used [0x00000007bf500000, 0x00000007bf500000, 0x00000007bf600000) to space 1024K, 0% used [0x00000007bf400000, 0x00000007bf400000, 0x00000007bf500000) concurrent mark-sweep generation total 10240K, used 481K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000) Metaspace used 2946K, capacity 4486K, committed 4864K, reserved 1056768K class space used 316K, capacity 386K, committed 512K, reserved 1048576KCopy the code

At this point, combined with code and log analysis:

byte[] array1 = new byte[2*_1MB]; array1 = new byte[2*_1MB]; array1 = new byte[2*_1MB]; byte[] array2 = new byte[128*1024]; Array1 = null; array1 = null;Copy the code

In this case, three 2M objects and one 128K object are allocated to Eden area. Array1 is null. In other words, all three 2m objects are junk.

Byte [] array3 = new byte[2*_1MB];

The first Young GC is triggered because there is not enough space in Eden. In this case, the 128K object is 0 years old.

[ParNew: 7144K->533K(9216K)

After GC, 533K objects are left. Where 128K is the object that Array2 points to. The remaining 400+ K are unknown objects created by the JVM itself.

Next, you keep creating objects and keep Array2 pointing to 128K objects. Guaranteed not to be collected by GC, increasing the age of 128K objects to 2 years.

array3 = null; byte[] array4 = new byte[2*_1MB]; Array4 = new byte[2*_1MB]; array4 = new byte[2*_1MB]; array4 = null; byte[] array5 = new byte[2*_1MB]; // The third minor gc, when the object pointed to by Array2 is 2 years oldCopy the code

ParNew: 6837K->0K(9216K)

After three GCS, the 128K object is 2 years old. The usage space of the new generation is 0.

concurrent mark-sweep generation total 10240K, used 481K

At this time, the space used in the old age is 481K. It’s the sum of some unknown objects and 128 kelvin of space.

This verifies that an object will enter the old age when it reaches a certain age threshold.