1 /*
   2  * Copyright (c) 2002, 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/memory/FillingStation.
  29  * VM Testbase keywords: [gc, stress, nonconcurrent]
  30  *
  31  * @library /vmTestbase
  32  *          /test/lib
  33  * @run driver jdk.test.lib.FileInstaller . .
  34  * @run main/othervm gc.memory.FillingStation.FillingStation
  35  */
  36 
  37 package gc.memory.FillingStation;
  38 
  39 public class FillingStation {
  40 
  41     public static final long minObjectSize  = 4;
  42     public static final long freeSpaceLimit = 64;
  43     public static final long maxObjectSize  = 32*1024;
  44 
  45     public static final boolean debug        = false;
  46 
  47     public static void main(String[] arg) {
  48         prologue();
  49         fill();
  50         epilogue();
  51     }
  52 
  53     public static void prologue() {
  54         _beforeMillis = System.currentTimeMillis();
  55     }
  56 
  57     public static void epilogue() {
  58         _afterMillis = System.currentTimeMillis();
  59         if (_overflow) {
  60             System.out.println("Overflowed!");
  61         }
  62         final double deltaSecs = (_afterMillis - _beforeMillis) / 1000.0;
  63         final double freeMegs = ((double) _freeBytes) / (1024.0 * 1024.0);
  64         final double totalMegs = ((double) _totalBytes) / (1024.0 * 1024.0);
  65         final double memRatio = freeMegs / totalMegs;
  66         System.out.println("Runtime.freeMemory()/Runtime.totalMemory: " +
  67                            Long.toString(_freeBytes) +
  68                            "/" +
  69                            Long.toString(_totalBytes) +
  70                            " = " +
  71                            Double.toString(memRatio));
  72         System.out.println("That took: " +
  73                            Double.toString(deltaSecs) +
  74                            " seconds");
  75     }
  76 
  77     public static void fill() {
  78         boolean _overflow = false;
  79         Runtime rt = java.lang.Runtime.getRuntime();
  80         java.util.Random stream = new java.util.Random();
  81         Space next = null;
  82         try {
  83             for (long available = rt.freeMemory();
  84                  available > freeSpaceLimit;
  85                  available = rt.freeMemory()) {
  86                 long request   = (available - freeSpaceLimit) / 2;
  87                 int maxRequest = (int) Math.min(maxObjectSize, request);
  88                 int minRequest = (int) Math.max(minObjectSize, maxRequest);
  89                 int size = stream.nextInt(minRequest);
  90                 if (debug) {
  91                     System.err.println("available: " + Long.toString(available) +
  92                                        "  maxRequest: " + Integer.toString(maxRequest) +
  93                                        "  minRequest: " + Integer.toString(minRequest) +
  94                                        "  size: " + Integer.toString(size));
  95                 }
  96                 next = new Space(size, next);
  97             }
  98         } catch (OutOfMemoryError oome) {
  99             _overflow = true;
 100         }
 101         _freeBytes = rt.freeMemory();
 102         _totalBytes = rt.totalMemory();
 103     }
 104 
 105     static long    _beforeMillis = 0L;
 106     static long    _afterMillis  = 0L;
 107     static long    _freeBytes    = 0L;
 108     static long    _totalBytes   = 0L;
 109     static boolean _overflow     = false;
 110 }
 111 
 112 class Space {
 113     public Space(int bytes, Space next) {
 114         _next = next;
 115         _space = new byte[bytes];
 116     }
 117     private Space              _next  = null;
 118     private byte[]             _space = null;
 119 }