|
-
March 9th, 2006, 02:41 PM
#1
How to dispose of Value Types on the heap
I derived a very simple class from EventArgs,IDisposable:
Code:
public class GoodEventArgs : EventArgs,IDisposable
{
public static int m_nDivisor;
public readonly int m_nQuotient;
public GoodEventArgs(int nQuotient)
{
//Is this the memory leak?
m_nQuotient = nQuotient;
}
public void Dispose()
{
//I only have Value Types, should I be doing anything in here?
}
}
Now I am tyring to see the effect that declaring many of these classes will have on memory...
Code:
GoodEventArgs.m_nDivisor = 2;
for (int index = 0; index <= 99; index++)
{
if (index % GoodEventArgs.m_nDivisor == 0)
{
//Or is this the memory leak?
using (GoodEventArgs newDivisibleEvent = new GoodEventArgs(index))
{
OnGoodEvent(newDivisibleEvent);
}
}
}
My memory usage for my app in task manager increases from 13,488K to 13,604K when I run through this for loop and I never get the memory back to 13,488. So it seems that I am leaking a little bit of memory each time I go through the for loop.
The only member variable of GoodEventArgs is of Type int. So Its not like I can set that to null and claim the memory back in the Dispose() fxn. How can I claim this memory back? Maybe the memory is allocated to the new GoodEventArgs and not the GoodEventArgs.m_nQuotient? In which case, how can I claim back the memory used by my GoodEventArgs refernce type?
-
March 9th, 2006, 06:02 PM
#2
Re: How to dispose of Value Types on the heap
The garbage collector will reclaim the memory WHEN IT NEEDS TO.
Memory in .NET isn't like memory in C++ : it's managed by the garbage collector.
It's not wise to try to free up managed memory yourself : believe me the garbage collector is very good at its job and 99.99% of the time you can just let it get on with it and forget about the memory.
Darwen.
-
March 9th, 2006, 06:11 PM
#3
Re: How to dispose of Value Types on the heap
Please comment on this article. http://www.codeproject.com/csharp/ID...asp#xx596929xx
I went through the article step by step and it creates a fairly convincing argument on the side of managing memory as allocated by the 'new' operator.
-
March 10th, 2006, 03:24 AM
#4
Re: How to dispose of Value Types on the heap
I went through the article step by step and it creates a fairly convincing argument on the side of managing memory as allocated by the ' new' operator.
Which bit in particular?
Quickly scanning the article, I get the impression that the article is only concerned with reclaiming memory allocated by creating *Bitmaps*. This is understandable since bitmaps can use a lot of memory which will not be reclaimed until the Dispose method is called.
Note that the memory reclaimed is not for the Bitmap object itself which is still under the control of the garbage collector, but for the external Windows resources that it uses.
You should read more about what Dispose is used for.
Useful? Then click on (Rate This Post) at the top of this post.
-
March 10th, 2006, 03:56 AM
#5
Re: How to dispose of Value Types on the heap
You don't need to implement the IDisposable interface if all you have are value or simple reference types. The IDisposable is used to free unmanaged resources, or very large objects.
In your case you don'r need the IDisposable class.
You can explicitly set the object to be null (if it is a reference type) and if the object is not used somewhere else the GC might collect it faster.
-
March 10th, 2006, 04:36 AM
#6
Re: How to dispose of Value Types on the heap
GC.Collect() method forces gabage collection..in case you insist
-
March 10th, 2006, 11:09 AM
#7
Re: How to dispose of Value Types on the heap
Thanks everyone for chiming in. I'm still trying to wrap my brain around the concept of memory management in C#. Actually, its more like I'm trying to let go of what I know to be true in C++. That is, "I am in complete control in C++". When I 'new something, I simply 'Delete it when I'm done.
But now, in C#, I have to trust the framework to delete something?
Here is an example. In the following code, I am as responsible as I can possibly be with memory management, and I still see the memory building in task manager when I invoke the handler a few times (or 100 times).
Code:
//derive from EventArgs
public class GoodEventArgs : EventArgs,IDisposable
{
public static int m_nDivisor;
public readonly int m_nQuotient;
bool m_bDisposed = false;
ImageClass m_image = new ImageClass();
public void Dispose()
{
//this is the version of Dispose the that client would invoke manually,
//invoking this method means that the client wants to destruct the object prematurely
//(before the compiler gets around to it) so dispose all managed and unmanaged types
Dispose(true);
//alert the Garbage Collection that we don't need to worry about cleaning up this object,
//we already did it manually by calling Dispose() public method
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool bDisposing)
{
if (!m_bDisposed)
{
//dispose managed resources
if (bDisposing)
{
m_image.Dispose();
}
//if there are any unmanaged resources, dispose them manually here (handles, connections, etc...)
//don't have any right now
//next time we get here, we will know we've already been here
m_bDisposed = true;
}
}
public GoodEventArgs(int nQuotient)
{
m_nQuotient = nQuotient;
m_image.LoadImage();
}
~GoodEventArgs()
{
//this dtor will implicitly handle managed object dispose on its own,
//so just call the Dispose fxn to dispose of unmanaged resources only
Dispose(false);
}
}
//the delegate and target fxn
public delegate void BadEventHandler(object theObject, BadEventArgs eventArgs);
public class DivisibleListener
{
public void GoodUserEntry(object theObject, GoodEventArgs eventArgs)
{
Console.WriteLine("Divisible by " + GoodEventArgs.m_nDivisor + ": "
+ eventArgs.m_nQuotient);
}
public void BadUserEntry(object theObject, BadEventArgs eventArgs)
{
Console.WriteLine("... '" + eventArgs.m_chPressed + "' key is invalid\nKey pressed must be between 1 and 9");
}
}
//define handler
public static event GoodEventHandler theGoodHandler;
public static void OnGoodEvent(GoodEventArgs eventArgs)
{
if (theGoodHandler != null)
theGoodHandler(null, eventArgs);
}
//invoke
GoodEventArgs.m_nDivisor = nDivisor;
GoodEventArgs newDivisibleEvent = null;
for (int index = 0; index <= 99; index++)
{
if (index % GoodEventArgs.m_nDivisor == 0)
{
using (newDivisibleEvent = new GoodEventArgs(index))
{
//memory usage builds up in task manager here
OnGoodEvent(newDivisibleEvent);
}
}
}
Will someone please convince me to stop worrying about this problem. Maybe I just am not grasping GarbageCollection yet?
-
March 10th, 2006, 12:03 PM
#8
Re: How to dispose of Value Types on the heap
It goes back to what Darwen posted earlier
The garbage collector will reclaim the memory WHEN IT NEEDS TO
Why don't you run a couple of instances of your program and also load up you computer's memory with some other apps. Then check in task manager (or a memory profiler) if the memory used by your program gets released as the available memory on the machine decreases.
I have tried this with apps I have worked on and garbage collector kicks in as needed.
-Satish
-
March 10th, 2006, 12:27 PM
#9
Re: How to dispose of Value Types on the heap
here's a tool you can use for that too (one for each version of the framework)
.net 1.1
.net 2.0
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|