1 /* 2 * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @key stress gc 27 * 28 * @summary converted from VM Testbase gc/gctests/SoftReference/SoftReferenceTest. 29 * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit] 30 * VM Testbase readme: 31 * DESCRIPTION 32 * Test that all SoftReferences has been cleared at time of OOM. 33 * 34 * COMMENTS 35 * This test was ported from JRockit test suite. 36 * 37 * @library /vmTestbase 38 * /test/lib 39 * @run driver jdk.test.lib.FileInstaller . . 40 * @run main/othervm gc.gctests.SoftReference.SoftReferenceTest.SoftReferenceTest -stressTime 600 41 */ 42 43 package gc.gctests.SoftReference.SoftReferenceTest; 44 45 import java.lang.ref.SoftReference; 46 import java.util.ArrayList; 47 import java.util.Random; 48 import nsk.share.TestFailure; 49 import nsk.share.gc.GC; 50 import nsk.share.gc.GCTestBase; 51 import nsk.share.test.ExecutionController; 52 import nsk.share.test.Stresser; 53 54 /** 55 * Tests for the SoftReference handling in JRockit. 56 */ 57 public final class SoftReferenceTest extends GCTestBase { 58 59 private ExecutionController stresser; 60 61 /** 62 * Test that all SoftReferences has been cleared at 63 * time of OOM. 64 * 65 * @return success if all SoftReferences are NULL at 66 * time of OOM. 67 */ 68 public void run() { 69 //prepare stresser 70 stresser = new Stresser("Stresser to limit execution time", runParams.getStressOptions()); 71 stresser.start(1); 72 73 long seed; 74 int minSize; 75 int maxSize; 76 int keepEveryXthObject; 77 long counter = 0; 78 79 seed = runParams.getSeed(); 80 minSize = 2048; 81 maxSize = 32768; 82 keepEveryXthObject = 4; 83 84 85 Random rndGenerator = new Random(seed); 86 long multiplier = maxSize - minSize; 87 ArrayList arrSoftRefs = new ArrayList(); 88 ArrayList arrObjects = new ArrayList(); 89 long numberOfNotNulledObjects = 0; 90 long oomSoftArraySize = 0; 91 92 try { 93 while (true && stresser.continueExecution()) { 94 int allocationSize = ((int) (rndGenerator.nextDouble() 95 * multiplier)) + minSize; 96 byte[] tmp = new byte[allocationSize]; 97 98 // Keep every Xth object to make sure we hit OOM pretty fast 99 if (counter % keepEveryXthObject == 0) { 100 arrObjects.add(tmp); 101 } else { 102 arrSoftRefs.add(new SoftReference(tmp)); 103 } 104 105 // Make sure the temporary object is dereferenced 106 tmp = null; 107 108 counter++; 109 if (counter == Long.MAX_VALUE) { 110 counter = 0; 111 } 112 } 113 } catch (OutOfMemoryError oome) { 114 // Get the number of soft refs first, so we don't trigger 115 // another OOM. 116 oomSoftArraySize = arrSoftRefs.size(); 117 118 for (int i = 0; i < arrSoftRefs.size(); i++) { 119 SoftReference sr = (SoftReference) arrSoftRefs.get(i); 120 Object o = sr.get(); 121 122 if (o != null) { 123 numberOfNotNulledObjects++; 124 } 125 } 126 127 // Make sure we clear all refs before we return failure, since 128 // coconut require some memory to complete, and since we're in 129 // an OOM situation, that could cause trouble. 130 131 arrSoftRefs = null; 132 arrObjects = null; 133 134 if (numberOfNotNulledObjects > 0) { 135 throw new TestFailure(numberOfNotNulledObjects + " out of " 136 + oomSoftArraySize + " SoftReferences was not " 137 + "null at time of OutOfMemoryError"); 138 } 139 } finally { 140 arrSoftRefs = null; 141 arrObjects = null; 142 } 143 144 145 } 146 147 public static void main(String[] args) { 148 GC.runTest(new SoftReferenceTest(), args); 149 } 150 }