Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/SoftReference/soft003/soft003.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/soft003.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]29*30* @library /vmTestbase31* /test/lib32* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.SoftReference.soft003.soft003 -t 133*/3435package gc.gctests.SoftReference.soft003;3637import java.lang.ref.Reference;38import nsk.share.test.*;39import nsk.share.gc.*;40import nsk.share.TestFailure;41import java.lang.ref.SoftReference;4243/**44* Test that GC clears soft references before throwing OOM.45*46* This test creates a number of soft references, then provokes47* GC with Algorithms.eatMemory() and checks that all references48* have been cleared.49*/50public class soft003 extends ThreadedGCTest {5152class Worker implements Runnable {5354private int arrayLength;55private int objectSize = 100;56private Reference[] references;5758public void run() {59for (int i = 0; i < arrayLength; ++i) {60references[i] = new SoftReference<MemoryObject>(new MemoryObject(LocalRandom.nextInt(objectSize)));61}62Algorithms.eatMemory(getExecutionController());63if (!getExecutionController().continueExecution()) {64return;65}66// Check that all references have been cleared67int n = 0;68for (int i = 0; i < arrayLength; ++i) {69if (references[i].get() != null) {70++n;71}72}73if (n != 0) {74references = null;75throw new TestFailure("Some of the references have been not cleared: " + n);76}77}7879public void Worker() {80arrayLength = Memory.getArrayLength(runParams.getTestMemory(), Memory.getReferenceSize() + objectSize);81System.out.println("Array size: " + arrayLength);82references = new Reference[arrayLength];83}84}8586@Override87protected Runnable createRunnable(int i) {88return new Worker();89}9091public static void main(String[] args) {92GC.runTest(new soft003(), args);93}94}959697