1 /* 2 * Copyright (c) 2017, 2018, 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 8147615 26 * @summary Test whether an unreferenced FileChannel is actually cleaned 27 * @requires (os.family == "linux") | (os.family == "mac") | (os.family == "solaris") | (os.family == "aix") 28 * @library /test/lib 29 * @build jdk.test.lib.util.FileUtils CleanerTest 30 * @modules java.management java.base/sun.nio.ch:+open 31 * @run main/othervm CleanerTest 32 */ 33 34 import com.sun.management.UnixOperatingSystemMXBean; 35 import java.lang.management.ManagementFactory; 36 import java.lang.management.OperatingSystemMXBean; 37 import java.lang.ref.PhantomReference; 38 import java.lang.ref.Reference; 39 import java.lang.ref.ReferenceQueue; 40 import java.lang.ref.PhantomReference; 41 import java.lang.ref.WeakReference; 42 import java.lang.reflect.Field; 43 import java.nio.channels.FileChannel; 44 import java.nio.file.Files; 45 import java.nio.file.Path; 46 import java.nio.file.Paths; 47 import java.nio.file.StandardOpenOption; 48 import java.util.HashSet; 49 50 import jdk.test.lib.util.FileUtils; 51 52 import sun.nio.ch.FileChannelImpl; 53 54 public class CleanerTest { 55 public static void main(String[] args) throws Throwable { 56 OperatingSystemMXBean mxBean = 57 ManagementFactory.getOperatingSystemMXBean(); 58 UnixOperatingSystemMXBean unixMxBean = null; 59 if (mxBean instanceof UnixOperatingSystemMXBean) { 60 unixMxBean = (UnixOperatingSystemMXBean)mxBean; 61 } else { 62 System.out.println("Non-Unix system: skipping test."); 63 return; 64 } 65 66 FileUtils.listFileDescriptors(System.out); 67 long fdCount0 = unixMxBean.getOpenFileDescriptorCount(); 68 69 Path path = Paths.get(System.getProperty("test.dir", "."), "junk"); 70 try { 71 FileChannel fc = FileChannel.open(path, StandardOpenOption.CREATE, 72 StandardOpenOption.READ, StandardOpenOption.WRITE); 73 74 // Prepare to wait for Channel, FD and Cleaner to be reclaimed 75 ReferenceQueue<Object> refQueue = new ReferenceQueue<>(); 76 HashSet<Reference<?>> pending = new HashSet<>(); 77 78 Reference<Object> fcRef = new PhantomReference<>(fc, refQueue); 79 pending.add(fcRef); 80 81 Field fdField = FileChannelImpl.class.getDeclaredField("fd"); 82 fdField.setAccessible(true); 83 Object fd = fdField.get(fc); // get the fd from the channel 84 WeakReference<Object> fdWeak = new WeakReference<>(fd, refQueue); 85 pending.add(fdWeak); 86 87 Field closerField = FileChannelImpl.class.getDeclaredField("closer"); 88 closerField.setAccessible(true); 89 Object closer = closerField.get(fc); 90 System.out.printf(" cleanup: %s, fd: %s, cf: %s%n", fc, fd, closer); 91 92 if (closer != null) { 93 WeakReference<Object> closerWeak = new WeakReference<>(closer, refQueue); 94 pending.add(closerWeak); 95 System.out.printf(" closerWeak: %s%n", closerWeak); 96 } 97 98 // Wait for all of the objects being tracked to be reclaimed; 99 // The test will timeout if they are not reclaimed within the jtreg timeout 100 Reference<?> r; 101 while (((r = refQueue.remove(1000L)) != null) 102 || !pending.isEmpty()) { 103 System.out.printf(" r: %s, pending: %d%n", r, pending.size()); 104 if (r != null) { 105 pending.remove(r); 106 } else { 107 fc = null; 108 fd = null; 109 closer = null; 110 System.gc(); // attempt to reclaim them 111 } 112 } 113 114 Reference.reachabilityFence(fc); 115 Reference.reachabilityFence(fd); 116 Reference.reachabilityFence(closer); 117 118 long fdCount = unixMxBean.getOpenFileDescriptorCount(); 119 if (fdCount != fdCount0) { 120 // Add debugging info about file descriptor changes 121 System.out.printf("initial count of open file descriptors: %d%n", fdCount0); 122 System.out.printf("final count of open file descriptors: %d%n", fdCount); 123 FileUtils.listFileDescriptors(System.out); 124 } 125 } finally { 126 Files.delete(path); 127 } 128 } 129 }