A question about Dispose method

G

Guest

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();
}
}
 
G

Guest

The example that you are quoting is specifically targetted at releasing unmanaged resources, hence there is a need to tell GC to not perform its finalization and let us do the releasing of objects because we know how a specific unmanaged object/resource behaves and allocates and deallocates stuff. That kind of example is not focused on managed objects because thats automatically done for you by the runtime in cooperation with GC. Now if you do want your managed objects to be collected when you want
try null-ing your object and than just calling GC.Collect() after that, you 'll see that the memory that your object was occupying is released.
example:
class ctype
{
int [] obj;//= new int[1000000];
string p="hello";

public void allocate()
{
obj= new int [10000000];
}
}

[STAThread]
static void Main(string[] args)
{
ctype obj= new ctype();
obj.allocate(); //memory goes up
obj= null; // nothing happens. GC is watching CNN
GC.Collect(); // object collected

}

Now there can be one more case in which you have written some cleanup logic in a Dispose method and you want that called as soon as your object goes out of scope. To ensure that this happens, you can implement an IDisposeable iterface and writiing your code in a "using" block as (from msdn):

Font MyFont3 = new Font("Arial", 10.0f);
using (MyFont3)
{
// use MyFont3
} // compiler will call Dispose on MyFont3

Also, do some reading on Undeterministic Finalization as you are new to C#.

Hope that helps.

Abubakar.
http://joehacker.blogspot.com/

xyu said:
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();
}
}
 
J

Jon Skeet [C# MVP]

xyu said:
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?

The garbage collector is responsible for actually destroying objects
and freeing the memory associated with them. You don't need to handle
that at all - although if you have a reachable reference to an object
you don't need, in some cases (very, very rare IME) it may be worth
setting the reference to null to help the garbage collector.
 
E

Eric Gunnerson [MS]

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 said:
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();
}
}
 
A

AlexS

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/architecture/default.aspx?pull=/library/en-us/dnpag/html/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 said:
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 said:
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();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

.net object dispose question 1
about dispose 6
On C# Dispose Pattern 2
Dispose pattern improvement? 4
Dispose method problem 1
Question on implementing IDisposable 4
IDispose problem 1
object dispose 2

Top