Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/SoftReference/soft004/soft004.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/SoftReference/soft004.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]29*30* @library /vmTestbase31* /test/lib32* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.SoftReference.soft004.soft004 -t 133*/3435package gc.gctests.SoftReference.soft004;3637import nsk.share.gc.*;38import java.lang.ref.SoftReference;3940/**41* Test that GC correctly clears soft references.42*43* This test is the same as soft003 except that it creates44* a number of soft references to same object.45*46* @see gc.gctests.SoftReference.soft00347*/48public class soft004 extends ThreadedGCTest {4950class Worker implements Runnable {5152private int arrayLength;53private int objectSize = 100;54private SoftReference[] references;5556private void makeReferences() {57MemoryObject obj = new MemoryObject(objectSize);58references = new SoftReference[arrayLength];59for (int i = 0; i < arrayLength; ++i) {60references[i] = new SoftReference<MemoryObject>(obj);61}62}6364public void run() {65arrayLength = Memory.getArrayLength(runParams.getTestMemory() - objectSize, Memory.getReferenceSize() + objectSize);66System.out.println("Array size: " + arrayLength);67makeReferences();68Algorithms.eatMemory(getExecutionController());69if (!getExecutionController().continueExecution()) {70return;71}72int n = 0;73for (int i = 0; i < arrayLength; ++i) {74if (references[i].get() != null) {75++n;76}77}78if (n != 0) {79log.error("Some of the references have been not cleared: " + n);80setFailed(true);81}82}83}8485@Override86protected Runnable createRunnable(int i) {87return new Worker();88}8990public static void main(String[] args) {91GC.runTest(new soft004(), args);92}93}949596