Stub/Exported object/Exportee lifecycle

When I export an object it creates a stub. I am wondering what is the lifetime of this stub. or more specifically, what is the lifetime of an exported object.

It seems that if a client calls a method, and in that method I export an object, but I do not hold that object locally, and I do not pass that object back to the client, then the stub / remote object can go stale at anytime.

Consider this

void MyObject remoteCall(){

Remote serverObj =new ServerObject();

UnicastRemoteObject.export(serverObj);

return serverObj;

}

In the above piece of code it is resonable to assume the GC/DGC can not collect the 'exported' object. That is, the stub will live because a reference to it is held until the RMI subsystem does its own substitution and returns the stub to the client. But consider code below

void MyObject remoteCall(){

return getObj();

}

Remote getObj(){

Remote serverObj =new ServerObject();

UnicastRemoteObject.export(serverObj);

serverObj;

}

In the above case, after the call getObj() completes, there is no strong reference to the stub. Thus, I expect that technically the DGC can collect it immediately. And my remoteCall method can fail since the object is no longer exported.

IIRC exported stubs/proxies are held with weak references.

I am trying to devise a system whereby the client can ask for an object, and the server will check in a local cache to see if it is already available and exported. But the unreferenced behavior is causing a problem because I can not guarantee the object is exported at anytime because this method can run at anytime unless I am holding a strong reference to the stub I guess!?

Perhaps that is a question there. If I hold a strong reference to the stub on the server will this keep the remote object exported? If I hold a strong reference to the remote object exported on the server, will this keep that object exported?

At this point I am starting to consider caching my 'remote' objects and simply exporting the object each time a client requests it, or I will have to maks sure the object is exported within the method call of the client and that the stub has a strong reference for the duration of the client call!?

I used to be confident, but I did not write enough comments in my code and so I am confused again :D

[2768 byte] By [_dnoyeBa] at [2007-11-27 8:47:30]
# 1

Another quick point. I am having unreferenced being called on my remote objects which tells me no clients are using them. The server only holds weak references to them, but said weak references are not cleared in a timely manner that I would expect after the unreferenced call. I am trying to get a GC call on the server now...

_dnoyeBa at 2007-7-12 20:52:28 > top of Java-index,Core,Core APIs...
# 2
The RMI runtime system retains a map of exported objects to stubs. So the stub is live and won't be GC'd locally for as long as the exported object remains exported.Call system.gc() inside the unreferenced() method.
ejpa at 2007-7-12 20:52:28 > top of Java-index,Core,Core APIs...
# 3
And by exported you mean as long as a stub is referenced by a client? or as long as the server holds a reference to the stub or as long as the server holds a reference to the exported object?
_dnoyeBa at 2007-7-12 20:52:28 > top of Java-index,Core,Core APIs...
# 4

The object stays exported until

(a) it is unexported by the application, or

(b) it is locally GC'd.

It can't be locally GC'd until

(i) all local references to it in the application have been cleared, and

(ii) all remote references to it by clients (and Registries) have been cleared by the action of DGC (which in turn clears local references to it in that table I described before.

I'm not sure about the server holding a reference to the stub, I don't think that's relevant.

So the answer to your question is yes, no, and yes.

ejpa at 2007-7-12 20:52:28 > top of Java-index,Core,Core APIs...