garbage collection of remote objects

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Mr071 (_at_)
Date: 05/07/04


Date: Thu, 6 May 2004 21:50:56 -0400

Despite everything that has been written about how LifetimeServiceManager on
the remote server eventually "release" the reference to the object so that
it can be garbage collected it *never* happens. I am not sure what needs to
be done in this code to make it release the remoted reference:

using System;
using System.Threading;
using System.Runtime.Remoting.Lifetime;

public interface IMy
{
 bool isAlive { get; }
}

public class Test
{
 public static void Main()
 {
  try
  {
   LifetimeServices.LeaseManagerPollTime = TimeSpan.FromSeconds(2);

   DataClass _x = new DataClass();
   _x.kill();
   _x = null;

   AppDomain _d = AppDomain.CreateDomain("new domain");
   DataClass _c = (DataClass)_d.CreateInstance("test",
"DataClass").Unwrap();

   ILease _l = (ILease)_c.GetLifetimeService();
   Console.WriteLine(_l.GetType().FullName);

   Console.Write("\npress ENTER to kill object");
   Console.ReadLine();

   _c.kill();

   Console.Write("\npress ENTER to induce GC");
   Console.ReadLine();

   Console.WriteLine("Abusing memory");

   int x = 150;
   byte[][] b = new byte[x][];
   for (int i = 0; i < x; i++)
   {
    b[i] = new byte[1024 * 1024];
    for (int j = 0; j < 1024 * 1024; j++)
     b[i][j] = (byte)(j % 255);
   }

   for (int i = 0; i < x; i++)
    Console.Write("{0} ", b[i][0]);

   Console.Write("press ENTER to end");
   Console.ReadLine();
  }
  catch (Exception e)
  {
   Console.WriteLine(e);
  }
 }
}

public class DataClass : MarshalByRefObject, IMy
{
 private bool theFlag = true;

 public void doWork() { Console.WriteLine("working"); }

 public override object InitializeLifetimeService()
 {
  ILease lease = (ILease)base.InitializeLifetimeService();
  if (lease.CurrentState == LeaseState.Initial)
  {
   lease.InitialLeaseTime = TimeSpan.FromSeconds(1);
   lease.SponsorshipTimeout = TimeSpan.Zero;
   lease.RenewOnCallTime = TimeSpan.Zero;
  }
  lease.Register(new MySponsor(this));

  return lease;
  }

  public bool isAlive { get { return theFlag; }}
  public void kill() { theFlag = false; }

  ~DataClass()
  {
   Console.WriteLine("Disposing");
  }
}

public class MySponsor : MarshalByRefObject, ISponsor
{
 private IMy my;

 public MySponsor(IMy my)
 {
  this.my = my;
 }

 public TimeSpan Renewal(ILease lease)
 {
  Console.WriteLine("\nContacted...");
  Console.WriteLine(my.isAlive);
  if (my.isAlive)
   return TimeSpan.FromSeconds(2);
  else
   return TimeSpan.Zero;
 }
}

Any ideas?

-- 
Mr 071, MCSD.NET, MCAD.NET
-----------------------------------------------
"...the rank and file are usually much more primitive than we imagine.
Propaganda must therefore always be essentially simple and repetitious."
-Joseph Goebbels, Nazi Propaganda Minister, aka Rush Limbaugh, aka Bill
O'Reilly, aka FOX News Editors.


Relevant Pages

  • Re: How to configure an assembly using CodeBase ...?
    ... Since the new assembly is on remote server, I could not move it to local GAC ... Without recompiling the application how can I make reference ... What do you mean by "CodeBase"? ...
    (microsoft.public.dotnet.languages.csharp)
  • Connect to remote COM+ object
    ... reference to a COM+ dll to get the class I need. ... hosted on a remote server as a COM+ component. ... Retrieving the COM class factory for remote component with CLSID ...
    (microsoft.public.dotnet.languages.csharp)
  • Connect to remote COM+ object
    ... reference to a COM+ dll to get the class I need. ... hosted on a remote server as a COM+ component. ... Retrieving the COM class factory for remote component with CLSID ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Are (Pick) basic and its programmers "obsolete"?
    ... We don't need carrots, we want lettuce, aka the green stuff. ... This is in reference to situations where people promise purchase of ...
    (comp.databases.pick)
  • Re: Im rolling a lock!
    ... (raiders out there will get the reference;). ... AKA the most useless epic item ... ever, which had a ridiculously high drop rate off Sulfuron Harbinger, and ...
    (alt.games.warcraft)