Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/memory/FillingStation/FillingStation.java
41159 views
/*1* Copyright (c) 2002, 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/memory/FillingStation.28* VM Testbase keywords: [gc, stress, nonconcurrent]29*30* @library /vmTestbase31* /test/lib32* @run main/othervm gc.memory.FillingStation.FillingStation33*/3435package gc.memory.FillingStation;3637import jdk.test.lib.Utils;38import java.util.Random;3940public class FillingStation {4142public static final long minObjectSize = 4;43public static final long freeSpaceLimit = 64;44public static final long maxObjectSize = 32*1024;4546public static final boolean debug = false;4748public static void main(String[] arg) {49prologue();50fill();51epilogue();52}5354public static void prologue() {55_beforeMillis = System.currentTimeMillis();56}5758public static void epilogue() {59_afterMillis = System.currentTimeMillis();60if (_overflow) {61System.out.println("Overflowed!");62}63final double deltaSecs = (_afterMillis - _beforeMillis) / 1000.0;64final double freeMegs = ((double) _freeBytes) / (1024.0 * 1024.0);65final double totalMegs = ((double) _totalBytes) / (1024.0 * 1024.0);66final double memRatio = freeMegs / totalMegs;67System.out.println("Runtime.freeMemory()/Runtime.totalMemory: " +68Long.toString(_freeBytes) +69"/" +70Long.toString(_totalBytes) +71" = " +72Double.toString(memRatio));73System.out.println("That took: " +74Double.toString(deltaSecs) +75" seconds");76}7778public static void fill() {79boolean _overflow = false;80Runtime rt = java.lang.Runtime.getRuntime();81Random stream = Utils.getRandomInstance();82Space next = null;83try {84for (long available = rt.freeMemory();85available > freeSpaceLimit;86available = rt.freeMemory()) {87long request = (available - freeSpaceLimit) / 2;88int maxRequest = (int) Math.min(maxObjectSize, request);89int minRequest = (int) Math.max(minObjectSize, maxRequest);90int size = stream.nextInt(minRequest);91if (debug) {92System.err.println("available: " + Long.toString(available) +93" maxRequest: " + Integer.toString(maxRequest) +94" minRequest: " + Integer.toString(minRequest) +95" size: " + Integer.toString(size));96}97next = new Space(size, next);98}99} catch (OutOfMemoryError oome) {100_overflow = true;101}102_freeBytes = rt.freeMemory();103_totalBytes = rt.totalMemory();104}105106static long _beforeMillis = 0L;107static long _afterMillis = 0L;108static long _freeBytes = 0L;109static long _totalBytes = 0L;110static boolean _overflow = false;111}112113class Space {114public Space(int bytes, Space next) {115_next = next;116_space = new byte[bytes];117}118private Space _next = null;119private byte[] _space = null;120}121122123