Re: garbage collection of remote objects

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

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


Date: Sat, 8 May 2004 17:45:47 -0400

I agree with GC == not_predictable. However, it seems like it *never*
releases these object. In previous version of this code I called
GC.Collect() and GC.WaitForPendingFinalizers() at different points in
implementation. It did nothing.
Has anyone ever "seen" a MarshalByRefObject disposed "naturally" before app
or appdomain starts shutting down?

Mr071

"Sunny" <sunnyask@icebergwireless.com> wrote in message
news:eVDh7bENEHA.3016@tk2msftngp13.phx.gbl...
> Hi,
> GC is not predictable. You do not know when and how it will happen. Try
> using:
> GC.Collect();
> GC.WaitForPendingFinalizers();
>
>
> Sunny
>
> In article <O#OB8W9MEHA.1312@TK2MSFTNGP12.phx.gbl>, "Mr071" <sanins @
> _no_spam_ hotmail.com> says...
> > 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?
> >
> >



Relevant Pages

  • Re: Running java programs from class files
    ... public static void main{ ... I can get it to work if I put the jar file in the Class-Path: line in the manifest and put the jar file in the same directory as the program jar file but not from the command line. ... The Lib class has been compiled and put into a jar file, ...
    (comp.lang.java.programmer)
  • Re: Anybody know how to set the color of the text in a disabled JMenuItem?
    ... platform specific implementation intended to be visibly distinct from ... public class test extends JFrame { ... private static final Color disColor = UIManager.get; ... public static void main{ ...
    (comp.lang.java.gui)
  • Re: pl. anyone can provide ans for the following code
    ... extends PrintStream ...
    (comp.lang.java.programmer)
  • Re: aligning components within boxes
    ... public class Test extends JFrame { ... public static void main{ ... Everybody else gave you excellent examples of how to fix your alignment problems with BoxLayout. ...
    (comp.lang.java.programmer)
  • Re: How to escape Main?
    ... If - as in the code below - put the call to init() in main, main gives me the compile method. ... public class myClass ... public static void main{ ...
    (comp.lang.java.programmer)