blob: 66218e465b5e7c527fb1eff9210f42f9cf079757 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4892507
27 * @summary Basic Test for MemoryPool.resetPeakUsage()
28 * @author Mandy Chung
29 *
30 * @build ResetPeakMemoryUsage MemoryUtil
31 * @run main ResetPeakMemoryUsage
32 */
33
34import java.lang.management.*;
35import java.util.*;
36
37public class ResetPeakMemoryUsage {
38 private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
39 private static List pools = ManagementFactory.getMemoryPoolMXBeans();
40 private static MemoryPoolMXBean mpool = null;
41
42 public static void main(String[] argv) {
43 ListIterator iter = pools.listIterator();
44 while (iter.hasNext()) {
45 MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
46 if (p.getType() == MemoryType.HEAP &&
47 p.isUsageThresholdSupported()) {
48 mpool = p;
49 System.out.println("Selected memory pool: ");
50 MemoryUtil.printMemoryPool(mpool);
51 break;
52 }
53 }
54 if (mpool == null) {
55 throw new RuntimeException("No heap pool found with threshold != -1");
56 }
57
58 MemoryUsage usage0 = mpool.getUsage();
59 MemoryUsage peak0 = mpool.getPeakUsage();
60 final long largeArraySize = (usage0.getMax() - usage0.getUsed()) / 10;
61
62 System.out.println("Before big object is allocated: ");
63 printMemoryUsage();
64
65 // Allocate a big array - need to allocate from the old gen
66 Object[][][] obj = new Object[1][1][(int) largeArraySize];
67
68 System.out.println("After the object is allocated: ");
69 printMemoryUsage();
70
71 MemoryUsage usage1 = mpool.getUsage();
72 MemoryUsage peak1 = mpool.getPeakUsage();
73
74 if (usage1.getUsed() <= usage0.getUsed()) {
75 throw new RuntimeException(
76 formatSize("Before allocation: used", usage0.getUsed()) +
77 " expected to be > " +
78 formatSize("After allocation: used", usage1.getUsed()));
79 }
80
81 if (peak1.getUsed() <= peak0.getUsed()) {
82 throw new RuntimeException(
83 formatSize("Before allocation: peak", peak0.getUsed()) +
84 " expected to be > " +
85 formatSize("After allocation: peak", peak1.getUsed()));
86 }
87
88
89 // The object is now garbage and do a GC
90 // memory usage should drop
91 obj = null;
92 mbean.gc();
93
94 System.out.println("After GC: ");
95 printMemoryUsage();
96
97 MemoryUsage usage2 = mpool.getUsage();
98 MemoryUsage peak2 = mpool.getPeakUsage();
99
100 if (usage2.getUsed() >= usage1.getUsed()) {
101 throw new RuntimeException(
102 formatSize("Before GC: used", usage1.getUsed()) + " " +
103 " expected to be > " +
104 formatSize("After GC: used", usage2.getUsed()));
105 }
106
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();
115
116 System.out.println("After resetPeakUsage: ");
117 printMemoryUsage();
118
119 MemoryUsage usage3 = mpool.getUsage();
120 MemoryUsage peak3 = mpool.getPeakUsage();
121
122 if (peak3.getUsed() != usage3.getUsed()) {
123 throw new RuntimeException(
124 formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
125 " expected to be equal to " +
126 formatSize("current used", usage3.getUsed()));
127 }
128
129 if (peak3.getUsed() >= peak2.getUsed()) {
130 throw new RuntimeException(
131 formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
132 " expected to be < " +
133 formatSize("previous peak", peak2.getUsed()));
134 }
135
136 System.out.println("Test passed.");
137 }
138
139 private static String INDENT = " ";
140 private static void printMemoryUsage() {
141 MemoryUsage current = mpool.getUsage();
142 MemoryUsage peak = mpool.getPeakUsage();
143 System.out.println("Current Usage: ");
144 MemoryUtil.printMemoryUsage(current);
145 System.out.println("Peak Usage: ");
146 MemoryUtil.printMemoryUsage(peak);
147
148 }
149 private static String formatSize(String name, long value) {
150 StringBuffer buf = new StringBuffer(name + " = " + value);
151 if (value > 0) {
152 buf.append(" (" + (value >> 10) + "K)");
153 }
154 return buf.toString();
155 }
156}