1 /*
   2  * Copyright (c) 2015, 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  * This is a helper class that forces the garbage collecting process. It
  26  * attempts to consume all memory available to the young generation by
  27  * creating a lot of unreferenced objects. Sooner or later the garbage
  28  * collector shall be invoked.
  29  *
  30  * For purposes of performance the task is implemented to be ran with 
  31  * a small memory size less or equal to 128 megabytes. This shall be
  32  * excplicitly specified with -Xmx JVM's option. In case if this option
  33  * is not specified it's not guaranteed that GC will be invoked.
  34  *
  35  * The class is intended to be used with -Xloggc option to provide diagnostics
  36  * output which can be parsed by the real tests.
  37  *
  38  * The class is implemented as an alternative to the explicit call to 
  39  * System.gc(). The second has some drawbacks and should be avoided.
  40  */
  41 public class GCTask {
  42     
  43     public static final int BLOCK_SIZE = 16 * 1024; // 16 kilobytes
  44     public static final int MAX_ITERATIONS = 8192; // 16K * 8K = 128M
  45     public static byte[] garbage;
  46 
  47     @SuppressWarnings("unused")
  48     public void run() {
  49         // ensure there's an object that will survive garbage collecting
  50         byte[] survivor = new byte[BLOCK_SIZE];
  51 
  52         // if -Xmx has not been specified we assume that we're working within
  53         // 128 megabytes of memory
  54         long iterCount = Math.min(Runtime.getRuntime().maxMemory() / BLOCK_SIZE,
  55             MAX_ITERATIONS);
  56         while (iterCount-- > 1) {
  57             // make an object that will be eligible for garbage collecting
  58             garbage = new byte[BLOCK_SIZE];
  59         }
  60     }
  61     
  62     public static void main(String[] args) {
  63         new GCTask().run();
  64     }
  65 
  66 }