Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/AsynchronousSocketChannel/Leaky.java
38828 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* @run main/othervm -XX:+DisableExplicitGC -XX:MaxDirectMemorySize=75m Leaky27*/2829import java.nio.ByteBuffer;30import java.nio.channels.*;31import java.net.*;32import java.util.List;33import java.util.concurrent.Future;34import java.util.concurrent.ThreadFactory;35import java.lang.management.BufferPoolMXBean;36import java.lang.management.ManagementFactory;3738/**39* Heap buffers must be substituted with direct buffers when doing I/O. This40* test creates a scenario on Windows that challenges the per-thread buffer41* cache and quickly leads to an OutOfMemoryError if temporary buffers are42* not returned to the native heap.43*/4445public class Leaky {4647static final int K = 1024;4849static class Connection {50private final AsynchronousSocketChannel client;51private final SocketChannel peer;52private final ByteBuffer dst;53private Future<Integer> readResult;5455Connection(AsynchronousChannelGroup group) throws Exception {56ServerSocketChannel ssc =57ServerSocketChannel.open().bind(new InetSocketAddress(0));58InetAddress lh = InetAddress.getLocalHost();59int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();60SocketAddress remote = new InetSocketAddress(lh, port);61client = AsynchronousSocketChannel.open(group);62client.connect(remote).get();63peer = ssc.accept();64ssc.close();65dst = ByteBuffer.allocate(K*K);66}6768void startRead() {69dst.clear();70readResult = client.read(dst);71}7273void write() throws Exception {74peer.write(ByteBuffer.wrap("X".getBytes()));75}7677void finishRead() throws Exception {78readResult.get();79}80}8182public static void main(String[] args) throws Exception {83ThreadFactory threadFactory = new ThreadFactory() {84@Override85public Thread newThread(Runnable r) {86Thread t = new Thread(r);87t.setDaemon(true);88return t;89}90};91AsynchronousChannelGroup group =92AsynchronousChannelGroup.withFixedThreadPool(4, threadFactory);9394final int CONNECTION_COUNT = 10;95Connection[] connections = new Connection[CONNECTION_COUNT];96for (int i=0; i<CONNECTION_COUNT; i++) {97connections[i] = new Connection(group);98}99100for (int i=0; i<1024; i++) {101// initiate reads102for (Connection conn: connections) {103conn.startRead();104}105106// write data so that the read can complete107for (Connection conn: connections) {108conn.write();109}110111// complete read112for (Connection conn: connections) {113conn.finishRead();114}115}116117// print summary of buffer pool usage118List<BufferPoolMXBean> pools =119ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);120for (BufferPoolMXBean pool: pools)121System.out.format(" %8s ", pool.getName());122System.out.println();123for (int i=0; i<pools.size(); i++)124System.out.format("%6s %10s %10s ", "Count", "Capacity", "Memory");125System.out.println();126for (BufferPoolMXBean pool: pools) {127System.out.format("%6d %10d %10d ",128pool.getCount(), pool.getTotalCapacity(), pool.getMemoryUsed());129}130System.out.println();131}132}133134135