1 /* 2 * Copyright (c) 2020, 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 package gc; 25 26 /* @test 27 * @bug 8240696 28 * @library /test/lib 29 * @build sun.hotspot.WhiteBox 30 * @modules java.base 31 * @run main ClassFileInstaller sun.hotspot.WhiteBox 32 * @run main/othervm 33 * -Xbootclasspath/a:. 34 * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 35 * gc.TestReferenceClearDuringMarking 36 */ 37 38 import java.lang.ref.WeakReference; 39 import sun.hotspot.WhiteBox; 40 41 public class TestReferenceClearDuringMarking { 42 private static final WhiteBox WB = WhiteBox.getWhiteBox(); 43 44 private static Object testA = new Object(); 45 private static Object testB = new Object(); 46 47 private static final WeakReference<Object> refA1 = new WeakReference<Object>(testA); 48 private static final WeakReference<Object> refA2 = new WeakReference<Object>(testA); 49 50 private static final WeakReference<Object> refB1 = new WeakReference<Object>(testB); 51 private static final WeakReference<Object> refB2 = new WeakReference<Object>(testB); 52 test()53 private static void test() { 54 while (!WB.isObjectInOldGen(testA) || 55 !WB.isObjectInOldGen(testB) || 56 !WB.isObjectInOldGen(refA1) || 57 !WB.isObjectInOldGen(refA2) || 58 !WB.isObjectInOldGen(refB1) || 59 !WB.isObjectInOldGen(refB2)) { 60 WB.fullGC(); 61 } 62 63 WB.concurrentGCAcquireControl(); 64 try { 65 testA = null; 66 testB = null; 67 68 WB.concurrentGCRunTo(WB.AFTER_MARKING_STARTED); 69 // Clear A1 early in marking, before reference discovery. 70 refA1.clear(); 71 72 WB.concurrentGCRunTo(WB.BEFORE_MARKING_COMPLETED); 73 // Clear B1 late in marking, after reference discovery. 74 refB1.clear(); 75 76 WB.concurrentGCRunToIdle(); 77 78 // Verify that A2 and B2 still cleared by GC, i.e. the preceding 79 // clear operations did not extend the lifetime of the referents. 80 if (!refA2.refersTo(null)) { 81 throw new RuntimeException("refA2 not cleared"); 82 } 83 if (!refB2.refersTo(null)) { 84 throw new RuntimeException("refB2 not cleared"); 85 } 86 } finally { 87 WB.concurrentGCReleaseControl(); 88 } 89 } 90 main(String[] args)91 public static void main(String[] args) { 92 if (WB.supportsConcurrentGCBreakpoints()) { 93 test(); 94 } 95 } 96 } 97