| CONTENTS |PREV |NEXT | Java Remote Method Invocation |
To accomplishreference-counting garbage collection, the RMI runtime keeps trackof all live references within each Java virtual machine. When alive reference enters a Java virtual machine, its reference countis incremented. The first reference to an object sends a"referenced" message to the server for the object. As livereferences are found to be unreferenced in the local virtualmachine, the count is decremented. When the last reference has beendiscarded, an unreferenced message is sent to the server. Manysubtleties exist in the protocol; most of these are related tomaintaining the ordering of referenced and unreferenced messages inorder to ensure that the object is not prematurely collected.
When a remote object isnot referenced by any client, the RMI runtime refers to it using aweak reference. The weak reference allows the Java virtualmachine's garbage collector to discard the object if no otherlocal references to the object exist. The distributed garbagecollection algorithm interacts with the local Java virtualmachine's garbage collector in the usual ways by holding normalor weak references to objects.
As long as a localreference to a remote object exists, it cannot be garbage-collectedand it can be passed in remote calls or returned to clients.Passing a remote object adds the identifier for the virtual machineto which it was passed to the referenced set. A remote objectneeding unreferenced notification must implement thejava.rmi.server.Unreferenced interface. When thosereferences no longer exist, theunreferenced methodwill be invoked.unreferenced is called when the setof references is found to be empty so it might be called more thanonce. Remote objects are only collected when no more references,either local or remote, still exist.
Note that if a networkpartition exists between a client and a remote server object, it ispossible that premature collection of the remote object will occur(since the transport might believe that the client crashed).Because of the possibility of premature collection, remotereferences cannot guarantee referential integrity; in other words,it is always possible that a remote reference may in fact not referto an existing object. An attempt to use such a reference willgenerate aRemoteException which must be handled bythe application.