I'm curious about shared/static object lifetime in an AppDomain where the RemotingCalls are the cause of creating the shared objects.
We're using a Remoting setup that uses client activated objects of wich we only use the functions to reach into the server. The remoting objects are setup as singletons.
The server setups a channel and uses RemotingConfiguration.Configure to load a configuration file.
Some of these server functions touch and use some static (shared in vb.net) variables on the server. I can't find out what the lifetime of these static variables is, they get created (static constructor is run) when they're touched for the first time. Using logging I can't see the objects dispose/finalize happen.
Waiting for a couple of minutes after connecting to the remoting server sees the shared objects alive and well.
The question:
So what is the expected live time of static objects in this remoting setup. Do they live as long as the AppDomain or do they get cycled out when the Remoting objects get swapped. And what is the correct way to extend their lifetime if needed?
The answer:
Static types live in AppDomain since they accessed the first time till AppDomain is unloaded. So you don't need to extend their lifetime as long as AppDomain is running.
Remoting objects are not garbage collected until after the lease expires - the lease protects them from the GC as there is no obvious visible reference to them. The default lease period is 5 minutes, the garbage collector may run in another couple of minutes (depending on load, memory usage, etc.) and then the last reference to your object should be gone. Only after all this has happened, an instance object should be collected on the next GC run. Static objects, however, won't be garbage collected at all.
As for the second part of the question, the correct way of extending the lifetime is called "sponsorship" - basically when the lease expires, the server asks the clients if anyone is willing to continue using this object. There's a pretty detailed article on the subject here. Don't just set the lifetime to infinity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With