1 /*
2  * Copyright (c) 2014, 2019, 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 package gc.g1;
25 
26 /*
27  * @test TestEagerReclaimHumongousRegions
28  * @bug 8027959
29  * @summary Test to make sure that eager reclaim of humongous objects work. We simply try to fill
30  * up the heap with humongous objects that should be eagerly reclaimable to avoid Full GC.
31  * @key gc
32  * @requires vm.gc.G1
33  * @library /test/lib
34  * @modules java.base/jdk.internal.misc
35  *          java.management
36  * @run main gc.g1.TestEagerReclaimHumongousRegions
37  */
38 
39 import java.util.regex.Pattern;
40 import java.util.regex.Matcher;
41 import java.util.LinkedList;
42 
43 import jdk.test.lib.process.OutputAnalyzer;
44 import jdk.test.lib.process.ProcessTools;
45 import jdk.test.lib.Asserts;
46 
47 class TestEagerReclaimHumongousRegionsReclaimRegionFast {
48     public static final int M = 1024*1024;
49 
50     public static LinkedList<Object> garbageList = new LinkedList<Object>();
51 
genGarbage()52     public static void genGarbage() {
53         for (int i = 0; i < 32*1024; i++) {
54             garbageList.add(new int[100]);
55         }
56         garbageList.clear();
57     }
58 
59     // A large object referenced by a static.
60     static int[] filler = new int[10 * M];
61 
main(String[] args)62     public static void main(String[] args) {
63 
64         int[] large = new int[M];
65 
66         Object ref_from_stack = large;
67 
68         for (int i = 0; i < 100; i++) {
69             // A large object that will be reclaimed eagerly.
70             large = new int[6*M];
71             genGarbage();
72             // Make sure that the compiler cannot completely remove
73             // the allocation of the large object until here.
74             System.out.println(large);
75         }
76 
77         // Keep the reference to the first object alive.
78         System.out.println(ref_from_stack);
79     }
80 }
81 
82 public class TestEagerReclaimHumongousRegions {
main(String[] args)83     public static void main(String[] args) throws Exception {
84         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
85             "-XX:+UseG1GC",
86             "-Xms128M",
87             "-Xmx128M",
88             "-Xmn16M",
89             "-Xlog:gc",
90             TestEagerReclaimHumongousRegionsReclaimRegionFast.class.getName());
91 
92         Pattern p = Pattern.compile("Full GC");
93 
94         OutputAnalyzer output = new OutputAnalyzer(pb.start());
95 
96         int found = 0;
97         Matcher m = p.matcher(output.getStdout());
98         while (m.find()) { found++; }
99         System.out.println("Issued " + found + " Full GCs");
100         Asserts.assertLT(found, 10, "Found that " + found + " Full GCs were issued. This is larger than the bound. Eager reclaim seems to not work at all");
101 
102         output.shouldHaveExitValue(0);
103     }
104 }
105