Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReference/weak007/weak007.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/weak007.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]29*30* @library /vmTestbase31* /test/lib32* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.WeakReference.weak007.weak007 -t 133*/3435package gc.gctests.WeakReference.weak007;3637import nsk.share.TestFailure;38import nsk.share.gc.*;39import java.lang.ref.WeakReference;40import java.lang.ref.Reference;4142/**43* Test that GC correctly clears weak references.44*45* This test creates a number of weak references,46* each of which points to the next, then provokes47* GC with Algorithms.eatMemory(). The test succeeds48* if last reference has been cleared and the object49* has been finalized.50*/51public class weak007 extends ThreadedGCTest {5253class Worker implements Runnable {5455private int length = 10000;56private int objectSize = 10000;57private Reference[] references;5859private void makeReferences() {60references[length - 1] = null;61FinMemoryObject obj = new FinMemoryObject(objectSize);62references[0] = new WeakReference(obj);63for (int i = 1; i < length; ++i) {64references[i] = new WeakReference(references[i - 1]);65}66for (int i = 0; i < length - 1; ++i) {67references[i] = null;68}69}7071public void run() {72makeReferences();73Algorithms.eatMemory(getExecutionController());74if (getExecutionController().continueExecution()) {75if (references[length - 1].get() != null) {76throw new TestFailure("Last weak reference has not been cleared");77}78} else {79log.info("Completed iterations: " + getExecutionController().getIteration());80}81}8283public Worker() {84log.info("Array size: " + length);85log.info("Object size: " + objectSize);86references = new WeakReference[length];87}88}8990@Override91protected Runnable createRunnable(int i) {92return new Worker();93}9495public static void main(String[] args) {96GC.runTest(new weak007(), args);97}98}99100101