Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/AllocateWithoutOomTest/AllocateWithoutOomTest.java
41155 views
/*1* Copyright (c) 2011, 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/AllocateWithoutOomTest.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]29* VM Testbase readme:30* DESCRIPTION31* Small stress test that should be able to run for a specified32* time without hitting an OOM.33*34* COMMENTS35* This test was ported from JRockit test suite.36*37* @library /vmTestbase38* /test/lib39* @run main/othervm40* -XX:-UseGCOverheadLimit41* gc.gctests.AllocateWithoutOomTest.AllocateWithoutOomTest42*/4344package gc.gctests.AllocateWithoutOomTest;4546import java.util.ArrayList;47import java.util.Random;48import nsk.share.TestFailure;49import nsk.share.gc.GC;50import nsk.share.gc.GCTestBase;51import nsk.share.test.Stresser;5253/**54* Small stress test that should be able to run for a specified55* time without hitting an OOM.56*/57public class AllocateWithoutOomTest extends GCTestBase {5859/**60* Small stress test that allocates objects in a certain interval61* and runs for a specified time. It should not throw any OOM during62* the execution.63*64* @return success if the test runs for the specified time without65* and exceptions being thrown.66*/67@Override68public void run() {69int minSize;70int maxSize;71727374minSize = 2048;75maxSize = 32768;767778ArrayList placeholder = new ArrayList();79long multiplier = maxSize - minSize;80Random rndGenerator = new Random(runParams.getSeed());8182long memoryUpperLimit = runParams.getTestMemory();83long memoryLowerLimit = runParams.getTestMemory() / 3;84long memoryAllocatedLowerLimit = memoryUpperLimit85- memoryLowerLimit;868788long totalAllocatedMemory = 0;89long totalAllocatedObjects = 0;90int allocationSize = -1;91long roundCounter = 1;9293try {94Stresser stresser = new Stresser(runParams.getStressOptions());95stresser.start(0);96while (stresser.continueExecution()) {97while (totalAllocatedMemory < memoryUpperLimit) {98allocationSize = ((int) (rndGenerator.nextDouble()99* multiplier)) + minSize;100byte[] tmp = new byte[allocationSize];101totalAllocatedMemory += allocationSize;102totalAllocatedObjects++;103placeholder.add(tmp);104tmp = null;105}106107// NOTE: Start on index 1 to make sure we don't remove to many108// consecutive objects in the beginning109int indexToRemove = 1;110111while (totalAllocatedMemory > memoryAllocatedLowerLimit) {112// NOTE: Terminate if we only have zero objects left113if (placeholder.size() == 0) {114throw new TestFailure("No more objects to free, "115+ "so we can't continue");116}117118if (indexToRemove >= placeholder.size()) {119indexToRemove = (placeholder.size() == 1) ? 0 : 1;120}121122byte[] tmp = (byte[]) placeholder.remove(indexToRemove);123124totalAllocatedMemory -= tmp.length;125totalAllocatedObjects--;126127tmp = null;128// NOTE: Since we removed one object, we only need to129// increment index by 1 to move two steps. We want to130// remove every other object to create fragmentation131indexToRemove++;132}133134roundCounter++;135}136placeholder = null;137log.info("Passed. Completed " + roundCounter138+ " rounds during the test");139} catch (OutOfMemoryError oome) {140placeholder = null;141throw new TestFailure("OOM thrown when allocating an object of size "142+ allocationSize, oome);143} finally {144placeholder = null;145}146}147148public static void main(String[] args) {149GC.runTest(new AllocateWithoutOomTest(), args);150}151}152153154