Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/AsynchronousSocketChannel/Leaky.java
41153 views
1
/*
2
* Copyright (c) 2008, 2012, 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
/* @test
25
* @bug 4607272 6999915 7185340
26
* @summary Unit test for AsynchronousSocketChannel
27
* @modules java.management
28
* @run main/othervm -XX:+DisableExplicitGC -XX:MaxDirectMemorySize=75m Leaky
29
*/
30
31
import java.nio.ByteBuffer;
32
import java.nio.channels.*;
33
import java.net.*;
34
import java.util.List;
35
import java.util.concurrent.Future;
36
import java.util.concurrent.ThreadFactory;
37
import java.lang.management.BufferPoolMXBean;
38
import java.lang.management.ManagementFactory;
39
40
/**
41
* Heap buffers must be substituted with direct buffers when doing I/O. This
42
* test creates a scenario on Windows that challenges the per-thread buffer
43
* cache and quickly leads to an OutOfMemoryError if temporary buffers are
44
* not returned to the native heap.
45
*/
46
47
public class Leaky {
48
49
static final int K = 1024;
50
51
static class Connection {
52
private final AsynchronousSocketChannel client;
53
private final SocketChannel peer;
54
private final ByteBuffer dst;
55
private Future<Integer> readResult;
56
57
Connection(AsynchronousChannelGroup group) throws Exception {
58
ServerSocketChannel ssc =
59
ServerSocketChannel.open().bind(new InetSocketAddress(0));
60
InetAddress lh = InetAddress.getLocalHost();
61
int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();
62
SocketAddress remote = new InetSocketAddress(lh, port);
63
client = AsynchronousSocketChannel.open(group);
64
client.connect(remote).get();
65
peer = ssc.accept();
66
ssc.close();
67
dst = ByteBuffer.allocate(K*K);
68
}
69
70
void startRead() {
71
dst.clear();
72
readResult = client.read(dst);
73
}
74
75
void write() throws Exception {
76
peer.write(ByteBuffer.wrap("X".getBytes()));
77
}
78
79
void finishRead() throws Exception {
80
readResult.get();
81
}
82
}
83
84
public static void main(String[] args) throws Exception {
85
ThreadFactory threadFactory = new ThreadFactory() {
86
@Override
87
public Thread newThread(Runnable r) {
88
Thread t = new Thread(r);
89
t.setDaemon(true);
90
return t;
91
}
92
};
93
AsynchronousChannelGroup group =
94
AsynchronousChannelGroup.withFixedThreadPool(4, threadFactory);
95
96
final int CONNECTION_COUNT = 10;
97
Connection[] connections = new Connection[CONNECTION_COUNT];
98
for (int i=0; i<CONNECTION_COUNT; i++) {
99
connections[i] = new Connection(group);
100
}
101
102
for (int i=0; i<1024; i++) {
103
// initiate reads
104
for (Connection conn: connections) {
105
conn.startRead();
106
}
107
108
// write data so that the read can complete
109
for (Connection conn: connections) {
110
conn.write();
111
}
112
113
// complete read
114
for (Connection conn: connections) {
115
conn.finishRead();
116
}
117
}
118
119
// print summary of buffer pool usage
120
List<BufferPoolMXBean> pools =
121
ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
122
for (BufferPoolMXBean pool: pools)
123
System.out.format(" %8s ", pool.getName());
124
System.out.println();
125
for (int i=0; i<pools.size(); i++)
126
System.out.format("%6s %10s %10s ", "Count", "Capacity", "Memory");
127
System.out.println();
128
for (BufferPoolMXBean pool: pools) {
129
System.out.format("%6d %10d %10d ",
130
pool.getCount(), pool.getTotalCapacity(), pool.getMemoryUsed());
131
}
132
System.out.println();
133
}
134
}
135
136