Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/FileChannel/TempDirectBuffersReclamation.java
41154 views
1
/*
2
* Copyright (c) 2018, 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
import java.io.IOException;
25
import java.io.UncheckedIOException;
26
import java.lang.management.BufferPoolMXBean;
27
import java.lang.management.ManagementFactory;
28
import java.nio.ByteBuffer;
29
import java.nio.channels.FileChannel;
30
import java.nio.charset.StandardCharsets;
31
import java.nio.file.Files;
32
import java.nio.file.Path;
33
34
import static java.nio.file.StandardOpenOption.CREATE;
35
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
36
import static java.nio.file.StandardOpenOption.WRITE;
37
38
/*
39
* @test
40
* @bug 8202788
41
* @summary Test reclamation of thread-local temporary direct byte buffers at thread exit
42
* @modules java.management
43
* @run main/othervm TempDirectBuffersReclamation
44
*/
45
public class TempDirectBuffersReclamation {
46
47
public static void main(String[] args) throws IOException {
48
49
BufferPoolMXBean dbPool = ManagementFactory
50
.getPlatformMXBeans(BufferPoolMXBean.class)
51
.stream()
52
.filter(bp -> bp.getName().equals("direct"))
53
.findFirst()
54
.orElseThrow(() -> new RuntimeException("Can't obtain direct BufferPoolMXBean"));
55
56
long count0 = dbPool.getCount();
57
long memoryUsed0 = dbPool.getMemoryUsed();
58
59
Thread thread = new Thread(TempDirectBuffersReclamation::doFileChannelWrite);
60
thread.start();
61
try {
62
thread.join();
63
} catch (InterruptedException e) {
64
throw new RuntimeException(e);
65
}
66
67
long count1 = dbPool.getCount();
68
long memoryUsed1 = dbPool.getMemoryUsed();
69
70
if (count0 != count1 || memoryUsed0 != memoryUsed1) {
71
throw new AssertionError(
72
"Direct BufferPool not same before thread activity and after thread exit.\n" +
73
"Before: # of buffers: " + count0 + ", memory used: " + memoryUsed0 + "\n" +
74
" After: # of buffers: " + count1 + ", memory used: " + memoryUsed1 + "\n"
75
);
76
}
77
}
78
79
static void doFileChannelWrite() {
80
try {
81
Path file = Files.createTempFile("test", ".tmp");
82
try (FileChannel fc = FileChannel.open(file, CREATE, WRITE, TRUNCATE_EXISTING)) {
83
fc.write(ByteBuffer.wrap("HELLO".getBytes(StandardCharsets.UTF_8)));
84
} finally {
85
Files.delete(file);
86
}
87
} catch (IOException e) {
88
throw new UncheckedIOException(e);
89
}
90
}
91
}
92
93