comparison test/gc/g1/TestShrinkDefragmentedHeap.java @ 20460:df66e3a3c4c2

8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap Summary: New test added Reviewed-by: jwilhelm, tschatzl Contributed-by: andrey.x.zakharov@oracle.com
author jwilhelm
date Thu, 11 Sep 2014 14:21:13 +0200
parents
children 7a6313074325
comparison
equal deleted inserted replaced
20459:a98dd542cd25 20460:df66e3a3c4c2
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 TestShrinkDefragmentedHeap
26 * @bug 8038423
27 * @summary Verify that heap shrinks after GC in the presence of fragmentation due to humongous objects
28 * 1. allocate small objects mixed with humongous ones
29 * "ssssHssssHssssHssssHssssHssssHssssH"
30 * 2. release all allocated object except the last humongous one
31 * "..................................H"
32 * 3. invoke gc and check that memory returned to the system (amount of committed memory got down)
33 *
34 * @library /testlibrary
35 */
36 import java.lang.management.ManagementFactory;
37 import java.lang.management.MemoryUsage;
38 import java.util.ArrayList;
39 import java.util.List;
40 import sun.management.ManagementFactoryHelper;
41 import static com.oracle.java.testlibrary.Asserts.*;
42 import com.oracle.java.testlibrary.ProcessTools;
43 import com.oracle.java.testlibrary.OutputAnalyzer;
44
45 public class TestShrinkDefragmentedHeap {
46 // Since we store all the small objects, they become old and old regions are also allocated at the bottom of the heap
47 // together with humongous regions. So if there are a lot of old regions in the lower part of the heap,
48 // the humongous regions will be allocated in the upper part of the heap anyway.
49 // To avoid this the Eden needs to be big enough to fit all the small objects.
50 private static final int INITIAL_HEAP_SIZE = 200 * 1024 * 1024;
51 private static final int MINIMAL_YOUNG_SIZE = 190 * 1024 * 1024;
52 private static final int REGION_SIZE = 1 * 1024 * 1024;
53
54 public static void main(String[] args) throws Exception, Throwable {
55 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
56 "-XX:InitialHeapSize=" + INITIAL_HEAP_SIZE,
57 "-Xmn" + MINIMAL_YOUNG_SIZE,
58 "-XX:MinHeapFreeRatio=10",
59 "-XX:MaxHeapFreeRatio=11",
60 "-XX:+UseG1GC",
61 "-XX:G1HeapRegionSize=" + REGION_SIZE,
62 "-verbose:gc",
63 GCTest.class.getName()
64 );
65
66 OutputAnalyzer output = ProcessTools.executeProcess(pb);
67 output.shouldHaveExitValue(0);
68 }
69
70 static class GCTest {
71
72 private static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
73 private static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
74 private static final String NEW_SIZE_FLAG_NAME = "NewSize";
75
76 private static final ArrayList<ArrayList<byte[]>> garbage = new ArrayList<>();
77
78 private static final int SMALL_OBJS_SIZE = 10 * 1024; // 10kB
79 private static final int SMALL_OBJS_COUNT = MINIMAL_YOUNG_SIZE / (SMALL_OBJS_SIZE-1);
80 private static final int ALLOCATE_COUNT = 3;
81 // try to put all humongous object into gap between min young size and initial heap size
82 // to avoid implicit GCs
83 private static final int HUMONG_OBJS_SIZE = (int) Math.max(
84 (INITIAL_HEAP_SIZE - MINIMAL_YOUNG_SIZE) / ALLOCATE_COUNT / 4,
85 REGION_SIZE * 1.1
86 );
87
88 private static final long initialHeapSize = getHeapMemoryUsage().getUsed();
89
90 public static void main(String[] args) throws InterruptedException {
91 new GCTest().test();
92 }
93
94 private void test() throws InterruptedException {
95 MemoryUsagePrinter.printMemoryUsage("init");
96
97 allocate();
98 System.gc();
99 MemoryUsage muFull = getHeapMemoryUsage();
100 MemoryUsagePrinter.printMemoryUsage("allocated");
101
102 free();
103 //Thread.sleep(1000); // sleep before measures due lags in JMX
104 MemoryUsage muFree = getHeapMemoryUsage();
105 MemoryUsagePrinter.printMemoryUsage("free");
106
107 assertLessThan(muFree.getCommitted(), muFull.getCommitted(), prepareMessageCommittedIsNotLess() );
108 }
109
110 private void allocate() {
111 System.out.format("Will allocate objects of small size = %s and humongous size = %s",
112 MemoryUsagePrinter.humanReadableByteCount(SMALL_OBJS_SIZE, false),
113 MemoryUsagePrinter.humanReadableByteCount(HUMONG_OBJS_SIZE, false)
114 );
115
116 for (int i = 0; i < ALLOCATE_COUNT; i++) {
117 ArrayList<byte[]> stuff = new ArrayList<>();
118 allocateList(stuff, SMALL_OBJS_COUNT / ALLOCATE_COUNT, SMALL_OBJS_SIZE);
119 garbage.add(stuff);
120
121 ArrayList<byte[]> humongousStuff = new ArrayList<>();
122 allocateList(humongousStuff, 4, HUMONG_OBJS_SIZE);
123 garbage.add(humongousStuff);
124 }
125 }
126
127 private void free() {
128 // do not free last one list
129 garbage.subList(0, garbage.size() - 1).clear();
130
131 // do not free last one element from last list
132 ArrayList stuff = garbage.get(garbage.size() - 1);
133 if (stuff.size() > 1) {
134 stuff.subList(0, stuff.size() - 1).clear();
135 }
136 System.gc();
137 }
138
139 private String prepareMessageCommittedIsNotLess() {
140 return String.format(
141 "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
142 + "%s = %s%n%s = %s",
143 MIN_FREE_RATIO_FLAG_NAME,
144 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
145 MAX_FREE_RATIO_FLAG_NAME,
146 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
147 );
148 }
149
150 private static void allocateList(List garbage, int count, int size) {
151 for (int i = 0; i < count; i++) {
152 garbage.add(new byte[size]);
153 }
154 }
155 }
156
157 static MemoryUsage getHeapMemoryUsage() {
158 return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
159 }
160
161 /**
162 * Prints memory usage to standard output
163 */
164 static class MemoryUsagePrinter {
165
166 public static String humanReadableByteCount(long bytes, boolean si) {
167 int unit = si ? 1000 : 1024;
168 if (bytes < unit) {
169 return bytes + " B";
170 }
171 int exp = (int) (Math.log(bytes) / Math.log(unit));
172 String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
173 return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
174 }
175
176 public static void printMemoryUsage(String label) {
177 MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
178 float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
179 System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
180 label,
181 humanReadableByteCount(memusage.getInit(), false),
182 humanReadableByteCount(memusage.getUsed(), false),
183 humanReadableByteCount(memusage.getCommitted(), false),
184 freeratio * 100
185 );
186 }
187 }
188 }