39 public class TestMemoryMXBeans {
40
41 static volatile Object sink;
42
43 public static void main(String[] args) throws Exception {
44 if (args.length < 2) {
45 throw new IllegalStateException("Should provide expected heap sizes");
46 }
47
48 long initSize = 1L * Integer.parseInt(args[0]) * 1024 * 1024;
49 long maxSize = 1L * Integer.parseInt(args[1]) * 1024 * 1024;
50
51 testMemoryBean(initSize, maxSize);
52 testAllocs();
53 }
54
55 public static void testMemoryBean(long initSize, long maxSize) {
56 MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
57 long heapInit = memoryMXBean.getHeapMemoryUsage().getInit();
58 long heapMax = memoryMXBean.getHeapMemoryUsage().getMax();
59 long nonHeapInit = memoryMXBean.getNonHeapMemoryUsage().getInit();
60 long nonHeapMax = memoryMXBean.getNonHeapMemoryUsage().getMax();
61
62 if (initSize > 0 && heapInit != initSize) {
63 throw new IllegalStateException("Init heap size is wrong: " + heapInit + " vs " + initSize);
64 }
65 if (maxSize > 0 && heapMax != maxSize) {
66 throw new IllegalStateException("Max heap size is wrong: " + heapMax + " vs " + maxSize);
67 }
68 }
69
70 public static void testAllocs() throws Exception {
71 MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
72
73 // Try multiple times, to capture either APIs we call allocate lazily, or the background threads allocating
74 int maxTries = 10;
75 int tries = 0;
76
77 while (true) {
78 // Compute how much we waste during the calls themselves:
79 long heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed();
|
39 public class TestMemoryMXBeans {
40
41 static volatile Object sink;
42
43 public static void main(String[] args) throws Exception {
44 if (args.length < 2) {
45 throw new IllegalStateException("Should provide expected heap sizes");
46 }
47
48 long initSize = 1L * Integer.parseInt(args[0]) * 1024 * 1024;
49 long maxSize = 1L * Integer.parseInt(args[1]) * 1024 * 1024;
50
51 testMemoryBean(initSize, maxSize);
52 testAllocs();
53 }
54
55 public static void testMemoryBean(long initSize, long maxSize) {
56 MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
57 long heapInit = memoryMXBean.getHeapMemoryUsage().getInit();
58 long heapMax = memoryMXBean.getHeapMemoryUsage().getMax();
59 memoryMXBean.getNonHeapMemoryUsage().getInit(); // value not used
60 memoryMXBean.getNonHeapMemoryUsage().getMax(); // value not used
61
62 if (initSize > 0 && heapInit != initSize) {
63 throw new IllegalStateException("Init heap size is wrong: " + heapInit + " vs " + initSize);
64 }
65 if (maxSize > 0 && heapMax != maxSize) {
66 throw new IllegalStateException("Max heap size is wrong: " + heapMax + " vs " + maxSize);
67 }
68 }
69
70 public static void testAllocs() throws Exception {
71 MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
72
73 // Try multiple times, to capture either APIs we call allocate lazily, or the background threads allocating
74 int maxTries = 10;
75 int tries = 0;
76
77 while (true) {
78 // Compute how much we waste during the calls themselves:
79 long heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed();
|