Leasing and Sponsorship
.NET manages the lifecycle of objects using garbage
collection. .NET keeps track of memory allocation and objects accessed by all
the clients in the app domain. When an object becomes unreachable by its
clients, the garbage collector eventually collects it. If the objects are in
the same app domain as the clients, garbage collection functions fine. In fact,
even in the case of a client in one app domain accessing an object in a
different app domain in the same process, garbage collection still works,
because all app domains in the same process share the same managed heap. In the
case of remote objects accessed across processes and machines, however, the
strategy breaks down because the object may not have any local clients. In this
case, if garbage collection were to take place, the garbage collector would not
find any references to the object and would deem it garbage, even though there
are remote clients (on other machines, or even in separate processes on the
same machine) who wish to use the object. The rest of this section addresses
this challenge.
In the following discussion, a "remote
object" is an object in a different process. The core piece of the .NET
remoting architecture designed to address this problem is called leasing and
sponsorship. The idea behind leasing is simple: each server object accessed by
remote clients is associated with a lease object. The lease object literally
gives the server object a lease on life. When a client creates a remote server
object (that is, actually creates it, rather than connects to an existing
instance), .NET creates a lease object and associates it with the server
object. A special entity in .NET remoting called the lease manager keeps track
of the server objects and their lease objects. Each lease object has an initial
lease time. The clock starts ticking as soon as the first reference to the
server object is marshaled across the app domain boundary, and the lease time
is decremented as time goes by. As long as the lease time doesn't expire, .NET
considers the server object as being used by its clients. The lease manager
keeps a reference to the server object, which prevents the server object from
being collected in case garbage collection is triggered. When the lease
expires, .NET assumes that the server object has no remaining remote clients.
.NET then disconnects the server object from the remoting infrastructure. The
server object becomes a candidate for garbage collection and is eventually
destroyed. After the object is disconnected, any client attempt to access it
results in an exception of type RemotingException, letting the client know the
object has been disconnected. This may appear strange at first, because the
object may very well still be alive. .NET behaves this way because otherwise,
the client's interaction with the remote object will be nondeterministic. If
.NET allowed remote clients to access objects past their lease time, it would
work some of the time but would fail in those cases in which garbage collection
had already taken place.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.