Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReference/weak004/weak004.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/weak004.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]29*30* @library /vmTestbase31* /test/lib32* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.WeakReference.weak004.weak004 -t 133*/3435package gc.gctests.WeakReference.weak004;3637import nsk.share.gc.*;38import java.lang.ref.WeakReference;3940/**41* Test that GC correctly clears weak references.42*43* This test is the same as weak003 except that it creates44* weak references to same object.45*/46public class weak004 extends ThreadedGCTest {4748class Worker implements Runnable {4950private int arrayLength;51private int objectSize = 100;52private WeakReference[] references;5354private void makeReferences() {55MemoryObject obj = new MemoryObject(objectSize);56references = new WeakReference[arrayLength];57for (int i = 0; i < arrayLength; ++i) {58references[i] = new WeakReference<MemoryObject>(obj);59}60}6162public void run() {63arrayLength = Memory.getArrayLength(runParams.getTestMemory() - objectSize, Memory.getReferenceSize() + objectSize);64System.out.println("Array size: " + arrayLength);65makeReferences();66Algorithms.eatMemory(getExecutionController());67if (!getExecutionController().continueExecution()) {68return;69}70int n = 0;71for (int i = 0; i < arrayLength; ++i) {72if (references[i].get() != null) {73++n;74}75}76if (n != 0) {77log.error("Some of the references have been not cleared: " + n);78setFailed(true);79}80}81}8283@Override84protected Runnable createRunnable(int i) {85return new Worker();86}8788public static void main(String[] args) {89GC.runTest(new weak004(), args);90}91}929394