1 /*
   2  * Copyright (c) 2000, 2008, 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  * @build UnexportLeak
  32  * @build UnexportLeak_Stub
  33  * @build Ping
  34  * @run main/othervm UnexportLeak
  35  */
  36 
  37 import java.lang.ref.*;
  38 import java.rmi.*;
  39 import java.rmi.server.*;
  40 import java.rmi.registry.*;
  41 
  42 public class UnexportLeak implements Ping {
  43 
  44     private static int PORT = 2006;
  45 
  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             LocateRegistry.createRegistry(PORT);
  53             Remote obj = new UnexportLeak();
  54             WeakReference wr = new WeakReference(obj);
  55             UnicastRemoteObject.exportObject(obj);
  56             LocateRegistry.getRegistry(PORT).rebind("UnexportLeak", obj);
  57             UnicastRemoteObject.unexportObject(obj, true);
  58             obj = null;
  59             flushRefs();
  60             if (wr.get() != null) {
  61                 System.err.println("FAILED: unexported object not collected");
  62                 throw new RuntimeException(
  63                     "FAILED: unexported object not collected");
  64             } else {
  65                 System.err.println("PASSED: unexported object collected");
  66             }
  67         } catch (RemoteException e) {
  68             System.err.println(
  69                 "FAILED: RemoteException encountered: " + e.getMessage());
  70             e.printStackTrace();
  71             throw new RuntimeException("FAILED: RemoteException encountered");
  72         }
  73     }
  74 
  75     /**
  76      * Force desparate garbage collection so that all WeakReference instances
  77      * will be cleared.
  78      */
  79     private static void flushRefs() {
  80         java.util.Vector chain = new java.util.Vector();
  81         try {
  82             while (true) {
  83                 int[] hungry = new int[65536];
  84                 chain.addElement(hungry);
  85             }
  86         } catch (OutOfMemoryError e) {
  87         }
  88     }
  89 }