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