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