< prev index next >

test/jdk/java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java

Print this page




  31  * @bug     4892507
  32  * @summary Basic Test for MemoryPool.resetPeakUsage()
  33  * @author  Mandy Chung
  34  *
  35  * @requires vm.opt.ExplicitGCInvokesConcurrent != "true"
  36  * @requires vm.opt.DisableExplicitGC != "true"
  37  * @library /lib/testlibrary/ /test/lib
  38  * @modules jdk.management
  39  *
  40  * @build jdk.testlibrary.* ResetPeakMemoryUsage MemoryUtil RunUtil
  41  * @build sun.hotspot.WhiteBox
  42  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  43  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. ResetPeakMemoryUsage
  44  */
  45 
  46 import java.lang.management.*;
  47 import java.lang.ref.WeakReference;
  48 import java.util.*;
  49 
  50 import sun.hotspot.code.Compiler;

  51 
  52 public class ResetPeakMemoryUsage {
  53     private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
  54     // make public so that it can't be optimized away easily
  55     public static Object[] obj;
  56 
  57     /**
  58      * Run the test multiple times with different GC versions.
  59      * First with default command line specified by the framework.
  60      * Then with all GC versions specified by the test.
  61      */
  62     public static void main(String a[]) throws Throwable {
  63         final String main = "ResetPeakMemoryUsage$TestMain";
  64         final String ms = "-Xms256m";
  65         final String mn = "-Xmn8m";
  66         if (!Compiler.isGraalEnabled()) { // Graal does not support CMS
  67             RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseConcMarkSweepGC");
  68         }
  69         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseParallelGC");
  70         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:G1HeapRegionSize=1m");



  71         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseSerialGC",
  72                 "-XX:MarkSweepAlwaysCompactCount=1");
  73     }
  74 
  75     private static class TestMain {
  76         public static void main(String[] argv) {
  77             List pools = ManagementFactory.getMemoryPoolMXBeans();
  78             ListIterator iter = pools.listIterator();
  79             boolean found = false;
  80             while (iter.hasNext()) {
  81                 MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
  82                 // only check heap pools that support usage threshold
  83                 // this is typically only the old generation space
  84                 // since the other spaces are expected to get filled up
  85                 if (p.getType() == MemoryType.HEAP &&
  86                     p.isUsageThresholdSupported())
  87                 {














  88                     found = true;
  89                     testPool(p);
  90                 }
  91             }

  92             if (!found) {
  93                 throw new RuntimeException("No heap pool found");
  94             }
  95         }
  96     }
  97 
  98     private static void testPool(MemoryPoolMXBean mpool) {
  99         System.out.println("Selected memory pool: ");
 100         MemoryUtil.printMemoryPool(mpool);
 101 
 102         MemoryUsage usage0 = mpool.getUsage();
 103         MemoryUsage peak0 = mpool.getPeakUsage();
 104 
 105         // use a size that is larger than the young generation and G1 regions
 106         // to force the array into the old gen
 107         int largeArraySize = 9 * 1000 * 1000;
 108 
 109         System.out.println("Before big object array (of size "+largeArraySize+") is allocated: ");
 110         printMemoryUsage(usage0, peak0);
 111 


 114 
 115         MemoryUsage usage1 = mpool.getUsage();
 116         MemoryUsage peak1 = mpool.getPeakUsage();
 117         System.out.println("After the object is allocated: ");
 118         printMemoryUsage(usage1, peak1);
 119 
 120         if (usage1.getUsed() <= usage0.getUsed()) {
 121             throw new RuntimeException(
 122                 formatSize("Before allocation: used", usage0.getUsed()) +
 123                 " expected to be < " +
 124                 formatSize("After allocation: used", usage1.getUsed()));
 125         }
 126 
 127         if (peak1.getUsed() <= peak0.getUsed()) {
 128             throw new RuntimeException(
 129                 formatSize("Before allocation: peak", peak0.getUsed()) +
 130                 " expected to be < " +
 131                 formatSize("After allocation: peak", peak1.getUsed()));
 132         }
 133 
 134 
 135         // The object is now garbage and do a GC
 136         // memory usage should drop
 137         obj = null;
 138 
 139         //This will cause sure shot GC unlike Runtime.gc() invoked by mbean.gc()
 140         while(weakRef.get() != null) {
 141             mbean.gc();
 142         }
 143 
 144         MemoryUsage usage2 = mpool.getUsage();
 145         MemoryUsage peak2 = mpool.getPeakUsage();
 146         System.out.println("After GC: ");
 147         printMemoryUsage(usage2, peak2);
 148 
 149         if (usage2.getUsed() >= usage1.getUsed()) {
 150             throw new RuntimeException(
 151                 formatSize("Before GC: used", usage1.getUsed()) + " " +
 152                 " expected to be > " +
 153                 formatSize("After GC: used", usage2.getUsed()));
 154         }
 155 
 156         mpool.resetPeakUsage();
 157 
 158         MemoryUsage usage3 = mpool.getUsage();
 159         MemoryUsage peak3 = mpool.getPeakUsage();
 160         System.out.println("After resetPeakUsage: ");




  31  * @bug     4892507
  32  * @summary Basic Test for MemoryPool.resetPeakUsage()
  33  * @author  Mandy Chung
  34  *
  35  * @requires vm.opt.ExplicitGCInvokesConcurrent != "true"
  36  * @requires vm.opt.DisableExplicitGC != "true"
  37  * @library /lib/testlibrary/ /test/lib
  38  * @modules jdk.management
  39  *
  40  * @build jdk.testlibrary.* ResetPeakMemoryUsage MemoryUtil RunUtil
  41  * @build sun.hotspot.WhiteBox
  42  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  43  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. ResetPeakMemoryUsage
  44  */
  45 
  46 import java.lang.management.*;
  47 import java.lang.ref.WeakReference;
  48 import java.util.*;
  49 
  50 import sun.hotspot.code.Compiler;
  51 import sun.hotspot.WhiteBox;
  52 
  53 public class ResetPeakMemoryUsage {
  54     private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
  55     // make public so that it can't be optimized away easily
  56     public static Object[] obj;
  57 
  58     /**
  59      * Run the test multiple times with different GC versions.
  60      * First with default command line specified by the framework.
  61      * Then with all GC versions specified by the test.
  62      */
  63     public static void main(String a[]) throws Throwable {
  64         final String main = "ResetPeakMemoryUsage$TestMain";
  65         final String ms = "-Xms256m";
  66         final String mn = "-Xmn8m";
  67         if (!Compiler.isGraalEnabled()) { // Graal does not support CMS
  68             RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseConcMarkSweepGC");
  69         }
  70         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseParallelGC");
  71         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:-G1UseLegacyMonitoring",
  72                                    "-XX:G1HeapRegionSize=1m");
  73         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:+G1UseLegacyMonitoring",
  74                                    "-XX:G1HeapRegionSize=1m");
  75         RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseSerialGC",
  76                                    "-XX:MarkSweepAlwaysCompactCount=1");
  77     }
  78 
  79     private static class TestMain {
  80         public static void main(String[] argv) {
  81             List pools = ManagementFactory.getMemoryPoolMXBeans();
  82             ListIterator iter = pools.listIterator();
  83             boolean found = false;
  84             while (iter.hasNext()) {
  85                 MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
  86                 // Only check heap pools that support a usage threshold.
  87                 // This is typically only the old generation space
  88                 // since the other spaces are expected to get filled up.
  89                 if (p.getType() == MemoryType.HEAP &&
  90                     p.isUsageThresholdSupported()) {
  91                     // In all collectors except G1, only the old generation supports a
  92                     // usage threshold. The G1 legacy mode "G1 Old Gen" also does. In
  93                     // G1 default mode, both the old space ("G1 Old Space": it's not
  94                     // really a generation in the non-G1 collector sense) and the
  95                     // humongous space ("G1 Humongous Space"), support a usage threshold.
  96                     // So, the following condition is true for all non-G1 old generations,
  97                     // for the G1 legacy old gen, and for the G1 default humongous space.
  98                     // It is not true for the G1 default old gen.
  99                     //
 100                     // We're allocating humongous objects in this test, so the G1 default
 101                     // mode "G1 Old Space" occupancy doesn't change, because humongous
 102                     // objects are allocated in the "G1 Humongous Space". If we allowed
 103                     // the G1 default mode "G1 Old Space", notification would never
 104                     // happen because no objects are allocated there.
 105                     if (!p.getName().equals("G1 Old Space")) {
 106                         found = true;
 107                         testPool(p);
 108                     }
 109                 }
 110             }
 111             if (!found) {
 112                 throw new RuntimeException("No heap pool found");
 113             }
 114         }
 115     }
 116 
 117     private static void testPool(MemoryPoolMXBean mpool) {
 118         System.out.println("Selected memory pool: ");
 119         MemoryUtil.printMemoryPool(mpool);
 120 
 121         MemoryUsage usage0 = mpool.getUsage();
 122         MemoryUsage peak0 = mpool.getPeakUsage();
 123 
 124         // use a size that is larger than the young generation and G1 regions
 125         // to force the array into the old gen
 126         int largeArraySize = 9 * 1000 * 1000;
 127 
 128         System.out.println("Before big object array (of size "+largeArraySize+") is allocated: ");
 129         printMemoryUsage(usage0, peak0);
 130 


 133 
 134         MemoryUsage usage1 = mpool.getUsage();
 135         MemoryUsage peak1 = mpool.getPeakUsage();
 136         System.out.println("After the object is allocated: ");
 137         printMemoryUsage(usage1, peak1);
 138 
 139         if (usage1.getUsed() <= usage0.getUsed()) {
 140             throw new RuntimeException(
 141                 formatSize("Before allocation: used", usage0.getUsed()) +
 142                 " expected to be < " +
 143                 formatSize("After allocation: used", usage1.getUsed()));
 144         }
 145 
 146         if (peak1.getUsed() <= peak0.getUsed()) {
 147             throw new RuntimeException(
 148                 formatSize("Before allocation: peak", peak0.getUsed()) +
 149                 " expected to be < " +
 150                 formatSize("After allocation: peak", peak1.getUsed()));
 151         }
 152 
 153         // The object is now garbage and do a GC. Memory usage should drop.


 154         obj = null;
 155 
 156         // This will cause a sure shot GC unlike Runtime.gc() invoked by mbean.gc()
 157         while (weakRef.get() != null) {
 158             mbean.gc();
 159         }
 160 
 161         MemoryUsage usage2 = mpool.getUsage();
 162         MemoryUsage peak2 = mpool.getPeakUsage();
 163         System.out.println("After GC: ");
 164         printMemoryUsage(usage2, peak2);
 165 
 166         if (usage2.getUsed() >= usage1.getUsed()) {
 167             throw new RuntimeException(
 168                 formatSize("Before GC: used", usage1.getUsed()) + " " +
 169                 " expected to be > " +
 170                 formatSize("After GC: used", usage2.getUsed()));
 171         }
 172 
 173         mpool.resetPeakUsage();
 174 
 175         MemoryUsage usage3 = mpool.getUsage();
 176         MemoryUsage peak3 = mpool.getPeakUsage();
 177         System.out.println("After resetPeakUsage: ");


< prev index next >