1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test TestG1TraceEagerReclaimHumongousObjects 26 * @bug 8058801 8048179 27 * @summary Ensure that the output for a G1TraceEagerReclaimHumongousObjects 28 * includes the expected necessary messages. 29 * @key gc 30 * @library /testlibrary 31 * @build com.oracle.java.testlibrary.* 32 * @run main/othervm TestG1TraceEagerReclaimHumongousObjects 33 */ 34 35 import com.oracle.java.testlibrary.ProcessTools; 36 import com.oracle.java.testlibrary.OutputAnalyzer; 37 import java.util.LinkedList; 38 39 public class TestG1TraceEagerReclaimHumongousObjects { 40 public static void main(String[] args) throws Exception { 41 testGCLogs(); 42 testHumongousObjectGCLogs(); 43 } 44 45 private static void testGCLogs() throws Exception { 46 47 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC", 48 "-Xms128M", 49 "-Xmx128M", 50 "-Xmn16M", 51 "-XX:G1HeapRegionSize=1M", 52 "-XX:+PrintGC", 53 "-XX:+UnlockExperimentalVMOptions", 54 "-XX:G1LogLevel=finest", 55 "-XX:+G1TraceEagerReclaimHumongousObjects", 56 GCTest.class.getName()); 57 58 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 59 60 // As G1EagerReclaimHumongousObjects is set(default), below logs should be displayed. 61 // And GCTest doesn't have humongous objects, so values should be zero. 62 output.shouldContain("[Humongous Reclaim"); 63 output.shouldContain("[Humongous Total: 0]"); 64 output.shouldContain("[Humongous Candidate: 0]"); 65 output.shouldContain("[Humongous Reclaimed: 0]"); 66 67 output.shouldHaveExitValue(0); 68 } 69 70 private static void testHumongousObjectGCLogs() throws Exception { 71 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC", 72 "-Xms128M", 73 "-Xmx128M", 74 "-Xmn16M", 75 "-XX:G1HeapRegionSize=1M", 76 "-XX:+PrintGC", 77 "-XX:+UnlockExperimentalVMOptions", 78 "-XX:G1LogLevel=finest", 79 "-XX:+G1TraceEagerReclaimHumongousObjects", 80 GCWithHumongousObjectTest.class.getName()); 81 82 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 83 84 // As G1ReclaimDeadHumongousObjectsAtYoungGC is set(default), below logs should be displayed. 85 output.shouldContain("[Humongous Reclaim"); 86 output.shouldContain("[Humongous Total"); 87 output.shouldContain("[Humongous Candidate"); 88 output.shouldContain("[Humongous Reclaimed"); 89 90 // As G1TraceReclaimDeadHumongousObjectsAtYoungGC is set and GCWithHumongousObjectTest has humongous objects, 91 // these logs should be displayed. 92 output.shouldContain("Live humongous"); 93 output.shouldContain("Dead humongous region"); 94 output.shouldHaveExitValue(0); 95 } 96 97 static class GCTest { 98 private static byte[] garbage; 99 100 public static void main(String [] args) { 101 System.out.println("Creating garbage"); 102 // create 128MB of garbage. This should result in at least one GC 103 for (int i = 0; i < 1024; i++) { 104 garbage = new byte[128 * 1024]; 105 } 106 System.out.println("Done"); 107 } 108 } 109 110 static class GCWithHumongousObjectTest { 111 112 public static final int M = 1024*1024; 113 public static LinkedList<Object> garbageList = new LinkedList<Object>(); 114 // A large object referenced by a static. 115 static int[] filler = new int[10 * M]; 116 117 public static void genGarbage() { 118 for (int i = 0; i < 32*1024; i++) { 119 garbageList.add(new int[100]); 120 } 121 garbageList.clear(); 122 } 123 124 public static void main(String[] args) { 125 126 int[] large = new int[M]; 127 Object ref = large; 128 129 System.out.println("Creating garbage"); 130 for (int i = 0; i < 100; i++) { 131 // A large object that will be reclaimed eagerly. 132 large = new int[6*M]; 133 genGarbage(); 134 // Make sure that the compiler cannot completely remove 135 // the allocation of the large object until here. 136 System.out.println(large); 137 } 138 139 // Keep the reference to the first object alive. 140 System.out.println(ref); 141 System.out.println("Done"); 142 } 143 } 144 }