Memory not reclaimed on modeless forms

P

PGP

Sorry i asked the same question yesterday but did a bad job on describing
the issue so here is a better description.

This is in .NET Compact Framework.

I have a form which creates modeless forms using code like

Form frm = new Form1() ;
frm.Show() ;

These modeless forms get closed and i can see their corresponding Dispose
getting called. It seems that the memory allocated for these forms are never
collected after they are closed. This causes the application to run slower
and slower as more forms are opened and closed.

What do i do to make sure that the memory allocated by these forms are
reclaimed when they are closed?

Thanks in advance.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

PGP said:
Sorry i asked the same question yesterday but did a bad job on describing
the issue so here is a better description.

This is in .NET Compact Framework.

I have a form which creates modeless forms using code like

Form frm = new Form1() ;
frm.Show() ;

These modeless forms get closed and i can see their corresponding Dispose
getting called. It seems that the memory allocated for these forms are
never collected after they are closed. This causes the application to run
slower and slower as more forms are opened and closed.

Remember that the GC runs in the background and at a not precise interval so
it's not uncommon to see the memory use to increase.
This in no way would make the app to run slower though.

I think that you may have another problem with the form, what is this form
doing? Do you use any 3rd party component there? Do you keep a reference to
the form (or to any object you create in the form) ina collection outside
the form?

What if you remove ALL the functionality of the forms and just create
/destroy it, do you see the same issue?

Also install the SP1 of the framework
 
P

PGP

Remember that the GC runs in the background and at a not precise interval
so it's not uncommon to see the memory use to increase.
This in no way would make the app to run slower though.

I think that you may have another problem with the form, what is this form
doing? Do you use any 3rd party component there? Do you keep a reference
to the form (or to any object you create in the form) ina collection
outside the form?

What if you remove ALL the functionality of the forms and just create
/destroy it, do you see the same issue?

Also install the SP1 of the framework


--

Ignacio.

I created samples to test this scenario. There are no outside collections
refering to the form in question. If the form is created and destroyed in
the same function, memory seems to be steady but finalize is not called on
the form even after a GC.Collect call. I understand there is no need to
force the collect, but this was done just for testing purposes to draw some
conclusions.

If i create a panel in a form, create a sub form and add the sub form to the
panel, then on a button press or something remove the form from the panel's
collection and do a GC.Collect, my finalizers are hit correctly. But if i
close the form using the form's top right close button, dispose is called
but not finalize. In the actual application i am trying to debug, users
close sub forms using the X button.
 
A

Alan Pretre

PGP said:
Sorry i asked the same question yesterday but did a bad job on describing
These modeless forms get closed and i can see their corresponding Dispose
getting called. It seems that the memory allocated for these forms are
never collected after they are closed. This causes the application to run
slower and slower as more forms are opened and closed.

What do i do to make sure that the memory allocated by these forms are
reclaimed when they are closed?

Years ago I got into the habit of putting

base.Dispose();

in the Closed event of my modeless forms. It has been a long time since I
have investigated this issue, but IIRC this was the only way I could be sure
that Dispose() was called in a timely manner.

The actual situation today may be different than I have described. As I
say, it has been years since I have looked into it, probably 4 years.

-- Alan
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
Ignacio.

I created samples to test this scenario. There are no outside collections
refering to the form in question. If the form is created and destroyed in
the same function, memory seems to be steady but finalize is not called on
the form even after a GC.Collect call. I understand there is no need to
force the collect, but this was done just for testing purposes to draw
some conclusions.

IMO You should let GC run alone, MS has expend a lot of resources to make it
run efficientely, so unless you really know what you are doing and why you
are doing it do not touch it.

I have an application that does run on a PPC for days and shows/destroy
forms on a regular basis and I have no problem at all with the memory.
If i create a panel in a form, create a sub form and add the sub form to
the panel,

what you mean with creating a subform inside the panl ?

Here is where the problem may be.
Why dont just add the controls that currently host the form to the panel
directly?
 
P

PGP

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,


IMO You should let GC run alone, MS has expend a lot of resources to make
it run efficientely, so unless you really know what you are doing and why
you are doing it do not touch it.

I have an application that does run on a PPC for days and shows/destroy
forms on a regular basis and I have no problem at all with the memory.


what you mean with creating a subform inside the panl ?

Here is where the problem may be.
Why dont just add the controls that currently host the form to the panel
directly?


--
Ignacio,

In the original issue, there was a toolbar object which held on to a
reference to the modeless form. This toolbar object was alive throughout the
application. Appropriate dispose and ref = null on form closed takes care of
the issue in actual form.

I also had to make sure that the maximizebox = minimizebox = false on the
modeless form so that it will display an "ok" on the top left rather than an
"X". Why do i have to do this? Does the "X" mean that the form will not be
closed ?
 

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

Top