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