1 /*
   2  * Copyright (c) 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 TestNoEagerReclaimOfHumongousRegions
  26  * @bug 8139424
  27  * @summary Test to check that a live humongous object is not eagerly reclaimed. This is a regression test for
  28  *          8139424 and the test will crash if an eager reclaim occur. The test is not 100% deterministic and
  29  *          might pass even if there are problems in the code, but it will never crash unless there is a problem.
  30  * @requires vm.gc=="G1" | vm.gc=="null"
  31  * @key gc
  32  * @library /testlibrary /test/lib
  33  * @modules java.base/sun.misc
  34  * @build TestNoEagerReclaimOfHumongousRegions
  35  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  36  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  37  * @run main/othervm -Xbootclasspath/a:. -Xlog:gc,gc+humongous=debug -XX:+UseG1GC -XX:MaxTenuringThreshold=0 -XX:G1RSetSparseRegionEntries=32 -XX:G1HeapRegionSize=1m -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestNoEagerReclaimOfHumongousRegions
  38  */
  39 
  40 import java.util.LinkedList;
  41 
  42 import sun.hotspot.WhiteBox;
  43 
  44 public class TestNoEagerReclaimOfHumongousRegions {
  45     // Helper class to keep reference to humongous byte[].
  46     static class LargeRef {
  47         private byte[] _ref;
  48 
  49         LargeRef(byte[] ref) {
  50             _ref = ref;
  51         }
  52 
  53         byte[] ref() { return _ref; }
  54     }
  55 
  56     static LargeRef humongous_reference_holder;
  57 
  58     public static void main(String[] args) throws InterruptedException{
  59         WhiteBox wb = WhiteBox.getWhiteBox();
  60         LinkedList<Object> garbageAndRefList = new LinkedList<Object>();
  61         // Creating a 1M large byte array. Since the test specifies the heap
  62         // region size to be 1m this will be a humongous object. We then
  63         // store a pointer to the array in the static object to keep it live
  64         // during the whole test.
  65         humongous_reference_holder = new LargeRef(new byte[1 * 1024 * 1024]);
  66 
  67         // Create some garbage and a reference to the humongous object each round.
  68         for (int i = 0; i < 32; i++) {
  69             garbageAndRefList.add(new byte[400*1000]);
  70             garbageAndRefList.add(new LargeRef(humongous_reference_holder.ref()));
  71 
  72             // Promote to old, goal is to get rem-set entries for the humongous
  73             // object from different regions. The test specifies MaxTenuringThreshold=0,
  74             // this will make sure we get objects promoted to old at once.
  75             wb.youngGC();
  76         }
  77         // Clear the garbage and reference list.
  78         garbageAndRefList.clear();
  79 
  80         // Run a concurrent mark cycle to mark all references but the static one as dead.
  81         wb.g1StartConcMarkCycle();
  82         while (wb.g1InConcurrentMark()) {
  83             Thread.sleep(100);
  84         }
  85 
  86         // Run a young collection to make sure humongous object still can't be eagerly reclaimed.
  87         wb.youngGC();
  88         // Will crash/assert if humongous object has been reclaimed.
  89         wb.fullGC();
  90     }
  91 }