--- /dev/null 2015-01-17 02:57:05.640001672 +0300 +++ new/test/gc/logging/GCTask.java 2015-01-20 17:14:16.073399906 +0300 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * This is a helper class that forces the garbage collecting process. It + * attempts to consume all memory available to the young generation by + * creating a lot of unreferenced objects. Sooner or later the garbage + * collector shall be invoked. + * + * For purposes of performance the task is implemented to be ran with + * a small memory size less or equal to 128 megabytes. This shall be + * excplicitly specified with -Xmx JVM's option. In case if this option + * is not specified it's not guaranteed that GC will be invoked. + * + * The class is intended to be used with -Xloggc option to provide diagnostics + * output which can be parsed by the real tests. + * + * The class is implemented as an alternative to the explicit call to + * System.gc(). The second has some drawbacks and should be avoided. + */ +public class GCTask { + + public static final int BLOCK_SIZE = 16 * 1024; // 16 kilobytes + public static final int MAX_ITERATIONS = 8192; // 16K * 8K = 128M + public static byte[] garbage; + + @SuppressWarnings("unused") + public void run() { + // ensure there's an object that will survive garbage collecting + byte[] survivor = new byte[BLOCK_SIZE]; + + // if -Xmx has not been specified we assume that we're working within + // 128 megabytes of memory + long iterCount = Math.min(Runtime.getRuntime().maxMemory() / BLOCK_SIZE, + MAX_ITERATIONS); + while (iterCount-- > 1) { + // make an object that will be eligible for garbage collecting + garbage = new byte[BLOCK_SIZE]; + } + } + + public static void main(String[] args) { + new GCTask().run(); + } + +}