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