1 /*
   2  * Copyright (c) 2003, 2013, 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  * The -XX:MarkSweepAlwaysCompactCount=1 argument below makes sure serial gc
  26  * compacts the heap at every full gc so that the usage is correctly updated.
  27  */
  28 
  29 /*
  30  * @test
  31  * @bug     4892507
  32  * @summary Basic Test for MemoryPool.resetPeakUsage()
  33  * @author  Mandy Chung
  34  *
  35  * @library /lib/testlibrary/
  36  * @build jdk.testlibrary.* ResetPeakMemoryUsage MemoryUtil RunUtil
  37  * @run main ResetPeakMemoryUsage
  38  */
  39 
  40 import java.lang.management.*;
  41 import java.util.*;
  42 
  43 public class ResetPeakMemoryUsage {
  44     private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
  45     // make public so that it can't be optimized away easily
  46     public static Object[] obj;
  47 
  48     /**
  49      * Run the test multiple times with different GC versions.
  50      * First with default command line specified by the framework.
  51      * Then with all GC versions specified by the test.
  52      */
  53     public static void main(String a[]) throws Throwable {
  54         final String main = "ResetPeakMemoryUsage$TestMain";
  55         final String ms = "-Xms256m";
  56         final String mn = "-Xmn8m";
  57         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseConcMarkSweepGC");
  58         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseParallelGC");
  59         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:G1HeapRegionSize=1m");
  60         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseSerialGC",
  61                 "-XX:MarkSweepAlwaysCompactCount=1");
  62     }
  63 
  64     private static class TestMain {
  65         public static void main(String[] argv) {
  66             List pools = ManagementFactory.getMemoryPoolMXBeans();
  67             ListIterator iter = pools.listIterator();
  68             boolean found = false;
  69             while (iter.hasNext()) {
  70                 MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
  71                 // only check heap pools that support usage threshold
  72                 // this is typically only the old generation space
  73                 // since the other spaces are expected to get filled up
  74                 if (p.getType() == MemoryType.HEAP &&
  75                     p.isUsageThresholdSupported())
  76                 {
  77                     found = true;
  78                     testPool(p);
  79                 }
  80             }
  81             if (!found) {
  82                 throw new RuntimeException("No heap pool found");
  83             }
  84         }
  85     }
  86 
  87     private static void testPool(MemoryPoolMXBean mpool) {
  88         System.out.println("Selected memory pool: ");
  89         MemoryUtil.printMemoryPool(mpool);
  90 
  91         MemoryUsage usage0 = mpool.getUsage();
  92         MemoryUsage peak0 = mpool.getPeakUsage();
  93 
  94         // use a size that is larger than the young generation and G1 regions
  95         // to force the array into the old gen
  96         int largeArraySize = 9 * 1000 * 1000;
  97 
  98         System.out.println("Before big object array (of size "+largeArraySize+") is allocated: ");
  99         printMemoryUsage(usage0, peak0);
 100 
 101         obj = new Object[largeArraySize];
 102 
 103         MemoryUsage usage1 = mpool.getUsage();
 104         MemoryUsage peak1 = mpool.getPeakUsage();
 105         System.out.println("After the object is allocated: ");
 106         printMemoryUsage(usage1, peak1);
 107 
 108         if (usage1.getUsed() <= usage0.getUsed()) {
 109             throw new RuntimeException(
 110                 formatSize("Before allocation: used", usage0.getUsed()) +
 111                 " expected to be < " +
 112                 formatSize("After allocation: used", usage1.getUsed()));
 113         }
 114 
 115         if (peak1.getUsed() <= peak0.getUsed()) {
 116             throw new RuntimeException(
 117                 formatSize("Before allocation: peak", peak0.getUsed()) +
 118                 " expected to be < " +
 119                 formatSize("After allocation: peak", peak1.getUsed()));
 120         }
 121 
 122 
 123         // The object is now garbage and do a GC
 124         // memory usage should drop
 125         obj = null;
 126         mbean.gc();
 127 
 128         MemoryUsage usage2 = mpool.getUsage();
 129         MemoryUsage peak2 = mpool.getPeakUsage();
 130         System.out.println("After GC: ");
 131         printMemoryUsage(usage2, peak2);
 132 
 133         if (usage2.getUsed() >= usage1.getUsed()) {
 134             throw new RuntimeException(
 135                 formatSize("Before GC: used", usage1.getUsed()) + " " +
 136                 " expected to be > " +
 137                 formatSize("After GC: used", usage2.getUsed()));
 138         }
 139 
 140         mpool.resetPeakUsage();
 141 
 142         MemoryUsage usage3 = mpool.getUsage();
 143         MemoryUsage peak3 = mpool.getPeakUsage();
 144         System.out.println("After resetPeakUsage: ");
 145         printMemoryUsage(usage3, peak3);
 146 
 147         if (peak3.getUsed() != usage3.getUsed()) {
 148             throw new RuntimeException(
 149                 formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
 150                 " expected to be equal to " +
 151                 formatSize("current used", usage3.getUsed()));
 152         }
 153 
 154         if (peak3.getUsed() >= peak2.getUsed()) {
 155             throw new RuntimeException(
 156                 formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
 157                 " expected to be < " +
 158                 formatSize("previous peak", peak2.getUsed()));
 159         }
 160 
 161         System.out.println(RunUtil.successMessage);
 162     }
 163 
 164     private static String INDENT = "    ";
 165     private static void printMemoryUsage(MemoryUsage current, MemoryUsage peak) {
 166         System.out.println("Current Usage: ");
 167         MemoryUtil.printMemoryUsage(current);
 168         System.out.println("Peak Usage: ");
 169         MemoryUtil.printMemoryUsage(peak);
 170 
 171     }
 172     private static String formatSize(String name, long value) {
 173         StringBuffer buf = new StringBuffer(name + " = " + value);
 174         if (value > 0) {
 175             buf.append(" (" + (value >> 10) + "K)");
 176         }
 177         return buf.toString();
 178     }
 179 }