Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/SoftReference/SoftReferenceTest/SoftReferenceTest.java
41161 views
/*1* Copyright (c) 2004, 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/SoftReferenceTest.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]29* VM Testbase readme:30* DESCRIPTION31* Test that all SoftReferences has been cleared at time of OOM.32*33* COMMENTS34* This test was ported from JRockit test suite.35*36* @library /vmTestbase37* /test/lib38* @run main/othervm gc.gctests.SoftReference.SoftReferenceTest.SoftReferenceTest -stressTime 60039*/4041package gc.gctests.SoftReference.SoftReferenceTest;4243import java.lang.ref.SoftReference;44import java.util.ArrayList;45import java.util.Random;46import nsk.share.TestFailure;47import nsk.share.gc.GC;48import nsk.share.gc.GCTestBase;49import nsk.share.test.ExecutionController;50import nsk.share.test.Stresser;5152/**53* Tests for the SoftReference handling in JRockit.54*/55public final class SoftReferenceTest extends GCTestBase {5657private ExecutionController stresser;5859/**60* Test that all SoftReferences has been cleared at61* time of OOM.62*63* @return success if all SoftReferences are NULL at64* time of OOM.65*/66public void run() {67//prepare stresser68stresser = new Stresser("Stresser to limit execution time", runParams.getStressOptions());69stresser.start(1);7071long seed;72int minSize;73int maxSize;74int keepEveryXthObject;75long counter = 0;7677seed = runParams.getSeed();78minSize = 2048;79maxSize = 32768;80keepEveryXthObject = 4;818283Random rndGenerator = new Random(seed);84long multiplier = maxSize - minSize;85ArrayList arrSoftRefs = new ArrayList();86ArrayList arrObjects = new ArrayList();87long numberOfNotNulledObjects = 0;88long oomSoftArraySize = 0;8990try {91while (true && stresser.continueExecution()) {92int allocationSize = ((int) (rndGenerator.nextDouble()93* multiplier)) + minSize;94byte[] tmp = new byte[allocationSize];9596// Keep every Xth object to make sure we hit OOM pretty fast97if (counter % keepEveryXthObject == 0) {98arrObjects.add(tmp);99} else {100arrSoftRefs.add(new SoftReference(tmp));101}102103// Make sure the temporary object is dereferenced104tmp = null;105106counter++;107if (counter == Long.MAX_VALUE) {108counter = 0;109}110}111} catch (OutOfMemoryError oome) {112// Get the number of soft refs first, so we don't trigger113// another OOM.114oomSoftArraySize = arrSoftRefs.size();115116for (int i = 0; i < arrSoftRefs.size(); i++) {117SoftReference sr = (SoftReference) arrSoftRefs.get(i);118Object o = sr.get();119120if (o != null) {121numberOfNotNulledObjects++;122}123}124125// Make sure we clear all refs before we return failure, since126// coconut require some memory to complete, and since we're in127// an OOM situation, that could cause trouble.128129arrSoftRefs = null;130arrObjects = null;131132if (numberOfNotNulledObjects > 0) {133throw new TestFailure(numberOfNotNulledObjects + " out of "134+ oomSoftArraySize + " SoftReferences was not "135+ "null at time of OutOfMemoryError");136}137} finally {138arrSoftRefs = null;139arrObjects = null;140}141142143}144145public static void main(String[] args) {146GC.runTest(new SoftReferenceTest(), args);147}148}149150151