1 /*
2  * Copyright (c) 2014, 2020, 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 TestEagerReclaimHumongousRegionsWithRefs
28  * @bug 8048179
29  * @summary Test to make sure that eager reclaim of humongous objects that have previously
30  * been referenced by other old gen regions work. We simply try to fill
31  * up the heap with humongous objects and create a remembered set entry from an object by
32  * referencing that we know is in the old gen. After changing this reference, the object
33  * should still be eagerly reclaimable to avoid Full GC.
34  * @requires vm.gc.G1
35  * @library /test/lib
36  * @modules java.base/jdk.internal.misc
37  *          java.management
38  * @run driver gc.g1.TestEagerReclaimHumongousRegionsWithRefs
39  */
40 
41 import java.util.regex.Pattern;
42 import java.util.regex.Matcher;
43 import java.util.LinkedList;
44 
45 import jdk.test.lib.process.OutputAnalyzer;
46 import jdk.test.lib.process.ProcessTools;
47 import static jdk.test.lib.Asserts.*;
48 
49 class RefHolder {
50   Object ref;
51 }
52 
53 class TestEagerReclaimHumongousRegionsWithRefsReclaimRegionFast {
54 
55     public static final int M = 1024*1024;
56 
57     public static LinkedList<Object> garbageList = new LinkedList<Object>();
58 
genGarbage()59     public static void genGarbage() {
60         for (int i = 0; i < 32*1024; i++) {
61             garbageList.add(new int[100]);
62         }
63         garbageList.clear();
64     }
65 
66 
67     // A large object referenced by a static.
68     static int[] filler = new int[10 * M];
69 
70     // Old gen object referencing the large object, generating remembered
71     // set entries.
72     static RefHolder fromOld = new RefHolder();
73 
main(String[] args)74     public static void main(String[] args) {
75 
76         int[] large = new int[M];
77 
78         Object ref_from_stack = large;
79 
80         for (int i = 0; i < 100; i++) {
81             // A large object that will be reclaimed eagerly.
82             large = new int[6*M];
83             fromOld.ref = large;
84             genGarbage();
85         }
86 
87         // Keep the reference to the first object alive.
88         System.out.println(ref_from_stack);
89     }
90 }
91 
92 public class TestEagerReclaimHumongousRegionsWithRefs {
93 
main(String[] args)94     public static void main(String[] args) throws Exception {
95         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
96             "-XX:+UseG1GC",
97             "-Xms128M",
98             "-Xmx128M",
99             "-Xmn16M",
100             "-Xlog:gc",
101             TestEagerReclaimHumongousRegionsWithRefsReclaimRegionFast.class.getName());
102 
103         Pattern p = Pattern.compile("Full GC");
104 
105         OutputAnalyzer output = new OutputAnalyzer(pb.start());
106 
107         int found = 0;
108         Matcher m = p.matcher(output.getStdout());
109         while (m.find()) {
110             found++;
111         }
112         System.out.println("Issued " + found + " Full GCs");
113 
114         assertLessThan(found, 10, "Found that " + found + " Full GCs were issued. This is larger than the bound. Eager reclaim of objects once referenced from old gen seems to not work at all");
115         output.shouldHaveExitValue(0);
116     }
117 }
118 
119