752N/A/*
752N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
752N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
752N/A *
752N/A * This code is free software; you can redistribute it and/or modify it
752N/A * under the terms of the GNU General Public License version 2 only, as
752N/A * published by the Free Software Foundation.
752N/A *
752N/A * This code is distributed in the hope that it will be useful, but WITHOUT
752N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
752N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
752N/A * version 2 for more details (a copy is included in the LICENSE file that
752N/A * accompanied this code).
752N/A *
752N/A * You should have received a copy of the GNU General Public License version
752N/A * 2 along with this work; if not, write to the Free Software Foundation,
752N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
752N/A *
752N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
752N/A * or visit www.oracle.com if you need additional information or have any
752N/A * questions.
752N/A */
752N/A
752N/A/* @test
752N/A * @bug 4607272 6999915 7185340
752N/A * @summary Unit test for AsynchronousSocketChannel
752N/A * @run main/othervm -XX:+DisableExplicitGC -XX:MaxDirectMemorySize=75m Leaky
752N/A */
752N/A
752N/Aimport java.nio.ByteBuffer;
752N/Aimport java.nio.channels.*;
752N/Aimport java.net.*;
752N/Aimport java.util.List;
752N/Aimport java.util.concurrent.Future;
752N/Aimport java.util.concurrent.ThreadFactory;
752N/Aimport java.lang.management.BufferPoolMXBean;
752N/Aimport java.lang.management.ManagementFactory;
752N/A
752N/A/**
752N/A * Heap buffers must be substituted with direct buffers when doing I/O. This
752N/A * test creates a scenario on Windows that challenges the per-thread buffer
752N/A * cache and quickly leads to an OutOfMemoryError if temporary buffers are
752N/A * not returned to the native heap.
752N/A */
752N/A
752N/Apublic class Leaky {
752N/A
752N/A static final int K = 1024;
752N/A
752N/A static class Connection {
752N/A private final AsynchronousSocketChannel client;
752N/A private final SocketChannel peer;
752N/A private final ByteBuffer dst;
752N/A private Future<Integer> readResult;
752N/A
752N/A Connection(AsynchronousChannelGroup group) throws Exception {
752N/A ServerSocketChannel ssc =
752N/A ServerSocketChannel.open().bind(new InetSocketAddress(0));
752N/A InetAddress lh = InetAddress.getLocalHost();
752N/A int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();
752N/A SocketAddress remote = new InetSocketAddress(lh, port);
752N/A client = AsynchronousSocketChannel.open(group);
752N/A client.connect(remote).get();
752N/A peer = ssc.accept();
752N/A ssc.close();
752N/A dst = ByteBuffer.allocate(K*K);
752N/A }
void startRead() {
dst.clear();
readResult = client.read(dst);
}
void write() throws Exception {
peer.write(ByteBuffer.wrap("X".getBytes()));
}
void finishRead() throws Exception {
readResult.get();
}
}
public static void main(String[] args) throws Exception {
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
};
AsynchronousChannelGroup group =
AsynchronousChannelGroup.withFixedThreadPool(4, threadFactory);
final int CONNECTION_COUNT = 10;
Connection[] connections = new Connection[CONNECTION_COUNT];
for (int i=0; i<CONNECTION_COUNT; i++) {
connections[i] = new Connection(group);
}
for (int i=0; i<1024; i++) {
// initiate reads
for (Connection conn: connections) {
conn.startRead();
}
// write data so that the read can complete
for (Connection conn: connections) {
conn.write();
}
// complete read
for (Connection conn: connections) {
conn.finishRead();
}
}
// print summary of buffer pool usage
List<BufferPoolMXBean> pools =
ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
for (BufferPoolMXBean pool: pools)
System.out.format(" %8s ", pool.getName());
System.out.println();
for (int i=0; i<pools.size(); i++)
System.out.format("%6s %10s %10s ", "Count", "Capacity", "Memory");
System.out.println();
for (BufferPoolMXBean pool: pools) {
System.out.format("%6d %10d %10d ",
pool.getCount(), pool.getTotalCapacity(), pool.getMemoryUsed());
}
System.out.println();
}
}