Path: blob/master/test/hotspot/jtreg/vmTestbase/metaspace/gc/MemoryUsageTest.java
41155 views
/*1* Copyright (c) 2013, 2018, 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 metaspace.gc;2425/**26* Test observers the progress on used/committed memory.27* MemoryPoolMXBean is used for that purpose.28*29* Depending on command line option the test checks either Metaspace or30* Compressed Class Space area.31*32* This test checks two things:33* 1) Loading/Unloading classes doesn't cause memory increase34* 2) Loading classes causes permanent increase of memory.35*/36public class MemoryUsageTest extends MetaspaceBaseGC {3738private String pool_name;3940public static void main(String[] args) {41new MemoryUsageTest().run(args);42}43444546/**47* Loads new classes by bunches and invokes GC after each bunch.48* Expected behavior: used/committed should stop growing after 5 iterations.49*/50public void checkForNotGrowing() {5152long p_used = 0;53long p_committed = 0;5455System.out.println("%%%% Loading classes without storing refs, invoking gc manually");56final int numberOfIteration = 10;57for (int i = 0; i < numberOfIteration; i++) {58loadNewClasses(500, false);59gc();60printMemoryUsage("% " + i + " ");61if (i == numberOfIteration / 2) {62// used/committed in the middle of the step.63p_used = getUsed();64p_committed = getCommitted();65}6667}68long used = getUsed();69long committed = getCommitted();7071// loading classes without keeping references to them72// should not affect used/commited metaspace73// but OK, let's allow some noise such as +/-8K74if (Math.abs((int) (used - p_used)) > 1024*8) {75throw new Fault("Used amount should be stable: " +76p_used + " --> " + used);77}78if (Math.abs((int) (committed - p_committed)) > 1024*8) {79throw new Fault("Committed amount should be stable: " +80p_committed + " --> " + committed);81}8283}8485/**86* Loads new classes by bunches and invokes GC after each bunch.87* Expected behavior: used/committed should keep growing88*/89public void checkForGrowing() {90long used = 0;91long committed = 0;92long p_used = 0 ;93long p_committed = 0;9495// loading new classes, starting to keep references.96// both used and commited metaspace should grow up.97System.out.println("%%%% Loading classes, refs are stored, gc is invoking manually");98for (int i = 0; i < 10; i++) {99try {100loadNewClasses(1000, true);101} catch (OutOfMemoryError oom) {102String message = oom.getMessage().toLowerCase();103if (message.contains("metaspace") || message.contains("compressed class space")) {104System.out.println("% oom is ok: " + oom);105return;106} else {107System.err.println("% unexpected OOM" + oom);108throw new Fault(oom);109}110}111112gc();113printMemoryUsage("% " + i + " ");114p_used = used;115p_committed = committed;116used = getUsed();117committed = getCommitted();118if (i > 0 && used <= p_used) {119throw new Fault("Used amount reduced unexpectedly " +120p_used + " --> " + used);121}122if (i > 0 && committed < p_committed) {123throw new Fault("Used amount reduced unexpectedly " +124p_committed + " --> " + committed);125}126}127}128129/**130* Looks up for memory pool name.131* @param args command line options132*/133@Override134protected void parseArgs(String[] args) {135if (args.length != 1) {136printUsage();137throw new Fault("MemoryPool is not specified");138}139140String a = args[0];141if (a.equalsIgnoreCase("-pool:compressed")) {142pool_name = "Compressed Class Space";143} else if (a.equalsIgnoreCase("-pool:metaspace")) {144pool_name = "Metaspace";145} else {146printUsage();147throw new Fault("Unrecongnized argument: " + a);148}149}150151private void printUsage() {152System.err.println("Usage: ");153System.err.println("java [-Xms..] [-XX:MetaspaceSize=..] [-XX:MaxMetaspaceSize=..] \\");154System.err.println(" " + MemoryUsageTest.class.getCanonicalName() + " -pool:<metaspace|compressed>");155}156157/**158* @return name of the MemoryPoolMXBean under test159*/160@Override161protected String getPoolName() {162return pool_name;163}164165@Override166protected void doCheck() {167checkForNotGrowing();168checkForGrowing();169}170171}172173174