1 /* 2 * Copyright (c) 1998, 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 4116437 26 * @summary Distributed Garbage Collector Memory Leak 27 * 28 * @author Laird Dornin 29 * 30 * @library ../../testlibrary 31 * @build CheckLeaseLeak CheckLeaseLeak_Stub LeaseLeakClient LeaseLeak 32 * @build TestLibrary 33 * @run main/othervm/timeout=240 CheckLeaseLeak 34 * 35 */ 36 37 /** 38 * A bug in sun.rmi.transport.DGCImp.checkLeases() results in memory 39 * leak of LeaseInfo objects. 40 * 41 * In order to verify that this problem no longer exists, we create a 42 * remote object and a serveral clients in different VMs. The clients 43 * call a remote method on an exported object. This will cause the rmi 44 * runtime to create several references (all with different vmids) to 45 * the remote object. Each vmid needs a seperate LeaseInfo object in 46 * the object table target DGCImpl.leaseTable. If the leak is fixed, 47 * the leaseTable field will contain no objects. We use reflection to 48 * find the number of objects contained in this table. 49 */ 50 51 import java.rmi.*; 52 import java.rmi.server.*; 53 import sun.rmi.transport.*; 54 import sun.rmi.*; 55 import java.util.Map; 56 import java.io.*; 57 import java.lang.reflect.*; 58 import java.rmi.registry.*; 59 60 public class CheckLeaseLeak extends UnicastRemoteObject implements LeaseLeak { 61 public CheckLeaseLeak() throws RemoteException { } 62 public void ping () throws RemoteException { } 63 64 /** 65 * Id to fake the DGC_ID, so we can later get a reference to the 66 * DGCImpl in the object table. 67 */ 68 private final static int DGC_ID = 2; 69 70 private final static int ITERATIONS = 10; 71 private final static int numberPingCalls = 0; 72 private final static int CHECK_INTERVAL = 400; 73 private final static int LEASE_VALUE = 20; 74 75 public static void main (String[] args) { 76 CheckLeaseLeak leakServer = null; 77 int numLeft =0; 78 79 /* 80 * we want DGC to collect leases *quickly* 81 * decrease the lease check interval 82 */ 83 TestLibrary.setInteger("sun.rmi.dgc.checkInterval", 84 CHECK_INTERVAL); 85 TestLibrary.setInteger("java.rmi.dgc.leaseValue", 86 LEASE_VALUE); 87 88 try { 89 Registry registry = 90 TestLibrary.createRegistryOnUnusedPort(); 91 int registryPort = TestLibrary.getRegistryPort(registry); 92 93 leakServer = new CheckLeaseLeak(); 94 registry.rebind("/LeaseLeak", leakServer); 95 96 /* create a bunch of clients in a *different* vm */ 97 for (int i = 0 ; i < ITERATIONS ; i ++ ) { 98 System.err.println("Created client: " + i); 99 100 JavaVM jvm = new JavaVM("LeaseLeakClient", 101 " -Djava.security.policy=" + 102 TestParams.defaultPolicy + 103 " -Drmi.registry.port=" + 104 registryPort, 105 ""); 106 jvm.start(); 107 108 if (jvm.getVM().waitFor() == 1 ) { 109 TestLibrary.bomb("Client process failed"); 110 } 111 } 112 numLeft = getDGCLeaseTableSize(); 113 Thread.sleep(3000); 114 115 } catch(Exception e) { 116 TestLibrary.bomb("CheckLeaseLeak Error: ", e); 117 } finally { 118 if (leakServer != null) { 119 TestLibrary.unexport(leakServer); 120 leakServer = null; 121 } 122 } 123 124 /* numLeft should be 2 - if 11 there is a problem. */ 125 if (numLeft > 2) { 126 TestLibrary.bomb("Too many objects in DGCImpl.leaseTable: "+ 127 numLeft); 128 } else { 129 System.err.println("Check leaseInfo leak passed with " + 130 numLeft 131 + " object(s) in the leaseTable"); 132 } 133 } 134 135 /** 136 * Obtain a reference to the main DGCImpl via reflection. Extract 137 * the DGCImpl using the ObjectTable and the well known ID of the 138 * DGCImpl. 139 */ 140 private static int getDGCLeaseTableSize () { 141 int numLeaseInfosLeft = 0; 142 143 /** 144 * Will eventually be set to point at the leaseTable inside 145 * DGCImpl. 146 */ 147 Map leaseTable = null; 148 final Remote[] dgcImpl = new Remote[1]; 149 Field f; 150 151 try { 152 f = (Field) java.security.AccessController.doPrivileged 153 (new java.security.PrivilegedExceptionAction() { 154 public Object run() throws Exception { 155 156 ObjID dgcID = new ObjID(DGC_ID); 157 158 /* 159 * Construct an ObjectEndpoint containing DGC's 160 * ObjID. 161 */ 162 Class oeClass = 163 Class.forName("sun.rmi.transport.ObjectEndpoint"); 164 Class[] constrParams = 165 new Class[]{ ObjID.class, Transport.class }; 166 Constructor oeConstructor = 167 oeClass.getDeclaredConstructor(constrParams); 168 oeConstructor.setAccessible(true); 169 Object oe = 170 oeConstructor.newInstance( 171 new Object[]{ dgcID, null }); 172 173 /* 174 * Get Target that contains DGCImpl in ObjectTable 175 */ 176 Class objTableClass = 177 Class.forName("sun.rmi.transport.ObjectTable"); 178 Class getTargetParams[] = new Class[] { oeClass }; 179 Method objTableGetTarget = 180 objTableClass.getDeclaredMethod("getTarget", 181 getTargetParams); 182 objTableGetTarget.setAccessible(true); 183 Target dgcTarget = (Target) 184 objTableGetTarget.invoke(null, new Object[]{ oe }); 185 186 /* get the DGCImpl from its Target */ 187 Method targetGetImpl = 188 dgcTarget.getClass().getDeclaredMethod 189 ("getImpl", null); 190 targetGetImpl.setAccessible(true); 191 dgcImpl[0] = 192 (Remote) targetGetImpl.invoke(dgcTarget, null); 193 194 /* Get the lease table from the DGCImpl. */ 195 Field reflectedLeaseTable = 196 dgcImpl[0].getClass().getDeclaredField 197 ("leaseTable"); 198 reflectedLeaseTable.setAccessible(true); 199 200 return reflectedLeaseTable; 201 } 202 }); 203 204 /** 205 * This is the leaseTable that will fill up with LeaseInfo 206 * objects if the LeaseInfo memory leak is not fixed. 207 */ 208 leaseTable = (Map) f.get(dgcImpl[0]); 209 210 numLeaseInfosLeft = leaseTable.size(); 211 212 } catch(Exception e) { 213 if (e instanceof java.security.PrivilegedActionException) 214 e = ((java.security.PrivilegedActionException) e). 215 getException(); 216 TestLibrary.bomb(e); 217 } 218 219 return numLeaseInfosLeft; 220 } 221 }