Marshal-by-reference objects (MBRs) do not reside in memory forever, whether they are server-activated Singleton objects or client-activated objects. Instead, unless the type overridesto control its own lifetime policies, each MBR has a lifetime that is controlled by a combination of leases, a lease manager, and a number of sponsors. In this case, an MBR object's lifetime is the total time the object remains active in memory. A lease is the period of time that a particular object is active in memory before the .NET Framework remoting system begins the process of deleting it and reclaiming the memory. The server application domain's lease manager is the object that determines when the remote object is marked for garbage collection. A sponsor is an object that can request a new lease for a particular object by registering itself with the lease manager.
Whenever an MBR object is remoted outside an application domain, a lifetime lease is created for that object. Each application domain contains a lease manager that is responsible for administering leases in its domain. The lease manager periodically examines all leases for expired lease times. If a lease has expired, the lease manager sends a request to its list of sponsors for that object and queries whether any of them commit to renew the lease. If no sponsor renews the lease, the lease manager removes the lease, the object is deleted, and its memory is reclaimed by garbage collection. An object's lifetime, then, can be much longer than its lifetime lease, if renewed more than once by a sponsor or by continually being called by clients.
A remote object's life is independent of the lives of its clients. The lease for a basic object may be very long, in which case the object may be used by multiple clients. The object's lease may be periodically renewed by a client, keeping the object alive longer. This approach uses leases efficiently because very little network traffic is required for distributed garbage collection. However, remote objects that use scarce resources can have a lease with a short lifetime, which a client frequently renews with a short time span. When all the clients are finished with the remote object, the .NET Framework remoting system marks the object for garbage collection quickly. This policy substitutes increased network traffic for more efficient use of server resources.
Using leases to manage the lifetime of remote objects is an alternative approach to reference counting, which can be complex and inefficient over unreliable network connections. Although leases can be configured to extend the lifetime of a remote object longer than is precisely required, the reduction in network traffic devoted to reference counting and sending requests to clients, makes leasing an attractive solution when properly configured for a particular scenario.
The following table describes the main properties of leases.
Specifies the initial span of time that an object remains in memory before the lease manager begins the process of deleting the object. In the configuration file, this is the leaseTime attribute of theconfiguration element. The default is 5 minutes. A lease time of 0 sets the lease to an infinite lifetime.
Specifies the span of time left before the lease expires. When a lease is renewed, its CurrentLeaseTime is set to the maximum of the CurrentLeaseTime or the RenewOnCallTime.
Specifies the maximum time span that the CurrentLeaseTime is set to after each remote call to the object. The default is 2 minutes.
Specifies the time that the lease manager waits for the sponsor to respond when notified that a lease has expired. If the sponsor does not respond in the specified time, the sponsor is removed and another sponsor is called. If there are no more sponsors, the lease expires and the remote object is marked for garbage collection. If the value is 0 (), the lease does not register sponsors. The default is 2 minutes.
Specifies the amount of time that the lease manager sleeps after checking for expired leases. The default is 10 seconds.
Leases are created when an MBR object is activated in another application domain. At that point, when theproperty is , the lease properties can be set. Once set, they cannot be changed directly. Only the CurrentLeaseTime can be changed, either from an call or when the lease manager calls on a sponsor and the sponsor responds with a object. MarshalByRefObject has the default implementation of a lifetime lease and unless this lease is modified when it is created, the lease properties are always the same.
Modifying Lease Properties
The lifetime lease properties can be modified in the following ways:
Declaring custom lifetime lease properties by overriding MarshalByRefObject.InitializeLifetimeService in your MBR object, either to set the properties on the lease yourself or to return a null reference (Nothing in Visual Basic). The latter option tells the .NET Framework remoting system that instances of this type have an infinite lifetime.
A developer or an administrator can also specify lifetime properties for all objects in a particular application in theelement in the application or machine configuration file. For more information, see .
Once created, a lease can be renewed in the following ways:
A client calls thedirectly.
If theproperty is set, each call to the remote object renews the lease for the specified time.
The lease calls anmethod to request a lease renewal and the sponsor responds with a .
For details, see.
Lease managers must periodically examine leases for time expiration. When a lease's time has expired, the lease is informed and it attempts to renew itself by invoking its sponsors.
The lease manager also maintains a list of sponsors from which leases are waiting for replies. If a sponsor does not respond in the interval specified by thetime span, it is removed from the sponsor list.
Note that it is possible for a malicious remoting client to abuse the lease system to mount a denial of service (DOS) attack against a remoting server. The malicious client sponsors many leases and then refuses to answer the server's renewal queries. This DOS attack is only possible ifis set to full.
When a lease is allowed to expire, no further lease messages or sponsor returns are accepted. The lease's reference is removed from the lease list, and the .NET Framework remoting system removes the object reference from its internal table. The garbage collection system then removes the lease and the object.