0N/A/*
3261N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4892507
0N/A * @summary Basic Test for MemoryPool.resetPeakUsage()
0N/A * @author Mandy Chung
0N/A *
0N/A * @build ResetPeakMemoryUsage MemoryUtil
2495N/A * @run main/othervm ResetPeakMemoryUsage
0N/A */
0N/A
0N/Aimport java.lang.management.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic class ResetPeakMemoryUsage {
0N/A private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
0N/A private static List pools = ManagementFactory.getMemoryPoolMXBeans();
0N/A private static MemoryPoolMXBean mpool = null;
0N/A
0N/A public static void main(String[] argv) {
0N/A ListIterator iter = pools.listIterator();
0N/A while (iter.hasNext()) {
0N/A MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
0N/A if (p.getType() == MemoryType.HEAP &&
0N/A p.isUsageThresholdSupported()) {
0N/A mpool = p;
0N/A System.out.println("Selected memory pool: ");
0N/A MemoryUtil.printMemoryPool(mpool);
0N/A break;
0N/A }
0N/A }
0N/A if (mpool == null) {
0N/A throw new RuntimeException("No heap pool found with threshold != -1");
0N/A }
0N/A
0N/A MemoryUsage usage0 = mpool.getUsage();
0N/A MemoryUsage peak0 = mpool.getPeakUsage();
0N/A final long largeArraySize = (usage0.getMax() - usage0.getUsed()) / 10;
0N/A
0N/A System.out.println("Before big object is allocated: ");
0N/A printMemoryUsage();
0N/A
0N/A // Allocate a big array - need to allocate from the old gen
0N/A Object[][][] obj = new Object[1][1][(int) largeArraySize];
0N/A
0N/A System.out.println("After the object is allocated: ");
0N/A printMemoryUsage();
0N/A
0N/A MemoryUsage usage1 = mpool.getUsage();
0N/A MemoryUsage peak1 = mpool.getPeakUsage();
0N/A
0N/A if (usage1.getUsed() <= usage0.getUsed()) {
0N/A throw new RuntimeException(
0N/A formatSize("Before allocation: used", usage0.getUsed()) +
0N/A " expected to be > " +
0N/A formatSize("After allocation: used", usage1.getUsed()));
0N/A }
0N/A
0N/A if (peak1.getUsed() <= peak0.getUsed()) {
0N/A throw new RuntimeException(
0N/A formatSize("Before allocation: peak", peak0.getUsed()) +
0N/A " expected to be > " +
0N/A formatSize("After allocation: peak", peak1.getUsed()));
0N/A }
0N/A
0N/A
0N/A // The object is now garbage and do a GC
0N/A // memory usage should drop
0N/A obj = null;
0N/A mbean.gc();
0N/A
0N/A System.out.println("After GC: ");
0N/A printMemoryUsage();
0N/A
0N/A MemoryUsage usage2 = mpool.getUsage();
0N/A MemoryUsage peak2 = mpool.getPeakUsage();
0N/A
0N/A if (usage2.getUsed() >= usage1.getUsed()) {
0N/A throw new RuntimeException(
0N/A formatSize("Before GC: used", usage1.getUsed()) + " " +
0N/A " expected to be > " +
0N/A formatSize("After GC: used", usage2.getUsed()));
0N/A }
0N/A
0N/A if (peak2.getUsed() != peak1.getUsed()) {
0N/A throw new RuntimeException(
0N/A formatSize("Before GC: peak", peak1.getUsed()) + " " +
0N/A " expected to be equal to " +
0N/A formatSize("After GC: peak", peak2.getUsed()));
0N/A }
0N/A
0N/A mpool.resetPeakUsage();
0N/A
0N/A System.out.println("After resetPeakUsage: ");
0N/A printMemoryUsage();
0N/A
0N/A MemoryUsage usage3 = mpool.getUsage();
0N/A MemoryUsage peak3 = mpool.getPeakUsage();
0N/A
0N/A if (peak3.getUsed() != usage3.getUsed()) {
0N/A throw new RuntimeException(
0N/A formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
0N/A " expected to be equal to " +
0N/A formatSize("current used", usage3.getUsed()));
0N/A }
0N/A
0N/A if (peak3.getUsed() >= peak2.getUsed()) {
0N/A throw new RuntimeException(
0N/A formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
0N/A " expected to be < " +
0N/A formatSize("previous peak", peak2.getUsed()));
0N/A }
0N/A
0N/A System.out.println("Test passed.");
0N/A }
0N/A
0N/A private static String INDENT = " ";
0N/A private static void printMemoryUsage() {
0N/A MemoryUsage current = mpool.getUsage();
0N/A MemoryUsage peak = mpool.getPeakUsage();
0N/A System.out.println("Current Usage: ");
0N/A MemoryUtil.printMemoryUsage(current);
0N/A System.out.println("Peak Usage: ");
0N/A MemoryUtil.printMemoryUsage(peak);
0N/A
0N/A }
0N/A private static String formatSize(String name, long value) {
0N/A StringBuffer buf = new StringBuffer(name + " = " + value);
0N/A if (value > 0) {
0N/A buf.append(" (" + (value >> 10) + "K)");
0N/A }
0N/A return buf.toString();
0N/A }
0N/A}