jdk/test/java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java
changeset 17438 c01b1889fbc8
parent 7668 d4a77089c587
child 17727 1703e6521c97
equal deleted inserted replaced
17437:c29f8d00fc0b 17438:c01b1889fbc8
    26  * @bug     4892507
    26  * @bug     4892507
    27  * @summary Basic Test for MemoryPool.resetPeakUsage()
    27  * @summary Basic Test for MemoryPool.resetPeakUsage()
    28  * @author  Mandy Chung
    28  * @author  Mandy Chung
    29  *
    29  *
    30  * @build ResetPeakMemoryUsage MemoryUtil
    30  * @build ResetPeakMemoryUsage MemoryUtil
    31  * @run main/othervm ResetPeakMemoryUsage
    31  * @run main/othervm -XX:+UseSerialGC -Xmn8m ResetPeakMemoryUsage
       
    32  * @run main/othervm -XX:+UseConcMarkSweepGC -Xmn8m ResetPeakMemoryUsage
       
    33  * @run main/othervm -XX:+UseParallelGC -Xmn8m ResetPeakMemoryUsage
       
    34  * @run main/othervm -XX:+UseG1GC -Xmn8m -XX:G1HeapRegionSize=1m ResetPeakMemoryUsage
    32  */
    35  */
    33 
    36 
    34 import java.lang.management.*;
    37 import java.lang.management.*;
    35 import java.util.*;
    38 import java.util.*;
    36 
    39 
    37 public class ResetPeakMemoryUsage {
    40 public class ResetPeakMemoryUsage {
    38     private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
    41     private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
    39     private static List pools = ManagementFactory.getMemoryPoolMXBeans();
    42     // make public so that it can't be optimized away easily
    40     private static MemoryPoolMXBean mpool = null;
    43     public static Object[] obj;
    41 
    44 
    42     public static void main(String[] argv) {
    45     public static void main(String[] argv) {
       
    46         List pools = ManagementFactory.getMemoryPoolMXBeans();
    43         ListIterator iter = pools.listIterator();
    47         ListIterator iter = pools.listIterator();
       
    48         boolean found = false;
    44         while (iter.hasNext()) {
    49         while (iter.hasNext()) {
    45             MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
    50             MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
       
    51             // only check heap pools that support usage threshold
       
    52             // this is typically only the old generation space
       
    53             // since the other spaces are expected to get filled up
    46             if (p.getType() == MemoryType.HEAP &&
    54             if (p.getType() == MemoryType.HEAP &&
    47                     p.isUsageThresholdSupported()) {
    55                 p.isUsageThresholdSupported())
    48                 mpool = p;
    56             {
    49                 System.out.println("Selected memory pool: ");
    57                 found = true;
    50                 MemoryUtil.printMemoryPool(mpool);
    58                 testPool(p);
    51                 break;
       
    52             }
    59             }
    53         }
    60         }
    54         if (mpool == null) {
    61         if (!found) {
    55             throw new RuntimeException("No heap pool found with threshold != -1");
    62             throw new RuntimeException("No heap pool found");
    56         }
    63         }
       
    64     }
       
    65 
       
    66     private static void testPool(MemoryPoolMXBean mpool) {
       
    67         System.out.println("Selected memory pool: ");
       
    68         MemoryUtil.printMemoryPool(mpool);
    57 
    69 
    58         MemoryUsage usage0 = mpool.getUsage();
    70         MemoryUsage usage0 = mpool.getUsage();
    59         MemoryUsage peak0 = mpool.getPeakUsage();
    71         MemoryUsage peak0 = mpool.getPeakUsage();
    60         final long largeArraySize = (usage0.getMax() - usage0.getUsed()) / 10;
       
    61 
    72 
    62         System.out.println("Before big object is allocated: ");
    73         // use a size that is larger than the young generation and G1 regions
    63         printMemoryUsage();
    74         // to force the array into the old gen
       
    75         int largeArraySize = 9 * 1000 * 1000;
    64 
    76 
    65         // Allocate a big array - need to allocate from the old gen
    77         System.out.println("Before big object array (of size "+largeArraySize+") is allocated: ");
    66         Object[][][] obj = new Object[1][1][(int) largeArraySize];
    78         printMemoryUsage(usage0, peak0);
    67 
    79 
    68         System.out.println("After the object is allocated: ");
    80         obj = new Object[largeArraySize];
    69         printMemoryUsage();
       
    70 
    81 
    71         MemoryUsage usage1 = mpool.getUsage();
    82         MemoryUsage usage1 = mpool.getUsage();
    72         MemoryUsage peak1 = mpool.getPeakUsage();
    83         MemoryUsage peak1 = mpool.getPeakUsage();
       
    84         System.out.println("After the object is allocated: ");
       
    85         printMemoryUsage(usage1, peak1);
    73 
    86 
    74         if (usage1.getUsed() <= usage0.getUsed()) {
    87         if (usage1.getUsed() <= usage0.getUsed()) {
    75             throw new RuntimeException(
    88             throw new RuntimeException(
    76                 formatSize("Before allocation: used", usage0.getUsed()) +
    89                 formatSize("Before allocation: used", usage0.getUsed()) +
    77                 " expected to be > " +
    90                 " expected to be < " +
    78                 formatSize("After allocation: used", usage1.getUsed()));
    91                 formatSize("After allocation: used", usage1.getUsed()));
    79         }
    92         }
    80 
    93 
    81         if (peak1.getUsed() <= peak0.getUsed()) {
    94         if (peak1.getUsed() <= peak0.getUsed()) {
    82             throw new RuntimeException(
    95             throw new RuntimeException(
    83                 formatSize("Before allocation: peak", peak0.getUsed()) +
    96                 formatSize("Before allocation: peak", peak0.getUsed()) +
    84                 " expected to be > " +
    97                 " expected to be < " +
    85                 formatSize("After allocation: peak", peak1.getUsed()));
    98                 formatSize("After allocation: peak", peak1.getUsed()));
    86         }
    99         }
    87 
   100 
    88 
   101 
    89         // The object is now garbage and do a GC
   102         // The object is now garbage and do a GC
    90         // memory usage should drop
   103         // memory usage should drop
    91         obj = null;
   104         obj = null;
    92         mbean.gc();
   105         mbean.gc();
    93 
   106 
    94         System.out.println("After GC: ");
       
    95         printMemoryUsage();
       
    96 
       
    97         MemoryUsage usage2 = mpool.getUsage();
   107         MemoryUsage usage2 = mpool.getUsage();
    98         MemoryUsage peak2 = mpool.getPeakUsage();
   108         MemoryUsage peak2 = mpool.getPeakUsage();
       
   109         System.out.println("After GC: ");
       
   110         printMemoryUsage(usage2, peak2);
    99 
   111 
   100         if (usage2.getUsed() >= usage1.getUsed()) {
   112         if (usage2.getUsed() >= usage1.getUsed()) {
   101             throw new RuntimeException(
   113             throw new RuntimeException(
   102                 formatSize("Before GC: used", usage1.getUsed()) + " " +
   114                 formatSize("Before GC: used", usage1.getUsed()) + " " +
   103                 " expected to be > " +
   115                 " expected to be > " +
   104                 formatSize("After GC: used", usage2.getUsed()));
   116                 formatSize("After GC: used", usage2.getUsed()));
   105         }
   117         }
   106 
   118 
   107         if (peak2.getUsed() != peak1.getUsed()) {
       
   108             throw new RuntimeException(
       
   109                 formatSize("Before GC: peak", peak1.getUsed()) + " " +
       
   110                 " expected to be equal to " +
       
   111                 formatSize("After GC: peak", peak2.getUsed()));
       
   112         }
       
   113 
       
   114         mpool.resetPeakUsage();
   119         mpool.resetPeakUsage();
   115 
       
   116         System.out.println("After resetPeakUsage: ");
       
   117         printMemoryUsage();
       
   118 
   120 
   119         MemoryUsage usage3 = mpool.getUsage();
   121         MemoryUsage usage3 = mpool.getUsage();
   120         MemoryUsage peak3 = mpool.getPeakUsage();
   122         MemoryUsage peak3 = mpool.getPeakUsage();
       
   123         System.out.println("After resetPeakUsage: ");
       
   124         printMemoryUsage(usage3, peak3);
   121 
   125 
   122         if (peak3.getUsed() != usage3.getUsed()) {
   126         if (peak3.getUsed() != usage3.getUsed()) {
   123             throw new RuntimeException(
   127             throw new RuntimeException(
   124                 formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
   128                 formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
   125                 " expected to be equal to " +
   129                 " expected to be equal to " +
   135 
   139 
   136         System.out.println("Test passed.");
   140         System.out.println("Test passed.");
   137     }
   141     }
   138 
   142 
   139     private static String INDENT = "    ";
   143     private static String INDENT = "    ";
   140     private static void printMemoryUsage() {
   144     private static void printMemoryUsage(MemoryUsage current, MemoryUsage peak) {
   141         MemoryUsage current = mpool.getUsage();
       
   142         MemoryUsage peak = mpool.getPeakUsage();
       
   143         System.out.println("Current Usage: ");
   145         System.out.println("Current Usage: ");
   144         MemoryUtil.printMemoryUsage(current);
   146         MemoryUtil.printMemoryUsage(current);
   145         System.out.println("Peak Usage: ");
   147         System.out.println("Peak Usage: ");
   146         MemoryUtil.printMemoryUsage(peak);
   148         MemoryUtil.printMemoryUsage(peak);
   147 
   149