Path: blob/master/test/jdk/java/nio/channels/AsynchronousSocketChannel/Leaky.java
41153 views
/*1* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4607272 6999915 718534025* @summary Unit test for AsynchronousSocketChannel26* @modules java.management27* @run main/othervm -XX:+DisableExplicitGC -XX:MaxDirectMemorySize=75m Leaky28*/2930import java.nio.ByteBuffer;31import java.nio.channels.*;32import java.net.*;33import java.util.List;34import java.util.concurrent.Future;35import java.util.concurrent.ThreadFactory;36import java.lang.management.BufferPoolMXBean;37import java.lang.management.ManagementFactory;3839/**40* Heap buffers must be substituted with direct buffers when doing I/O. This41* test creates a scenario on Windows that challenges the per-thread buffer42* cache and quickly leads to an OutOfMemoryError if temporary buffers are43* not returned to the native heap.44*/4546public class Leaky {4748static final int K = 1024;4950static class Connection {51private final AsynchronousSocketChannel client;52private final SocketChannel peer;53private final ByteBuffer dst;54private Future<Integer> readResult;5556Connection(AsynchronousChannelGroup group) throws Exception {57ServerSocketChannel ssc =58ServerSocketChannel.open().bind(new InetSocketAddress(0));59InetAddress lh = InetAddress.getLocalHost();60int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();61SocketAddress remote = new InetSocketAddress(lh, port);62client = AsynchronousSocketChannel.open(group);63client.connect(remote).get();64peer = ssc.accept();65ssc.close();66dst = ByteBuffer.allocate(K*K);67}6869void startRead() {70dst.clear();71readResult = client.read(dst);72}7374void write() throws Exception {75peer.write(ByteBuffer.wrap("X".getBytes()));76}7778void finishRead() throws Exception {79readResult.get();80}81}8283public static void main(String[] args) throws Exception {84ThreadFactory threadFactory = new ThreadFactory() {85@Override86public Thread newThread(Runnable r) {87Thread t = new Thread(r);88t.setDaemon(true);89return t;90}91};92AsynchronousChannelGroup group =93AsynchronousChannelGroup.withFixedThreadPool(4, threadFactory);9495final int CONNECTION_COUNT = 10;96Connection[] connections = new Connection[CONNECTION_COUNT];97for (int i=0; i<CONNECTION_COUNT; i++) {98connections[i] = new Connection(group);99}100101for (int i=0; i<1024; i++) {102// initiate reads103for (Connection conn: connections) {104conn.startRead();105}106107// write data so that the read can complete108for (Connection conn: connections) {109conn.write();110}111112// complete read113for (Connection conn: connections) {114conn.finishRead();115}116}117118// print summary of buffer pool usage119List<BufferPoolMXBean> pools =120ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);121for (BufferPoolMXBean pool: pools)122System.out.format(" %8s ", pool.getName());123System.out.println();124for (int i=0; i<pools.size(); i++)125System.out.format("%6s %10s %10s ", "Count", "Capacity", "Memory");126System.out.println();127for (BufferPoolMXBean pool: pools) {128System.out.format("%6d %10d %10d ",129pool.getCount(), pool.getTotalCapacity(), pool.getMemoryUsed());130}131System.out.println();132}133}134135136