Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/g1/TestHumongousShrinkHeap.java
32284 views
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 8056043
27
* @requires vm.gc=="G1" | vm.gc=="null"
28
* @summary Verify that heap shrinks after GC in the presence of fragmentation
29
* due to humongous objects
30
* @library /testlibrary
31
* @run main/othervm -XX:-ExplicitGCInvokesConcurrent -XX:MinHeapFreeRatio=10
32
* -XX:MaxHeapFreeRatio=12 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc
33
* TestHumongousShrinkHeap
34
*/
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
43
public class TestHumongousShrinkHeap {
44
45
public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
46
public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
47
48
private static final List<List<byte[]>> garbage = new ArrayList();
49
private static final int REGION_SIZE = 1024 * 1024; // 1M
50
private static final int LISTS_COUNT = 10;
51
private static final int HUMON_SIZE = Math.round(.9f * REGION_SIZE);
52
53
private static final long TOTAL_MEMORY = Runtime.getRuntime().totalMemory();
54
private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory();
55
56
private static final int HUMON_COUNT = (int) ((TOTAL_MEMORY / HUMON_SIZE) / LISTS_COUNT);
57
58
public static void main(String[] args) {
59
if (HUMON_COUNT == 0) {
60
System.out.println("Skipped. Heap is too small");
61
return;
62
}
63
64
if (TOTAL_MEMORY + REGION_SIZE * HUMON_COUNT > MAX_MEMORY) {
65
System.out.println("Skipped. Initial heap size is to close to max heap size.");
66
return;
67
}
68
69
System.out.format("Running with %s initial heap size of %s maximum heap size. "
70
+ "Will allocate humongous object of %s size %d times.%n",
71
MemoryUsagePrinter.humanReadableByteCount(TOTAL_MEMORY, false),
72
MemoryUsagePrinter.humanReadableByteCount(MAX_MEMORY, false),
73
MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false),
74
HUMON_COUNT
75
);
76
new TestHumongousShrinkHeap().test();
77
}
78
79
private final void test() {
80
System.gc();
81
MemoryUsagePrinter.printMemoryUsage("init");
82
83
allocate();
84
MemoryUsagePrinter.printMemoryUsage("allocated");
85
MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
86
87
free();
88
MemoryUsagePrinter.printMemoryUsage("free");
89
MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
90
91
assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format(
92
"committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
93
+ "%s = %s%n%s = %s",
94
MIN_FREE_RATIO_FLAG_NAME,
95
ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
96
MAX_FREE_RATIO_FLAG_NAME,
97
ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
98
));
99
}
100
101
private void allocate() {
102
103
for (int i = 0; i < LISTS_COUNT; i++) {
104
List<byte[]> stuff = new ArrayList();
105
allocateList(stuff, HUMON_COUNT, HUMON_SIZE);
106
MemoryUsagePrinter.printMemoryUsage("allocate #" + (i+1));
107
garbage.add(stuff);
108
}
109
}
110
111
private void free() {
112
// do not free last one list
113
garbage.subList(0, garbage.size() - 1).clear();
114
115
// do not free last one element from last list
116
List stuff = garbage.get(garbage.size() - 1);
117
stuff.subList(0, stuff.size() - 1).clear();
118
System.gc();
119
}
120
121
private static void allocateList(List garbage, int count, int size) {
122
for (int i = 0; i < count; i++) {
123
garbage.add(new byte[size]);
124
}
125
}
126
}
127
128
/**
129
* Prints memory usage to standard output
130
*/
131
class MemoryUsagePrinter {
132
133
public static String humanReadableByteCount(long bytes, boolean si) {
134
int unit = si ? 1000 : 1024;
135
if (bytes < unit) {
136
return bytes + " B";
137
}
138
int exp = (int) (Math.log(bytes) / Math.log(unit));
139
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
140
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
141
}
142
143
public static void printMemoryUsage(String label) {
144
MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
145
float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
146
System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
147
label,
148
humanReadableByteCount(memusage.getInit(), false),
149
humanReadableByteCount(memusage.getUsed(), false),
150
humanReadableByteCount(memusage.getCommitted(), false),
151
freeratio * 100
152
);
153
}
154
}
155
156