Question about Destroying Objects..?

  • Thread starter Thread starter pachanga
  • Start date Start date
P

pachanga

After you destroy a Object and its send to the garbage collections, can
you retrieve the object back? Also, if you can, can you destroy an
object permantly with no trace of it?
 
pachanga said:
After you destroy a Object and its send to the garbage collections,
can you retrieve the object back? Also, if you can, can you destroy
an object permantly with no trace of it?

Firstly, you don't destroy an object - it gets released for you
automatically. You don't send it to the garbage collector - the garbage
collector notices when you no longer need it.

After it has been garbage collected, there is no way of retrieving it.
 
Hi pachange,

In addition to Jon I might add that it is possible to ressurect an
object after you mark it for garbage collecting (clear references) and
before it is being collected. The garbage collector calls the
object's finalizer [~MyClass()] prior to destroying it. You can at
this stage reattach references. I believe this finalizer will only
be called once, and not in a ressurected object.

After the object has been garbage collected there is no way to revive it.
Note also that this in no way is the same as a destructor, but is handy
for cleaning up resources like file handles and database connections.

Further reading: IDisposable, IDisposable.Dispose, Object.Finalize
 
Hi Pachanga,

All .NET objects are created by CLR on a managed heap. Once destroyed and GC
called by CLR there is now way you can retrieve the object back. Reason is
first > The managed heap is rebuilt and optimized for freed up space thereby
changing application roots (Pointers containing addresses)
Second > Programmatically you cannot access any application root for direct
memory read or write.

Also how do you trace an object say after creation if its there in memory...
Thanks
C# Pro
 
Hi Pachanga,

You won't ever know when the garbage collector will run. However, if you
had a situation where you wanted to be able to resurrect an object before
the garbage collector got to it, you could use weak references. I mention
it because it exists and is a possibility, but I make no assumptions as to
whether it is appropriate in your case. Before using it, you should learn
as much as you can about garbage collection. Here's a very simple example:

Test tst1 = new Test();

WeakReference wkTst = new WeakReference(tst1);
tst1 = null;

//GC.Collect();

Test tst2 = (Test)wkTst.Target;

if (tst2 != null)
{
Console.WriteLine("Test has not been GC'd");
}
else
{
Console.WriteLine("Test has been GC'd");
}

Uncomment the GC.Collect() line for a different result. BTW, you should
*not* use GC.Collect in your code. The garbage collector is optimized to
know when it should run and you could potentially slow your own code down by
using it yourself. This is simply an example to force the behavior of the
weak reference.

Here are a couple articles you can read to get a better understanding of how
Garbage Collection and Automatic Memory Management works:

http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/

Joe
 
Back
Top