Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/HeapUsageTest/HeapUsageTest.java
41159 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 stress26*27* @summary converted from VM Testbase gc/gctests/HeapUsageTest.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]29* VM Testbase readme:30* DESCRIPTION31* Originally it was Micro benchmark that tests the heap usage.32*33* COMMENTS34* This test was ported from JRockit test suite.35*36* @library /vmTestbase37* /test/lib38* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.HeapUsageTest.HeapUsageTest39*/4041package gc.gctests.HeapUsageTest;4243import java.util.ArrayList;44import nsk.share.TestFailure;45import nsk.share.gc.GC;46import nsk.share.gc.GCTestBase;47import nsk.share.test.Stresser;4849/**50* Micro benchmark that tests the heap usage.51*/52public class HeapUsageTest extends GCTestBase {5354/**55* Helper class to store allocation size and iterations for the56* HeapUsageTest heap usage test program57*/58private class TestValue {5960private int allocationSize;61private int allocationIterations;6263TestValue(int allocSize, int allocIters) {64allocationSize = allocSize;65allocationIterations = allocIters;66}6768final int getSize() {69return allocationSize;70}7172final int getIterations() {73return allocationIterations;74}75}7677/**78* Simple micro benchmark for testing heap usage. Returns a percentage79* that tells how much of the total heap size the test was able to80* allocate.81*82* @return success if test could run until OOME was thrown, and was83* able to determine the heap usage in percent without any other84* exceptions being thrown.85*/86public void run() {8788try {89int[] testParams =90new int[]{512, 5, 2048, 3, 3145728, 2};919293TestValue[] values = new TestValue[testParams.length / 2];94for (int i = 0; i < testParams.length / 2; i++) {95values[i] = new TestValue(testParams[i * 2],96testParams[i * 2 + 1]);97}9899// NOTE: The call to Runtime might not look like it does anything100// here, but it codegens the class, so it will not OOM later on101// due to low-mem sitation for codegen.102Runtime r = Runtime.getRuntime();103// NOTE: Codegen freeMemory() and maxMemory() so this104// doesn't cause OOM in a OOM situation105ArrayList holdObjects = new ArrayList();106long currentAllocatedSize = 0;107Stresser stresser = new Stresser(runParams.getStressOptions());108stresser.start(0);109try {110long loopCount;111int nrOfLoops = 0;112113for (int i = 0; i < values.length; i++) {114if (values[i].getIterations() > nrOfLoops) {115nrOfLoops = values[i].getIterations();116}117}118119for (loopCount = 0;; loopCount++) {120for (int i = 0; i < nrOfLoops; i++) {121for (int k = 0; k < values.length; k++) {122if (i < values[k].getIterations()) {123if (!stresser.continueExecution()) {124// no time to eat all heap125return;126}127byte[] tmp = new byte[values[k].getSize()];128holdObjects.add(tmp);129currentAllocatedSize += (long) values[k].getSize();130}131}132}133}134} catch (OutOfMemoryError oome) {135long oomMaxMemory = r.maxMemory();136137holdObjects = null;138139double myPercentUsed =140(((double) (currentAllocatedSize))141/ oomMaxMemory) * 100;142143log.info("Heap usage percentage ( "144+ myPercentUsed + " %) " + myPercentUsed);145} finally {146// NOTE: In case OOM wasn't hit, release references147// and cleanup by calling System.gc();148holdObjects = null;149}150} catch (OutOfMemoryError oome2) {151throw new TestFailure("OutOfMemoryError thrown even though it shouldn't. "152+ "Please investigate.", oome2);153}154}155156public static void main(String[] args) {157GC.runTest(new HeapUsageTest(), args);158}159}160161162