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