Also when you don't call it, you might finish with out of memory errors and
thousands of old objects floating in heap, which GC did not process just
because application was in tight loop. So, you have to choose - what is more
important in your case.
There are some hints in
http://msdn.microsoft.com/architectu...l/scalenet.asp
on what to keep in mind. Calling GC.Collect is really last resort and I
agree here with Eric. However, disposing or nulling is another issue.
Sometimes even simple setting of reference to null helps GC to collect
efficiently. I guess it depends how quickly you create and dispose your
objects when running and how much time you give GC to work.
It is expected that object is taken out of heap when last reference to it is
going out of scope or is explicitly nulled. However, when last reference is
nulled say only after couple of hours, which is common with hashtables, GC
might be "confused".
I find it also "confusing" when people are pointing out that Dispose is for
unmanaged resources. The rule is that if object is IDisposable you must
dispose it. You never know which resources are used in the object.
What I find surprising, is that for example Marshal.PtrToStructure is eating
heap, but Marshal.ReadIntPtr is not. And you have no Dispose for structures.
Also, Message.GetLParam method allocates from heap. These are managed
methods, which have no relation to Dispose yet. I think, here MS did "small"
mistake. I understand that you never know what kind of structure will be
instantiated, however heap-allocated ones must be disposable.
So, my last advice is - do what you want, because general recommendations
are working "generally". But at the end of the day use profiler.
HTH
Alex
"Eric Gunnerson [MS]" <er****@online.microsoft.com> wrote in message
news:eY**************@TK2MSFTNGP09.phx.gbl...
The GC handles all of the details, and as others have pointed out,
Dispose() is for the times when you have unmanaged resources.
You should avoid calling GC.Collect() directly, as you will likely
increase the amount of time spent by the GC process. There is a fair amount of
overhead to a GC, so if you call it very time an object could be free'd,
you'd be wasting a large amount of time.
--
Eric Gunnerson
Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/
This posting is provided "AS IS" with no warranties, and confers no
rights. "xyu" <xy*@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com... Hello,
First I would like to thank anyone who helps me, much appreciated.
I'm a c++ programmer just started using c#. I'm writing some big hash table so I want to actively take my object off the heap and release the
memory when it's deleted from the hash table so that GC recollection does
not need to run that frequently.
I read up examples about Dispose method in MSDN. What I find very
strange is that it's all talking about how to release managed and unmanaged
resources inside this object but there isn't any code to release the
object itself? How is that managed?
Example(from MSDN)
public class DisposeExample
{
public class MyResource: IDisposable
{
private IntPtr handle;
private Component component = new Component();
private bool disposed = false;
public MyResource(IntPtr handle)
{
this.handle = handle;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if(!this.disposed)
{
if(disposing)
{
component.Dispose();
}
CloseHandle(handle);
handle = IntPtr.Zero;
}
disposed = true;
}
[System.Runtime.InteropServices.DllImport("Kernel32 ")]
private extern static Boolean CloseHandle(IntPtr handle);
~MyResource()
{
Dispose(false);
}
}
public static void Main()
{
MyResource obj = new MyResource( //whatever here );
// I call this function to clear all the resource obj holds
// What why I can not find any code which take obj itself off
the heap????? obj.Dispose();
}
}