Path: blob/master/test/jdk/java/nio/channels/FileChannel/TempDirectBuffersReclamation.java
41154 views
/*1* Copyright (c) 2018, 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*/2223import java.io.IOException;24import java.io.UncheckedIOException;25import java.lang.management.BufferPoolMXBean;26import java.lang.management.ManagementFactory;27import java.nio.ByteBuffer;28import java.nio.channels.FileChannel;29import java.nio.charset.StandardCharsets;30import java.nio.file.Files;31import java.nio.file.Path;3233import static java.nio.file.StandardOpenOption.CREATE;34import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;35import static java.nio.file.StandardOpenOption.WRITE;3637/*38* @test39* @bug 820278840* @summary Test reclamation of thread-local temporary direct byte buffers at thread exit41* @modules java.management42* @run main/othervm TempDirectBuffersReclamation43*/44public class TempDirectBuffersReclamation {4546public static void main(String[] args) throws IOException {4748BufferPoolMXBean dbPool = ManagementFactory49.getPlatformMXBeans(BufferPoolMXBean.class)50.stream()51.filter(bp -> bp.getName().equals("direct"))52.findFirst()53.orElseThrow(() -> new RuntimeException("Can't obtain direct BufferPoolMXBean"));5455long count0 = dbPool.getCount();56long memoryUsed0 = dbPool.getMemoryUsed();5758Thread thread = new Thread(TempDirectBuffersReclamation::doFileChannelWrite);59thread.start();60try {61thread.join();62} catch (InterruptedException e) {63throw new RuntimeException(e);64}6566long count1 = dbPool.getCount();67long memoryUsed1 = dbPool.getMemoryUsed();6869if (count0 != count1 || memoryUsed0 != memoryUsed1) {70throw new AssertionError(71"Direct BufferPool not same before thread activity and after thread exit.\n" +72"Before: # of buffers: " + count0 + ", memory used: " + memoryUsed0 + "\n" +73" After: # of buffers: " + count1 + ", memory used: " + memoryUsed1 + "\n"74);75}76}7778static void doFileChannelWrite() {79try {80Path file = Files.createTempFile("test", ".tmp");81try (FileChannel fc = FileChannel.open(file, CREATE, WRITE, TRUNCATE_EXISTING)) {82fc.write(ByteBuffer.wrap("HELLO".getBytes(StandardCharsets.UTF_8)));83} finally {84Files.delete(file);85}86} catch (IOException e) {87throw new UncheckedIOException(e);88}89}90}919293