Path: blob/master/test/hotspot/jtreg/gc/TestReferenceClearDuringMarking.java
40930 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package gc;2425/* @test26* @bug 824069627* @library /test/lib28* @build sun.hotspot.WhiteBox29* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox30* @run main/othervm31* -Xbootclasspath/a:.32* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI33* gc.TestReferenceClearDuringMarking34*/3536import java.lang.ref.WeakReference;37import sun.hotspot.WhiteBox;3839public class TestReferenceClearDuringMarking {40private static final WhiteBox WB = WhiteBox.getWhiteBox();4142private static Object testA = new Object();43private static Object testB = new Object();4445private static final WeakReference<Object> refA1 = new WeakReference<Object>(testA);46private static final WeakReference<Object> refA2 = new WeakReference<Object>(testA);4748private static final WeakReference<Object> refB1 = new WeakReference<Object>(testB);49private static final WeakReference<Object> refB2 = new WeakReference<Object>(testB);5051private static void test() {52while (!WB.isObjectInOldGen(testA) ||53!WB.isObjectInOldGen(testB) ||54!WB.isObjectInOldGen(refA1) ||55!WB.isObjectInOldGen(refA2) ||56!WB.isObjectInOldGen(refB1) ||57!WB.isObjectInOldGen(refB2)) {58WB.fullGC();59}6061WB.concurrentGCAcquireControl();62try {63testA = null;64testB = null;6566WB.concurrentGCRunTo(WB.AFTER_MARKING_STARTED);67// Clear A1 early in marking, before reference discovery.68refA1.clear();6970WB.concurrentGCRunTo(WB.BEFORE_MARKING_COMPLETED);71// Clear B1 late in marking, after reference discovery.72refB1.clear();7374WB.concurrentGCRunToIdle();7576// Verify that A2 and B2 still cleared by GC, i.e. the preceding77// clear operations did not extend the lifetime of the referents.78if (!refA2.refersTo(null)) {79throw new RuntimeException("refA2 not cleared");80}81if (!refB2.refersTo(null)) {82throw new RuntimeException("refB2 not cleared");83}84} finally {85WB.concurrentGCReleaseControl();86}87}8889public static void main(String[] args) {90if (WB.supportsConcurrentGCBreakpoints()) {91test();92}93}94}959697