1 /*
   2  * Copyright (c) 2014, 2015, 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  * @library /testlibrary
  34  * @modules java.base/sun.misc
  35  *          java.management
  36  */
  37 
  38 import java.util.regex.Pattern;
  39 import java.util.regex.Matcher;
  40 import java.util.LinkedList;
  41 
  42 import jdk.test.lib.OutputAnalyzer;
  43 import jdk.test.lib.ProcessTools;
  44 import static jdk.test.lib.Asserts.*;
  45 
  46 class RefHolder {
  47   Object ref;
  48 }
  49 
  50 class ReclaimRegionFast {
  51 
  52     public static final int M = 1024*1024;
  53 
  54     public static LinkedList<Object> garbageList = new LinkedList<Object>();
  55 
  56     public static void genGarbage() {
  57         for (int i = 0; i < 32*1024; i++) {
  58             garbageList.add(new int[100]);
  59         }
  60         garbageList.clear();
  61     }
  62 
  63 
  64     // A large object referenced by a static.
  65     static int[] filler = new int[10 * M];
  66 
  67     // Old gen object referencing the large object, generating remembered
  68     // set entries.
  69     static RefHolder fromOld = new RefHolder();
  70 
  71     public static void main(String[] args) {
  72 
  73         int[] large = new int[M];
  74 
  75         Object ref_from_stack = large;
  76 
  77         for (int i = 0; i < 100; i++) {
  78             // A large object that will be reclaimed eagerly.
  79             large = new int[6*M];
  80             fromOld.ref = large;
  81             genGarbage();
  82         }
  83 
  84         // Keep the reference to the first object alive.
  85         System.out.println(ref_from_stack);
  86     }
  87 }
  88 
  89 public class TestEagerReclaimHumongousRegionsWithRefs {
  90 
  91     public static void main(String[] args) throws Exception {
  92         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  93             "-XX:+UseG1GC",
  94             "-Xms128M",
  95             "-Xmx128M",
  96             "-Xmn16M",
  97             "-Xlog:gc",
  98             ReclaimRegionFast.class.getName());
  99 
 100         Pattern p = Pattern.compile("Full GC");
 101 
 102         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 103 
 104         int found = 0;
 105         Matcher m = p.matcher(output.getStdout());
 106         while (m.find()) {
 107             found++;
 108         }
 109         System.out.println("Issued " + found + " Full GCs");
 110 
 111         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");
 112         output.shouldHaveExitValue(0);
 113     }
 114 }
 115