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

Print this page
rev 7875 : imported patch 8023101


  97         }
  98 
  99         if (peak1.getUsed() <= peak0.getUsed()) {
 100             throw new RuntimeException(
 101                 formatSize("Before allocation: peak", peak0.getUsed()) +
 102                 " expected to be < " +
 103                 formatSize("After allocation: peak", peak1.getUsed()));
 104         }
 105 
 106 
 107         // The object is now garbage and do a GC
 108         // memory usage should drop
 109         obj = null;
 110         mbean.gc();
 111 
 112         MemoryUsage usage2 = mpool.getUsage();
 113         MemoryUsage peak2 = mpool.getPeakUsage();
 114         System.out.println("After GC: ");
 115         printMemoryUsage(usage2, peak2);
 116 
 117         if (usage2.getUsed() >= usage1.getUsed()) {
 118             throw new RuntimeException(
 119                 formatSize("Before GC: used", usage1.getUsed()) + " " +
 120                 " expected to be > " +
 121                 formatSize("After GC: used", usage2.getUsed()));
 122         }
 123 
 124         mpool.resetPeakUsage();
 125 
 126         MemoryUsage usage3 = mpool.getUsage();
 127         MemoryUsage peak3 = mpool.getPeakUsage();
 128         System.out.println("After resetPeakUsage: ");
 129         printMemoryUsage(usage3, peak3);
 130 
 131         if (peak3.getUsed() != usage3.getUsed()) {
 132             throw new RuntimeException(
 133                 formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
 134                 " expected to be equal to " +
 135                 formatSize("current used", usage3.getUsed()));
 136         }
 137 
 138         if (peak3.getUsed() >= peak2.getUsed()) {
 139             throw new RuntimeException(
 140                 formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
 141                 " expected to be < " +
 142                 formatSize("previous peak", peak2.getUsed()));
 143         }
 144 
 145         System.out.println("Test passed.");
 146     }
 147 
 148     private static String INDENT = "    ";
 149     private static void printMemoryUsage(MemoryUsage current, MemoryUsage peak) {
 150         System.out.println("Current Usage: ");
 151         MemoryUtil.printMemoryUsage(current);
 152         System.out.println("Peak Usage: ");
 153         MemoryUtil.printMemoryUsage(peak);
 154 
 155     }
 156     private static String formatSize(String name, long value) {
 157         StringBuffer buf = new StringBuffer(name + " = " + value);
 158         if (value > 0) {
 159             buf.append(" (" + (value >> 10) + "K)");
 160         }
 161         return buf.toString();


  97         }
  98 
  99         if (peak1.getUsed() <= peak0.getUsed()) {
 100             throw new RuntimeException(
 101                 formatSize("Before allocation: peak", peak0.getUsed()) +
 102                 " expected to be < " +
 103                 formatSize("After allocation: peak", peak1.getUsed()));
 104         }
 105 
 106 
 107         // The object is now garbage and do a GC
 108         // memory usage should drop
 109         obj = null;
 110         mbean.gc();
 111 
 112         MemoryUsage usage2 = mpool.getUsage();
 113         MemoryUsage peak2 = mpool.getPeakUsage();
 114         System.out.println("After GC: ");
 115         printMemoryUsage(usage2, peak2);
 116 
 117         if (usage2.getUsed() > usage1.getUsed()) {
 118             throw new RuntimeException(
 119                 formatSize("Before GC: used", usage1.getUsed()) + " " +
 120                 " expected to be >= " +
 121                 formatSize("After GC: used", usage2.getUsed()));
 122         }
 123 
 124         mpool.resetPeakUsage();
 125 
 126         MemoryUsage usage3 = mpool.getUsage();
 127         MemoryUsage peak3 = mpool.getPeakUsage();
 128         System.out.println("After resetPeakUsage: ");
 129         printMemoryUsage(usage3, peak3);
 130 
 131         if (peak3.getUsed() != usage3.getUsed()) {
 132             throw new RuntimeException(
 133                 formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
 134                 " expected to be equal to " +
 135                 formatSize("current used", usage3.getUsed()));
 136         }
 137 
 138         if (peak3.getUsed() > peak2.getUsed()) {
 139             throw new RuntimeException(
 140                 formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
 141                 " expected to be <= " +
 142                 formatSize("previous peak", peak2.getUsed()));
 143         }
 144 
 145         System.out.println("Test passed.");
 146     }
 147 
 148     private static String INDENT = "    ";
 149     private static void printMemoryUsage(MemoryUsage current, MemoryUsage peak) {
 150         System.out.println("Current Usage: ");
 151         MemoryUtil.printMemoryUsage(current);
 152         System.out.println("Peak Usage: ");
 153         MemoryUtil.printMemoryUsage(peak);
 154 
 155     }
 156     private static String formatSize(String name, long value) {
 157         StringBuffer buf = new StringBuffer(name + " = " + value);
 158         if (value > 0) {
 159             buf.append(" (" + (value >> 10) + "K)");
 160         }
 161         return buf.toString();