Hello All,
I am developing a window form applicationin .NET Compact Framework 2.0. My problem is after i dispose a form it is not freeing up the memory used by that form.
Here is the code ::
static
void Main(){
1. using (Form1 frm = new Form1())
2. {
3. frm.ShowDialog();
4. }
5. }
If Form1 is a blank form with just a button that close the form (this.Close()) then my Memory usage is as follows
Line No. Used Memory in MB
1. 17.08
Once Form1 is loaded 17.42
5. 17.42
Before the application started the Used Memory size was 14.55. MB
FYI ::
I know that the Compact framework also take some space, but 14.55 MB incudes CF being used before.
Because I am in debugging mode so the application is taking almost 2.5 MB for starting.
Someone please help me on the above as if onclosing as well as disposing form if CF does no free up memory, my application will go out of memory very soon.
Thankyou,

How to free resources on windows form
critical5
NETCF is garbage collected; memory is not releases immediately but only when GC runs. If you don’t actually see OOM exception, there’s probably nothing to worry about. Run that in a loop 1000 times or so and see if you run out of memory if you suspect there’s an issue.
J1234
what all measures should it take to release resources.
I have created a DBAccess class as well as GeneralFunctions class, is this a good idea or bad. I call these classes on almost each Form that i use.
I tried to dispose all the object that i create than also it is not releasing almost 2 MB once the form is disposed.
Robert-UTS
Deepu.MI
Jeanne P
So you do get OOM, then there's a problem.
Are you disposing of all objects implementing IDisposable, e.g. Bitmap objects you're using on these forms
For example, each time you do something like this previous bitmap would be stuck in memory:
pictureBox1.Image = new Bitmap(“picture.jpg”);
Also, if you using DataBinding, make sure to set DataSource on, say, DataGrid control to null in form’s Dispose().
Promit
You have to call Dispose() on all bitmaps you no longer need, that's the only way.
You can do so from FormClosing event, override form's Dispose() and so on.
Neo the 1
Can i somehow froce the Garbage collector to collect any resource that is currently not being referenced.
Dallastower
Ilya - personally thanks for this valuable info.
I do have a question but apologise if its silly, I don't have much experience directly with GC.
I know invoking/calling GC is bad and should be slapped on the back of your hand for doing so (as the saying goes), but in this case making an exception, would it be ok to call the GC and release resources back to the OS
once again, apologies for this silly Q
Thanks
luca82
You can, but that’s pretty pointless in this particular case as application is about to quit and all resources would be freed anyway.
If you add a loop, you might want to invoke GC on each iteration and print out allocated memory to the debugger.
That would allow you to monitor amount of memory dynamically. Normally it would stabilize after few iterations; that means there’s no leak.
prk
i was facing similar problem, instead of multiple forms , i have a single form , and multiple control, so instead of opening another form i just load the repective control. I noticed when i add lot background images and different images to my app, OOM started to appear , then i updated the Dispose method (generated winform designer) and added calls to dispose so that each and every bitmap is disposed. this delayed the OOM to a larger extent , but still after a very long time i m stil getting it.
i have added bitmaps as a resource in the control . do i need to clear them too
might be they are casusing the problem.
any idea how to detect.
regards
Sergio Ordine
Are you running into OOM exception after you started disposing of everything
xRuntime
The above example was just to explain the problem.
In my application i have almost 10 form being called from the same class as specified above. Each of these form takes almost 3-4 MB of data. What is happening is everytime the form is closed it is not releasing memory.
Due to the above reason I get the Out of Memory Exception also.
Can we collect the memory used by a form after the form is being disposed.
I tried to use GC.Collect also. But it also seems to only collect few KB of data out of 3-4 MB being used by the Form
Please suggest.
selva_kumar
GC does that automatically except for native resources like Bitmaps. It's up to you to Dispose() of all IDisposable objects to free these resources.
Tammam_Koujan
That most likely means there's something you're not disposing of. Or, is it possible you’re keeping references to these disposed forms or other objects so they can not be collected Have you tried NETCF RPM tool to monitor memory allocation and GC runs
http://blogs.msdn.com/stevenpr/