Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/memory/Nio/Nio.java
41159 views
1
/*
2
* Copyright (c) 2013, 2020, 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
26
* @modules java.base/jdk.internal.misc
27
* @key stress
28
*
29
* @summary converted from VM Testbase gc/memory/Nio.
30
* VM Testbase keywords: [gc, stress, stressopt, monitoring]
31
*
32
* @library /vmTestbase
33
* /test/lib
34
* @run main/othervm -XX:MaxDirectMemorySize=50M gc.memory.Nio.Nio
35
*/
36
37
package gc.memory.Nio;
38
39
import java.lang.management.ManagementFactory;
40
import java.nio.ByteBuffer;
41
import jdk.internal.misc.VM;
42
import com.sun.management.HotSpotDiagnosticMXBean;
43
import java.io.File;
44
import java.io.IOException;
45
46
/**
47
* Test that uses java.nio.ByteBuffer to allocate native memory.
48
* The test allocates all the memory available and checks that
49
* further attempts to allocate more more will fail.
50
* Test also checks that allocating native memory doesn't affect heap.
51
* Test also cheks that GC can find unused native memory.
52
*
53
* @summary Checks that nio.ByteBuffer allocates native memory and doesn't affect heap.
54
* @run main/othervm -XX:MaxDirectMemorySize=50M gc.memory.Nio.Nio
55
*/
56
public class Nio {
57
58
static final int MAX_SIZE = (int)VM.maxDirectMemory();
59
60
public static void main(String[] args) {
61
System.exit(new Nio().run() + 95 /*STATUS_BASE*/);
62
}
63
64
public Nio() {
65
}
66
67
public int run() {
68
// Step0: init
69
gc();
70
long usedHeap_0 = getUsedHeap();
71
long usedNonHeap_0 = getUsedNonHeap();
72
73
// Step1: allocate the all available direct memory
74
// no OOME, no heap memory should be used
75
System.out.println("Allocating all the direct memory: " + MAX_SIZE);
76
ByteBuffer bb;
77
try {
78
bb = ByteBuffer.allocateDirect((int)MAX_SIZE);
79
System.out.println("... success");
80
} catch (OutOfMemoryError oom) {
81
throw new Fault("Unexpected OOME during the first allocation " + oom);
82
}
83
long usedHeap_1 = getUsedHeap();
84
long usedNonHeap_1 = getUsedNonHeap();
85
checkHeapIsNotAffected(usedHeap_0, usedHeap_1, usedNonHeap_0, usedNonHeap_1);
86
// Step2: invoke GC, it shouldn't help.
87
System.out.println("Doing gc");
88
gc();
89
90
// Step3: allocate 1 byte in the direct memory
91
// OOM is expected
92
try {
93
System.out.println("Allocating 1 byte");
94
ByteBuffer.allocateDirect(1);
95
throw new Fault("No OOM, but we already allocated all the memory");
96
} catch (OutOfMemoryError oom) {
97
System.out.println("Expected OOM " + oom);
98
}
99
100
// Step4: read and write into allocated memory
101
double d0 = -3.1415;
102
float f0 = 41234.6f;
103
bb.putDouble(MAX_SIZE/2, d0);
104
bb.putFloat(MAX_SIZE - 17, f0);
105
double d1 = bb.getDouble(MAX_SIZE/2);
106
float f1 = bb.getFloat(MAX_SIZE - 17);
107
System.out.println("put: " + d0 + ", " + f0);
108
System.out.println("got: " + d1 + ", " + f1);
109
if (d0 != d1 || f0 != f1) {
110
throw new Fault("read/write to buffer check failed");
111
}
112
113
// Step5:
114
// clean the buffer, use gc, try to allocate again
115
// no OOM is expected
116
bb = null;
117
gc();
118
try {
119
System.out.println("Allocating 10 bytes");
120
ByteBuffer.allocateDirect(10);
121
} catch (OutOfMemoryError oom) {
122
throw new Fault("Nop, OOM is unexpected again: " + oom);
123
}
124
125
126
System.out.println("The long quest has done! Congratulations");
127
128
return 0;
129
}
130
131
132
public static void gc() {
133
System.gc();
134
try {
135
Thread.currentThread().sleep(200);
136
} catch (Exception ignore) {
137
}
138
}
139
140
/**
141
* @return the size of used heap
142
*/
143
public static long getUsedHeap() {
144
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
145
}
146
147
/**
148
* @return the size of used non heap
149
*/
150
public static long getUsedNonHeap() {
151
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed();
152
}
153
154
/**
155
* Check that heap and non-heap memory have NOT changed significantly.
156
* Throws a Fault if check failed.
157
*
158
* @param h_before used heap before
159
* @param h_after used heap after
160
* @param nh_before used non heap before
161
* @param nh_after used non heap after
162
*/
163
void checkHeapIsNotAffected(long h_before, long h_after, long nh_before, long nh_after) {
164
165
if (h_after - h_before > MAX_SIZE * 0.75) {
166
System.err.println("Used heap before: " + h_before);
167
System.err.println("Used heap after : " + h_after);
168
dumpHeap();
169
String failed = "Allocating direct memory should not eat the heap!"
170
+ " Heap dumped to heapDump.hprof file.";
171
throw new Fault(failed);
172
}
173
if (nh_after - nh_before > MAX_SIZE * 0.75) {
174
System.err.println("Used heap before: " + nh_before);
175
System.err.println("Used heap after : " + nh_after);
176
dumpHeap();
177
throw new Fault("Allocating direct memory should not eat non the heap!");
178
}
179
}
180
181
/**
182
* Try to make heap dump
183
*/
184
void dumpHeap() {
185
HotSpotDiagnosticMXBean mxBean = ManagementFactory
186
.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
187
try {
188
System.out.println("Try to dump heap to heapDump.hprof file..");
189
mxBean.dumpHeap("heapDump.hprof", false);
190
System.out.println("Done");
191
} catch (IOException e) {
192
System.out.println("Failed to dump heap");
193
e.printStackTrace();
194
}
195
}
196
197
/**
198
* RuntimeException signaling a test failure.
199
*/
200
public static class Fault extends RuntimeException {
201
public Fault(String message) {
202
super(message);
203
}
204
}
205
206
}
207
208