Path: blob/master/test/hotspot/jtreg/gc/arguments/AllocationHelper.java
40943 views
/*1* Copyright (c) 2015, 2019, 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*/2223package gc.arguments;2425import java.util.concurrent.Callable;2627/**28* Helper class which allocates memory.29*30* Typical usage:31* <pre>32* {@code33* AllocationHelper allocator = new AllocationHelper(MAX_ITERATIONS, ARRAY_LENGTH, CHUNK_SIZE,34* () -> (verifier()));35* // Allocate byte[CHUNK_SIZE] ARRAY_LENGTH times. Total allocated bytes will be CHUNK_SIZE * ARRAY_LENGTH + refs length.36* // Then invoke verifier and iterate MAX_ITERATIONS times.37* allocator.allocateMemoryAndVerify();38* }39* </pre>40*/41public final class AllocationHelper {4243private final int arrayLength;44private final int maxIterations;45private final int chunkSize;4647// garbageStorage is used to store link to garbage to prevent optimization.48private static Object garbageStorage;49private byte garbage[][];50private final Callable<?> verifierInstance;5152/**53* Create an AllocationHelper with specified iteration count, array length, chunk size and verifier.54*55* @param maxIterations56* @param arrayLength57* @param chunkSize58* @param verifier - Callable instance which will be invoked after all allocation cycle. Can be null;59*/60public AllocationHelper(int maxIterations, int arrayLength, int chunkSize, Callable<?> verifier) {61if ((arrayLength <= 0) || (maxIterations <= 0) || (chunkSize <= 0)) {62throw new IllegalArgumentException("maxIterations, arrayLength and chunkSize should be greater then 0.");63}64this.arrayLength = arrayLength;65this.maxIterations = maxIterations;66this.chunkSize = chunkSize;67verifierInstance = verifier;68garbage = new byte[this.arrayLength][];69garbageStorage = garbage;70}7172private void allocateMemoryOneIteration() {73for (int j = 0; j < arrayLength; j++) {74garbage[j] = new byte[chunkSize];75}76}7778/**79* Allocate memory and invoke Verifier during all iteration.80*81* @throws java.lang.Exception82*/83public void allocateMemoryAndVerify() throws Exception {84for (int i = 0; i < maxIterations; i++) {85allocateMemoryOneIteration();86if (verifierInstance != null) {87verifierInstance.call();88}89}90}9192/**93* The same as allocateMemoryAndVerify() but hides OOME94*95* @throws Exception96*/97public void allocateMemoryAndVerifyNoOOME() throws Exception {98try {99allocateMemoryAndVerify();100} catch (OutOfMemoryError e) {101// exit on OOME102}103}104105/**106* Release link to allocated garbage to make it available for further GC107*/108public void release() {109if (garbage != null) {110garbage = null;111garbageStorage = null;112}113}114}115116117