Path: blob/master/test/hotspot/jtreg/gc/TestReferenceRefersToDuringConcMark.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* @requires vm.gc != "Shenandoah" | vm.opt.ShenandoahGCMode != "iu"27* @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.TestReferenceRefersToDuringConcMark34*/3536import java.lang.ref.Reference;37import java.lang.ref.WeakReference;38import sun.hotspot.WhiteBox;3940public class TestReferenceRefersToDuringConcMark {41private static final WhiteBox WB = WhiteBox.getWhiteBox();4243private static volatile Object testObject = null;4445private static WeakReference<Object> testWeak = null;4647private static void setup() {48testObject = new Object();49testWeak = new WeakReference<Object>(testObject);50}5152private static void gcUntilOld(Object o) throws Exception {53if (!WB.isObjectInOldGen(o)) {54WB.fullGC();55if (!WB.isObjectInOldGen(o)) {56fail("object not promoted by full gc");57}58}59}6061private static void gcUntilOld() throws Exception {62gcUntilOld(testObject);63gcUntilOld(testWeak);64}6566private static void fail(String msg) throws Exception {67throw new RuntimeException(msg);68}6970private static void expectNotCleared(Reference<Object> ref,71String which) throws Exception {72if (ref.refersTo(null)) {73fail("expected " + which + " to not be cleared");74}75}7677private static void expectValue(Reference<Object> ref,78Object value,79String which) throws Exception {80expectNotCleared(ref, which);81if (!ref.refersTo(value)) {82fail(which + " doesn't refer to expected value");83}84}8586private static void checkInitialStates() throws Exception {87expectValue(testWeak, testObject, "testWeak");88}8990private static void discardStrongReferences() {91testObject = null;92}9394private static void testConcurrentCollection() throws Exception {95setup();96gcUntilOld();9798WB.concurrentGCAcquireControl();99try {100checkInitialStates();101102discardStrongReferences();103104WB.concurrentGCRunTo(WB.BEFORE_MARKING_COMPLETED);105106// For most collectors - the configurations tested here -,107// calling get() will keep testObject alive.108if (testWeak.get() == null) {109fail("testWeak unexpectedly == null");110}111112WB.concurrentGCRunToIdle();113114expectNotCleared(testWeak, "testWeak");115} finally {116WB.concurrentGCReleaseControl();117}118}119120public static void main(String[] args) throws Exception {121if (WB.supportsConcurrentGCBreakpoints()) {122testConcurrentCollection();123}124}125}126127128