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