Mercurial > hg > truffle
comparison test/gc/g1/TestHumongousShrinkHeap.java @ 20381:09e9e5240710
8037925: CMM Testing: an allocated humongous object at the end of the heap should not prevents shrinking the heap
Summary: New test added.
Reviewed-by: ehelin, tschatzl, jwilhelm
Contributed-by: andrey.x.zakharov@oracle.com
author | jwilhelm |
---|---|
date | Wed, 03 Sep 2014 09:23:58 +0200 |
parents | |
children | b1266b08b994 |
comparison
equal
deleted
inserted
replaced
20380:9337d0e7ea4f | 20381:09e9e5240710 |
---|---|
1 /* | |
2 * Copyright (c) 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 | |
24 /** | |
25 * @test TestHumongousShrinkHeap | |
26 * @bug 8036025 | |
27 * @summary Verify that heap shrinks after GC in the presence of fragmentation due to humongous objects | |
28 * @library /testlibrary | |
29 * @run main/othervm -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=50 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc TestHumongousShrinkHeap | |
30 */ | |
31 | |
32 import java.lang.management.ManagementFactory; | |
33 import java.lang.management.MemoryUsage; | |
34 import java.util.ArrayList; | |
35 import java.util.List; | |
36 import sun.management.ManagementFactoryHelper; | |
37 import static com.oracle.java.testlibrary.Asserts.*; | |
38 | |
39 public class TestHumongousShrinkHeap { | |
40 | |
41 public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio"; | |
42 public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio"; | |
43 | |
44 private static final ArrayList<ArrayList<byte[]>> garbage = new ArrayList<>(); | |
45 private static final int PAGE_SIZE = 1024 * 1024; // 1M | |
46 private static final int PAGES_NUM = 5; | |
47 | |
48 | |
49 public static void main(String[] args) { | |
50 new TestHumongousShrinkHeap().test(); | |
51 } | |
52 | |
53 private final void test() { | |
54 System.gc(); | |
55 MemoryUsagePrinter.printMemoryUsage("init"); | |
56 | |
57 eat(); | |
58 MemoryUsagePrinter.printMemoryUsage("eaten"); | |
59 MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); | |
60 | |
61 free(); | |
62 MemoryUsagePrinter.printMemoryUsage("free"); | |
63 MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); | |
64 | |
65 assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format( | |
66 "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n" | |
67 + "%s = %s%n%s = %s", | |
68 MIN_FREE_RATIO_FLAG_NAME, | |
69 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(), | |
70 MAX_FREE_RATIO_FLAG_NAME, | |
71 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue() | |
72 )); | |
73 } | |
74 | |
75 private void eat() { | |
76 int HumongousObjectSize = Math.round(.9f * PAGE_SIZE); | |
77 System.out.println("Will allocate objects of size=" + | |
78 MemoryUsagePrinter.humanReadableByteCount(HumongousObjectSize, true)); | |
79 | |
80 for (int i = 0; i < PAGES_NUM; i++) { | |
81 ArrayList<byte[]> stuff = new ArrayList<>(); | |
82 eatList(stuff, 100, HumongousObjectSize); | |
83 MemoryUsagePrinter.printMemoryUsage("eat #" + i); | |
84 garbage.add(stuff); | |
85 } | |
86 } | |
87 | |
88 private void free() { | |
89 // do not free last one list | |
90 garbage.subList(0, garbage.size() - 1).clear(); | |
91 | |
92 // do not free last one element from last list | |
93 ArrayList stuff = garbage.get(garbage.size() - 1); | |
94 stuff.subList(0, stuff.size() - 1).clear(); | |
95 System.gc(); | |
96 } | |
97 | |
98 private static void eatList(List garbage, int count, int size) { | |
99 for (int i = 0; i < count; i++) { | |
100 garbage.add(new byte[size]); | |
101 } | |
102 } | |
103 } | |
104 | |
105 /** | |
106 * Prints memory usage to standard output | |
107 */ | |
108 class MemoryUsagePrinter { | |
109 | |
110 public static String humanReadableByteCount(long bytes, boolean si) { | |
111 int unit = si ? 1000 : 1024; | |
112 if (bytes < unit) { | |
113 return bytes + " B"; | |
114 } | |
115 int exp = (int) (Math.log(bytes) / Math.log(unit)); | |
116 String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i"); | |
117 return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); | |
118 } | |
119 | |
120 public static void printMemoryUsage(String label) { | |
121 MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); | |
122 float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted(); | |
123 System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n", | |
124 label, | |
125 humanReadableByteCount(memusage.getInit(), true), | |
126 humanReadableByteCount(memusage.getUsed(), true), | |
127 humanReadableByteCount(memusage.getCommitted(), true), | |
128 freeratio * 100 | |
129 ); | |
130 } | |
131 } |