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 TestEagerReclaimHumongousRegionsClearMarkBits
  26  * @bug 8051973
  27  * @summary Test to make sure that eager reclaim of humongous objects correctly clears
  28  * mark bitmaps at reclaim.
  29  * @key gc
  30  * @library /testlibrary
  31  * @modules java.base/sun.misc
  32  *          java.management
  33  */
  34 
  35 import java.util.ArrayList;
  36 import java.util.LinkedList;
  37 import java.util.Random;
  38 
  39 import jdk.test.lib.OutputAnalyzer;
  40 import jdk.test.lib.ProcessTools;
  41 
  42 // An object that has a few references to other instances to slow down marking.
  43 class ObjectWithSomeRefs {
  44     public ObjectWithSomeRefs other1;
  45     public ObjectWithSomeRefs other2;
  46     public ObjectWithSomeRefs other3;
  47     public ObjectWithSomeRefs other4;
  48 }
  49 
  50 class ReclaimRegionFast {
  51     public static final long MAX_MILLIS_FOR_RUN = 50 * 1000; // The maximum runtime for the actual test.
  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(Object large) {
  58         for (int i = 0; i < 64*1024; i++) {
  59             Object[] garbage = new Object[50];
  60             garbage[0] = large;
  61             garbageList.add(garbage);
  62         }
  63         garbageList.clear();
  64     }
  65 
  66     public static ArrayList<ObjectWithSomeRefs> longList = new ArrayList<ObjectWithSomeRefs>();
  67 
  68     public static void main(String[] args) {
  69 
  70         for (int i = 0; i < 16*1024; i++) {
  71              longList.add(new ObjectWithSomeRefs());
  72         }
  73 
  74         Random rnd = new Random();
  75         for (int i = 0; i < longList.size(); i++) {
  76              int len = longList.size();
  77              longList.get(i).other1 = longList.get(rnd.nextInt(len));
  78              longList.get(i).other2 = longList.get(rnd.nextInt(len));
  79              longList.get(i).other3 = longList.get(rnd.nextInt(len));
  80              longList.get(i).other4 = longList.get(rnd.nextInt(len));
  81         }
  82 
  83         int[] large1 = new int[M];
  84         int[] large2 = null;
  85         int[] large3 = null;
  86         int[] large4 = null;
  87 
  88         Object ref_from_stack = large1;
  89 
  90         long start_millis = System.currentTimeMillis();
  91 
  92         for (int i = 0; i < 20; i++) {
  93             long current_millis = System.currentTimeMillis();
  94             if ((current_millis - start_millis) > MAX_MILLIS_FOR_RUN) {
  95               System.out.println("Finishing test because maximum runtime exceeded");
  96               break;
  97             }
  98             // A set of large objects that will be reclaimed eagerly - and hopefully marked.
  99             large1 = new int[M - 20];
 100             large2 = new int[M - 20];
 101             large3 = new int[M - 20];
 102             large4 = new int[M - 20];
 103             genGarbage(large1);
 104             // Make sure that the compiler cannot completely remove
 105             // the allocation of the large object until here.
 106             System.out.println(large1 + " " + large2 + " " + large3 + " " + large4);
 107         }
 108 
 109         // Keep the reference to the first object alive.
 110         System.out.println(ref_from_stack);
 111     }
 112 }
 113 
 114 public class TestEagerReclaimHumongousRegionsClearMarkBits {
 115     public static void main(String[] args) throws Exception {
 116         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 117             "-XX:+UseG1GC",
 118             "-Xms128M",
 119             "-Xmx128M",
 120             "-Xmn2M",
 121             "-XX:G1HeapRegionSize=1M",
 122             "-XX:InitiatingHeapOccupancyPercent=0", // Want to have as much as possible initial marks.
 123             "-XX:+PrintGC",
 124             "-XX:+UnlockDiagnosticVMOptions",
 125             "-XX:+VerifyAfterGC",
 126             "-XX:ConcGCThreads=1", // Want to make marking as slow as possible.
 127             "-XX:+IgnoreUnrecognizedVMOptions", // G1VerifyBitmaps is develop only.
 128             "-XX:+G1VerifyBitmaps",
 129             ReclaimRegionFast.class.getName());
 130         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 131         output.shouldHaveExitValue(0);
 132     }
 133 }
 134