Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReference/weak006/weak006.java
41161 views
/*1* Copyright (c) 2007, 2020, 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*/2223/*24* @test25* @key stress randomness26*27* @summary converted from VM Testbase gc/gctests/WeakReference/weak006.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]29*30* @library /vmTestbase31* /test/lib32* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.WeakReference.weak006.weak006 -t 133*/3435package gc.gctests.WeakReference.weak006;3637import nsk.share.test.*;38import nsk.share.gc.*;39import nsk.share.TestFailure;40import java.lang.ref.WeakReference;41import java.lang.ref.SoftReference;42import java.lang.ref.PhantomReference;43import java.lang.ref.Reference;4445/**46* Test that GC correctly clears references.47*48* This test randomly creates a number of weak, soft,49* phantom and strong references, each of which points50* to the next, then provokes GC with Algorithms.eatMemory().51* The test succedes if last reference has been cleared.52*/53public class weak006 extends ThreadedGCTest {5455class Worker implements Runnable {5657private int length;58private int objectSize = 100;59private Object[] references;60private Reference lastReference;6162private Object makeReference(int n, Object o) {63switch (n) {64case 0:65return new WeakReference(o);66case 1:67return new SoftReference(o);68case 2:69return new PhantomReference(o, null);70case 4: {71// Array of strong references72int len = Memory.getArrayLength(objectSize, Memory.getReferenceSize());73Object[] arr = new Object[len];74for (int i = 0; i < len; ++i) {75arr[i] = o;76}77return arr;78}79case 5: {80// reference to array of strong references and strong reference to reference81int len = Memory.getArrayLength(objectSize, Memory.getReferenceSize());82Object[] arr = new Object[len];83for (int i = 1; i < len; ++i) {84arr[i] = o;85}86Reference ref = (Reference) makeReference(LocalRandom.nextInt(3), arr);87if (len > 0) {88arr[0] = ref;89}90return ref;91}92case 3:93default:94// Strong reference95return o;96}97}9899private void clear() {100lastReference = null;101references[length - 1] = null;102}103104private void makeReferences(int n) {105clear();106MemoryObject obj = new MemoryObject(objectSize);107references[0] = new WeakReference(obj);108for (int i = 1; i < length; ++i) {109if (i != length - 1) {110references[i] = makeReference(LocalRandom.nextInt(2), references[i - 1]);111} else {112lastReference = (Reference) makeReference(n, references[i - 1]);113references[i] = lastReference;114}115}116for (int i = 0; i < length; ++i) {117references[i] = null;118}119}120121public void run() {122makeReferences(0);123ExecutionController stresser = getExecutionController();124Algorithms.eatMemory(stresser);125if (!stresser.continueExecution()) {126return;127}128if (lastReference.get() != null) {129references = null;130throw new TestFailure("Last weak reference has not been cleared");131}132makeReferences(1);133Algorithms.eatMemory(stresser);134if (!stresser.continueExecution()) {135return;136}137if (lastReference.get() != null) {138references = null;139throw new TestFailure("Last soft reference has not been cleared");140}141}142143public Worker() {144length = Memory.getArrayLength(runParams.getTestMemory() - objectSize, Memory.getReferenceSize() + objectSize);145System.out.println("Array size: " + length);146references = new Object[length];147}148}149150@Override151protected Runnable createRunnable(int i) {152return new Worker();153}154155public static void main(String[] args) {156GC.runTest(new weak006(), args);157}158}159160161