Path: blob/master/test/hotspot/jtreg/gc/TestReferenceClearDuringReferenceProcessing.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 825651727* @requires vm.gc.Z | vm.gc.Shenandoah28* @requires vm.gc != "null"29* @library /test/lib30* @build sun.hotspot.WhiteBox31* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox32* @run main/othervm33* -Xbootclasspath/a:.34* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI35* gc.TestReferenceClearDuringReferenceProcessing36*/3738import java.lang.ref.Reference;39import java.lang.ref.ReferenceQueue;40import java.lang.ref.WeakReference;41import sun.hotspot.WhiteBox;4243public class TestReferenceClearDuringReferenceProcessing {44private static final WhiteBox WB = WhiteBox.getWhiteBox();4546private static Object testObject = new Object();47private static final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();48private static final WeakReference<Object> ref = new WeakReference<Object>(testObject, queue);4950private static final long TIMEOUT = 10000; // 10sec in millis5152private static void test() {53while (!WB.isObjectInOldGen(testObject) ||54!WB.isObjectInOldGen(ref)) {55WB.fullGC();56}5758WB.concurrentGCAcquireControl();59try {60testObject = null;61WB.concurrentGCRunTo(WB.AFTER_CONCURRENT_REFERENCE_PROCESSING_STARTED);62if (!ref.refersTo(null)) {63throw new RuntimeException("ref not apparently cleared");64}6566ref.clear();6768WB.concurrentGCRunToIdle();6970Reference<? extends Object> enqueued = null;7172try {73enqueued = queue.remove(TIMEOUT);74} catch (InterruptedException e) {75throw new RuntimeException("queue.remove interrupted");76}77if (enqueued == null) {78throw new RuntimeException("ref not enqueued");79} else if (enqueued != ref) {80throw new RuntimeException("some other ref enqeueued");81}82} finally {83WB.concurrentGCReleaseControl();84}85}8687public static void main(String[] args) {88if (WB.supportsConcurrentGCBreakpoints()) {89// Also requires concurrent reference processing, but we90// don't have a predicate for that. For now,91// use @requires and CLA to limit the applicable collectors.92test();93}94}95}969798