|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | +/* @test |
| 25 | + * @summary Test that pinned regions with no Java references into them |
| 26 | + * do not make the garbage collector reclaim that region. |
| 27 | + * This test simulates this behavior using Whitebox/Unsafe methods |
| 28 | + * to pin a Java object in a region with no other pinnable objects and |
| 29 | +* lose the reference to it before the garbage collection. |
| 30 | + * @requires vm.gc.G1 |
| 31 | + * @requires vm.debug |
| 32 | + * @library /test/lib |
| 33 | + * @modules java.base/jdk.internal.misc:+open |
| 34 | + * java.management |
| 35 | + * @build jdk.test.whitebox.WhiteBox |
| 36 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox |
| 37 | + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UseG1GC |
| 38 | + * -Xbootclasspath/a:. -Xlog:gc=debug,gc+ergo+cset=trace,gc+phases=debug -XX:G1HeapRegionSize=1m -Xms30m gc.g1.pinnedobjs.TestPinnedEvacEmpty |
| 39 | + */ |
| 40 | + |
| 41 | +package gc.g1.pinnedobjs; |
| 42 | + |
| 43 | +import jdk.internal.misc.Unsafe; |
| 44 | + |
| 45 | +import jdk.test.lib.Asserts; |
| 46 | +import jdk.test.lib.process.OutputAnalyzer; |
| 47 | +import jdk.test.lib.process.ProcessTools; |
| 48 | +import jdk.test.whitebox.WhiteBox; |
| 49 | + |
| 50 | +public class TestPinnedEvacEmpty { |
| 51 | + |
| 52 | + private static final jdk.internal.misc.Unsafe unsafe = Unsafe.getUnsafe(); |
| 53 | + private static final WhiteBox wb = WhiteBox.getWhiteBox(); |
| 54 | + |
| 55 | + private static final long objSize = wb.getObjectSize(new Object()); |
| 56 | + |
| 57 | + // How many j.l.Object should we allocate when creating garbage. |
| 58 | + private static final long numAllocations = 1024 * 1024 * 3 / objSize; |
| 59 | + |
| 60 | + public static void main(String[] args) throws Exception { |
| 61 | + // Remove garbage from VM initialization. |
| 62 | + wb.fullGC(); |
| 63 | + |
| 64 | + // Allocate garbage so that the target object will be in a new region. |
| 65 | + for (int i = 0; i < numAllocations; i++) { |
| 66 | + Object z = new Object(); |
| 67 | + } |
| 68 | + int[] o = new int[100]; // The target object to pin. |
| 69 | + // Further allocate garbage so that any additional allocations of potentially |
| 70 | + // pinned objects can not be allocated in the same region as the target object. |
| 71 | + for (int i = 0; i < numAllocations; i++) { |
| 72 | + Object z = new Object(); |
| 73 | + } |
| 74 | + |
| 75 | + Asserts.assertTrue(!wb.isObjectInOldGen(o), "should not be in old gen already"); |
| 76 | + |
| 77 | + // Pin the object. |
| 78 | + wb.pinObject(o); |
| 79 | + |
| 80 | + // And forget the (Java) reference to the int array. After this, the garbage |
| 81 | + // collection should find a completely empty pinned region. The collector |
| 82 | + // must not collect/free it. |
| 83 | + o = null; |
| 84 | + |
| 85 | + // Do garbage collection to zap the data in the pinned region. |
| 86 | + wb.youngGC(); |
| 87 | + } |
| 88 | +} |
| 89 | + |
0 commit comments