What is the Best way to release memory

M

Manoj Misran

I have an MDI application with only one top level menu. All the menu
options called child forms and are no merge menus on these child forms.
They are simple forms with various controls and other things.

Now if I create various objects in this form class, do I really need to
dispose them and set them to null before closing the form? When I close
the form and set the form to null, I assume that all the variables or
objects or events created within that form should become unreachable
anyway and should be available for GC to collect. But then when I check
using scitech memory profiler and take a snapshot before calling the
form and compare it with a snapshot after closing and setting the form
to null, I see hundreds of strings and other objects generated and never
released. If I do the same thing second time, I see the same thing again
but lot less new objects and so on......

Can someone explain me what is wrong here and what is the best practice.
My application is 24/7 kind of business app and it runs out of memory
after every 3-4 hours of continuous use.

Thanks in advance

Manj Misran
 
1

100

Hi Manoj,
Ideally you don't have to worry about this. Since you have closed the form
and set all references to null it is enough. You can use the profiler and
you can see that the things stay in memory and they will stay there until
your application needs this memory. The GC will be started and the memory
occupied by those objects will be reclaimed. If your application doesn't use
extensively memory GC may not be started at all.

What you have to do is to remove all reference to the form. Bear in mind
that if this MDI-child form uses events form other controls or object (not
part of the form), which is unlikely I think, those events are references to
the form as well so make sure you remove all handlers to events of controls
and objects which may stay after the form is closed.

Well that is *idealy* I've seen posts in this group where programmers
developing 24/7 applications complains that MenuItem has bug that cause
memory leaks and finaly their applications get terminated by the OS with
message saying that the application has run out of memory. So if you have
such a problem you may consider not using windows forms' menu controls and
go for third party one.

B\rgds
100
 
M

Manoj Misran

Thank you very much for the reply Mr. Noname ;-).
You said "Ideally you don't have to worry about this. Since you have
closed the form and set all references to null it is enough."
Here what do you mean by "all references" is it all references to the
form or all references of all objects or variables created inside the
form.

You are absolutely right on the memory leak in Menu Items, but I found a
work around on that problem but still I am running out of memory after
every 3-4 hours and this thing is pissing me off. Its a simple form and
different users come and open the form, enter data and close the form
and go. In an hour this form is opened and closed approximately 60
times. So after four hours i.e. 60*4=240 times if you open and close the
form, it runs out of memory. I open and close the form using following
code:

Form myForm = new LoadForm();
myForm.ShowDialog();
myForm.Dispose();
myForm=null;

Is there anything that I am missing here. Its a very simple application
and I am not keeping any reference of this form anywhere other that this
myForm variable.

Thanks again for the reply Sir.


Manj Misran
 
1

100

Hi Manoj,
Thank you very much for the reply Mr. Noname ;-).
You said "Ideally you don't have to worry about this. Since you have
closed the form and set all references to null it is enough."
Here what do you mean by "all references" is it all references to the
form or all references of all objects or variables created inside the
form.
No, only to the references to the form is enough. Since the form is not
referenced all objects inside are not reachable thus eligable for GC. Does
your LoadForm use any events or delegates of objects not created inside.
Those are references too. As long as you use ShowDialog I suppose the answer
is *NO*.
Are you sure this very form eats you memory? It could be in other parts of
the application.
Try to do the following. Open the form and take the snapshot of the memory
usage. Then close the form set the referece to null and call
System.GC.Collect() and then System.GC.WaitForPendingFinalizers() and look
at the memory usage again. Try to find out if all objects of the form and
the form itself get collected.
You shouldn't use GC class in normal circumstances, but hopefully it might
help you to localize the problem.

B\rgds
100
 
M

Manoj Misran

No, only to the references to the form is enough. Since the form is not
referenced all objects inside are not reachable thus eligable for GC. Does
Thank you. That's what I thought should be the case but the actual
behaviour is different.
your LoadForm use any events or delegates of objects not created inside.
Those are references too. As long as you use ShowDialog I suppose the answer
is *NO*.
Your answer is right. I am not using any externally created objects. I
am using a toolbar, imagelist and various events in my form but all
are created inside and none of them is static. I am not disposing any
of them before closing the form because I am disposing and setting the
form itself to null.
Are you sure this very form eats you memory? It could be in other parts of
the application.
Yes Sir, because this is the only form my end-users use in the 24/7
application. All other forms are administrative and are used by
management people very rarely. The PC with end-users gets hung after
every 3-4 hours. Other PCs are working OK.
Try to do the following. Open the form and take the snapshot of the memory
usage. Then close the form set the referece to null and call
System.GC.Collect() and then System.GC.WaitForPendingFinalizers() and look
at the memory usage again. Try to find out if all objects of the form and
the form itself get collected.
I did this and found many graphic objects and strings created but
never released. It gets accumulated every time I call the form and
close it.

Thanks for the reply.
Regds
-Manoj
 

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

PLS HELP!!! MDI application Closing event 5
Problem (dispose?) MDI forms 16
Is it possible ? 2
Weird Form Dispose Error 3
Memory not released 4
Form.showicon doesn't work? 1
MDI Child Form Problem 1
Memory Leak 5

Top