1 /* 2 * Copyright (c) 2000, 2019, 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 4331349 26 * @summary synopsis: unexporting doesn't guarantee that DGC will 27 * let go of remote object 28 * 29 * @author Ann Wollrath 30 * 31 * @library ../../../testlibrary 32 * @modules java.rmi/sun.rmi.registry 33 * java.rmi/sun.rmi.server 34 * java.rmi/sun.rmi.transport 35 * java.rmi/sun.rmi.transport.tcp 36 * @build TestLibrary UnexportLeak_Stub Ping 37 * @run main/othervm UnexportLeak 38 */ 39 40 import java.lang.ref.*; 41 import java.rmi.*; 42 import java.rmi.server.*; 43 import java.rmi.registry.*; 44 45 public class UnexportLeak implements Ping { 46 public void ping() { 47 } 48 49 public static void main(String[] args) { 50 try { 51 System.err.println("\nRegression test for bug 4331349\n"); 52 Registry registry = TestLibrary.createRegistryOnEphemeralPort(); 53 int registryPort = TestLibrary.getRegistryPort(registry); 54 Remote obj = new UnexportLeak(); 55 WeakReference wr = new WeakReference(obj); 56 UnicastRemoteObject.exportObject(obj); 57 LocateRegistry.getRegistry(registryPort).rebind("UnexportLeak", obj); 58 UnicastRemoteObject.unexportObject(obj, true); 59 obj = null; 60 flushRefs(); 61 if (wr.get() != null) { 62 System.err.println("FAILED: unexported object not collected"); 63 throw new RuntimeException( 64 "FAILED: unexported object not collected"); 65 } else { 66 System.err.println("PASSED: unexported object collected"); 67 } 68 } catch (RemoteException e) { 69 System.err.println( 70 "FAILED: RemoteException encountered: " + e.getMessage()); 71 e.printStackTrace(); 72 throw new RuntimeException("FAILED: RemoteException encountered"); 73 } 74 } 75 76 /** 77 * Force desperate garbage collection so that all WeakReference instances 78 * will be cleared. 79 */ 80 private static void flushRefs() { 81 java.util.Vector chain = new java.util.Vector(); 82 try { 83 while (true) { 84 int[] hungry = new int[65536]; 85 chain.addElement(hungry); 86 } 87 } catch (OutOfMemoryError e) { 88 // An inflated Java monitor can keep 'obj' alive so request 89 // an explicit GC to make sure things are cleaned up. 90 System.gc(); 91 } 92 } 93 }