Object memory is allocated directly in the old generation

Objects are allocated preferentially in the young generation, but in some cases objects are allocated directly in the old generation, as follows:

1. The size of the allocated object is larger than the eden space. Fits all collectors.

2. The remaining space of the eden space is insufficient to allocate, and the memory size of the allocated object is not less than half of the total space of the eden space. It is directly allocated to the old age without triggering the Minor GC. Suitable for -XX:+UseParallelGC, -XX:+UseParallelOldGC, that is, for Parallel Scavenge.

3. Large objects enter the old age directly, controlled by the -XX:PretenureSizeThreshold parameter, suitable for -XX:+UseSerialGC, -XX:+UseParNewGC, -XX:+UseConcMarkSweepGC, which is suitable for Serial and ParNew collectors.

Here is the test procedure:

1. The first case.

Test code:

	/**
	 * The object size exceeds the eden space size, -Xms30m -Xmx30m -Xmn10m -XX:+PrintGCDetails
	 */
	private static void testMoreThanEden() {

		/* Eden area is 8M, from/to space is 1M each */
		LargeObject largeOb1 = new LargeObject(_1M * 8, "largeOb1");
	}

1. Test environment: Java HotSpot(TM) 64-Bit Server VM

-XX:+UseSerialGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Heap
 def new generation   total 9216K, used 1148K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  14% used [0x00000000fec00000, 0x00000000fed1f168, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 tenured generation   total 10240K, used 8192K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  80% used [0x00000000ff600000, 0x00000000ffe00010, 0x00000000ffe00200, 0x0000000100000000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K
2. Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseParNewGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails
Test results:
Over Constructing LargeObject largeOb1

Heap
 par new generation   total 9216K, used 1148K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  14% used [0x00000000fec00000, 0x00000000fed1f168, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 tenured generation   total 10240K, used 8192K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  80% used [0x00000000ff600000, 0x00000000ffe00010, 0x00000000ffe00200, 0x0000000100000000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K
Java HotSpot(TM) 64-Bit Server VM warning: Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release
3. Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseParallelGC/-XX:+UseParallelOldGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Heap
 PSYoungGen      total 9216K, used 1148K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 14% used [0x00000000ff600000,0x00000000ff71f168,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 8192K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 80% used [0x00000000fec00000,0x00000000ff400010,0x00000000ff600000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K
4. Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseConcMarkSweepGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails
Test results:
Over Constructing LargeObject largeOb1

Heap
 par new generation   total 9216K, used 1148K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  14% used [0x00000000fec00000, 0x00000000fed1f0c8, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 concurrent mark-sweep generation total 10240K, used 8192K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K

Second, the second case, divided into three test method codes to test, only the first test method tests all collectors.

Test code:

	/**
	 * -Xms30m -Xmx30m -Xmn10m -XX:+PrintGCDetails if eden
	 * The remaining space of space is insufficient to allocate, and the size to be allocated is not less than half of the total space of eden space, directly allocated to the old age, without triggering Minor
	 *GC. Suitable for -XX:+UseParallelGC, -XX:+UseParallelOldGC.
	 */
	private static void testMoreThanAHalfEden1() {

		/* eden space is 8M, from/to space is 1M each */
		LargeObject largeOb1 = new LargeObject(_1M * 2, "largeOb1");
		LargeObject largeOb2 = new LargeObject(_1M * 5, "largeOb2");
		largeOb2 = null;

		/* When largeOb3 is 1M, 2M, and 3M, Minor GC will be triggered, starting from 4M and directly allocated to the old generation*/
		LargeObject largeOb3 = new LargeObject(_1M * 4, "largeOb3");
	}

1. Test environment: Java HotSpot(TM) 64-Bit Server VM

-XX:+UseSerialGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Over Constructing LargeObject largeOb2

[GC (Allocation Failure) [DefNew: 8152K->554K(9216K), 0.0020207 secs] 8152K->2602K(19456K), 0.0020612 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Over Constructing LargeObject largeOb3

Heap
 def new generation   total 9216K, used 4892K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  52% used [0x00000000fec00000, 0x00000000ff03cb68, 0x00000000ff400000)
  from space 1024K,  54% used [0x00000000ff500000, 0x00000000ff58a800, 0x00000000ff600000)
  to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
 tenured generation   total 10240K, used 2048K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  20% used [0x00000000ff600000, 0x00000000ff800010, 0x00000000ff800200, 0x0000000100000000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K
2. Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseParNewGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Over Constructing LargeObject largeOb2

[GC (Allocation Failure) [ParNew: 8152K->564K(9216K), 0.0015776 secs] 8152K->2612K(19456K), 0.0016120 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Over Constructing LargeObject largeOb3

Heap
 par new generation   total 9216K, used 4903K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  52% used [0x00000000fec00000, 0x00000000ff03cb68, 0x00000000ff400000)
  from space 1024K,  55% used [0x00000000ff500000, 0x00000000ff58d1c0, 0x00000000ff600000)
  to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
 tenured generation   total 10240K, used 2048K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  20% used [0x00000000ff600000, 0x00000000ff800010, 0x00000000ff800200, 0x0000000100000000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K
Java HotSpot(TM) 64-Bit Server VM warning: Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release
3. Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseParallelGC/-XX:+UseParallelOldGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Over Constructing LargeObject largeOb2

Over Constructing LargeObject largeOb3

Heap
 PSYoungGen      total 9216K, used 8192K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 100% used [0x00000000ff600000,0x00000000ffe00000,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 4096K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 40% used [0x00000000fec00000,0x00000000ff000010,0x00000000ff600000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K
4. Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseConcMarkSweepGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Over Constructing LargeObject largeOb2

[GC (Allocation Failure) [ParNew: 8152K->573K(9216K), 0.0045070 secs] 8152K->2623K(19456K), 0.0046730 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Over Constructing LargeObject largeOb3

Heap
 par new generation   total 9216K, used 4912K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  52% used [0x00000000fec00000, 0x00000000ff03cb70, 0x00000000ff400000)
  from space 1024K,  55% used [0x00000000ff500000, 0x00000000ff58f550, 0x00000000ff600000)
  to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
 concurrent mark-sweep generation total 10240K, used 2050K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
 Metaspace       used 2783K, capacity 4486K, committed 4864K, reserved 1056768K
  class space   used 300K, capacity 386K, committed 512K, reserved 1048576K

Test code:

	/**
	 * -Xms30m -Xmx30m -Xmn10m -XX:+PrintGCDetails space The remaining space is insufficient to allocate, and the size to be allocated is not less than eden
	 * Half of the total space of space is directly allocated to the old generation, without triggering Minor GC. Suitable for -XX:+UseParallelGC, -XX:+UseParallelOldGC.
	 */
	private static void testMoreThanAHalfEden2() {

		/* eden space is 8M, from/to space is 1M each */
		LargeObject largeOb1 = new LargeObject(_1M * 2, "largeOb1");

		LargeObject largeOb2 = new LargeObject(_1M * 2, "largeOb2");
		largeOb2 = null;

		/* When largeOb2 is 2M, 3M, 4M, largeOb3 is directly allocated to the old generation, if it is 1M, largeOb3 is allocated to the young generation*/
		LargeObject largeOb3 = new LargeObject(_1M * 4, "largeOb3");
	}
Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseParallelGC/-XX:+UseParallelOldGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Over Constructing LargeObject largeOb2

Over Constructing LargeObject largeOb3

Heap
 PSYoungGen      total 9216K, used 5244K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 64% used [0x00000000ff600000,0x00000000ffb1f188,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 4096K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 40% used [0x00000000fec00000,0x00000000ff000010,0x00000000ff600000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K

Test code:

	/**
	 * -Xms40m -Xmx40m -Xmn20m -XX:+PrintGCDetails space The remaining space is insufficient to allocate, and the size to be allocated is not less than eden
	 * Half of the total space of space is directly allocated to the old generation, without triggering Minor GC. Suitable for -XX:+UseParallelGC, -XX:+UseParallelOldGC.
	 */
	private static void testMoreThanAHalfEden3() {

		/* eden space is 16M, from/to space is 2M each */
		LargeObject largeOb1 = new LargeObject(_1M * 10, "largeOb1");

		/* If it is less than 8M, MinorGC is triggered, if it is greater than or equal to 8M, it is directly allocated to the old generation */
		LargeObject largeOb2 = new LargeObject(_1M * 8, "largeOb2");
	}

Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseParallelGC/-XX:+UseParallelOldGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Over Constructing LargeObject largeOb2

Heap
 PSYoungGen      total 17920K, used 11776K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
  eden space 15360K, 76% used [0x00000000fec00000,0x00000000ff780380,0x00000000ffb00000)
  from space 2560K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x0000000100000000)
  to   space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
 ParOldGen       total 20480K, used 8192K [0x00000000fd800000, 0x00000000fec00000, 0x00000000fec00000)
  object space 20480K, 40% used [0x00000000fd800000,0x00000000fe000010,0x00000000fec00000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K

Three, the third situation.

Test code:

	/**
	 * -XX:PretenureSizeThreshold=2097152 -Xms20m -Xmx20m -Xmn10m
	 * -XX:+PrintGCDetails,适合-XX:+UseSerialGC、-XX:+UseParNewGC、-XX:+UseConcMarkSweepGC
	 */
	private static void testPretenureSizeThreshold() {

		/* The eden space is 16M, and the from/to space is 2M. If it is 1M, it is directly allocated to the new generation. If it is 2M, it is directly allocated to the old generation.*/
		LargeObject largeOb1 = new LargeObject(_1M * 2, "largeOb1");
	}

1. Test environment: Java HotSpot(TM) 64-Bit Server VM

-XX:+UseSerialGC -XX:PretenureSizeThreshold=2097152  -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Heap
 def new generation   total 9216K, used 1148K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  14% used [0x00000000fec00000, 0x00000000fed1f168, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 tenured generation   total 10240K, used 2048K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  20% used [0x00000000ff600000, 0x00000000ff800010, 0x00000000ff800200, 0x0000000100000000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K
2. Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseParNewGC -XX:PretenureSizeThreshold=2097152 -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails
Test results:
Over Constructing LargeObject largeOb1

Heap
 par new generation   total 9216K, used 1148K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  14% used [0x00000000fec00000, 0x00000000fed1f168, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 tenured generation   total 10240K, used 2048K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  20% used [0x00000000ff600000, 0x00000000ff800010, 0x00000000ff800200, 0x0000000100000000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K
Java HotSpot(TM) 64-Bit Server VM warning: Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release
3. Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseParallelGC/-XX:+UseParallelOldGC -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Heap
 PSYoungGen      total 9216K, used 3196K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 39% used [0x00000000ff600000,0x00000000ff91f178,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 0K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ff600000)
 Metaspace       used 2784K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K
4. Test environment: Java HotSpot(TM) 64-Bit Server VM
-XX:+UseConcMarkSweepGC -XX:PretenureSizeThreshold=2097152 -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails

Test Results:

Over Constructing LargeObject largeOb1

Heap
 par new generation   total 9216K, used 1148K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  14% used [0x00000000fec00000, 0x00000000fed1f0c8, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 concurrent mark-sweep generation total 10240K, used 2048K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
 Metaspace       used 2786K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 300K, capacity 386K, committed 512K, reserved 1048576K

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324885379&siteId=291194637