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