Dispose doesn't free memory

M

Memory Issues

In an MDI app after the first internal screen is launched,
used, and then closed, it doesn't return the memory.
I have noticed that if you open the same form again the
memory stays about the same and doesn't reallocate another
10-25 megs depending on whats on the form.

Is this normal?

I get the same results with two regular forms, the first
one having a button to launch form 2, and form 2 having a
large image on it (to make the memory difference
significant) but if you watch the task manager most of the
time when you close the second form, the memory actually
goes up a little.

I also tried some tests with vb6 in the same manner as the
paragraph above and it frees up memory 99% of the time
when the second form is closed.

When using .Net I have closed, disposed, nulled the form
references, tried initializing the form variable only
inside a sub, public/private to the class level and just
about everything else I can think of and C# hold onto it
like a bull dog only occassionally releasing 2000k.

I have also tried the same procedures in VB.NET, and
J#.NET and I get exactly the same results.

Can anyone give me an idea of whats up?

One other point: I have testing excel with adding and
removing worksheets. For instance on work sheet 2 I will
add many pictures/shapes, etc.
I watch the memory in task manager and when I delete the
sheet with lots items on it the memory stays the same or
goes up a bit.

And yet I at random I tested a digital camera software app
called camedia Master [MDI app] and it release memory just
fine (I am assuming it was not written in .net).

Thank for Assistance. I am writing the C# app to run in a
citrix environment that has to support many users so the
memory has to stay low on the servers. At first I thought
it might be a citrix issue but my own W2000 Server
responds the same way.
 
I

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

When using .Net I have closed, disposed, nulled the form
references, tried initializing the form variable only
inside a sub, public/private to the class level and just
about everything else I can think of and C# hold onto it
like a bull dog only occassionally releasing 2000k.

I have also tried the same procedures in VB.NET, and
J#.NET and I get exactly the same results.

Can anyone give me an idea of whats up?

Put this call right after you set the variable to null:
GC.Collect();

It force the garbage collector to reclaim all the memory that is
innaccesible.

If not make sure that you are not holding a reference to those objects
elsewhere in the system.

IF this answer your question, take a look at the GC documentation in MSDN


Cheers,
 
T

Telmo Sampaio

Just remember that directly calling GC will cause it to crawl through the
whole heap, reclaiming memory for all unrefeenced ojecs for all apps that
are runing. This can be time consumig when you user has multiple .NET apps
runing. MS even recommends not calling the GC directly.
 
I

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

Hi,

Yes, that's true , but we are trying to find why the memory is not freed
back.

Cheers,
 
T

Telmo Sampaio

TRUE! :)
I think it is the regular issue I find with C programmers... they are so
used to allocating/deallocating memory that they do not get used to Garbage
Collection.
 
D

Doug Hagerman

I tried the method below and the memory still went up.
Even after leaving it for several minutes the task manager still showed
the memory at the same higher level.

static void Main()
{
Application.Run(new Form1());
}

private void btnOpenForm_Click(object sender, System.EventArgs e)
{
frmNextForm f=new frmNextForm();
f.ShowDialog(); //or f.Show();
// and dispose and such
//in a different routine
f.Dispose();
f=null;
System.GC.Collect();
//GC.WaitForPendingFinalizers();
}

//-------------------------
I have also tried

frmNextForm f=null;

private void btnOpenForm_Click(object sender, System.EventArgs e)
{
f=new frmNextForm();
f.ShowDialog(); //or f.Show();
// and dispose and such
//in a different routine
f.Dispose();
f=null;
System.GC.Collect();
//GC.WaitForPendingFinalizers();
}

Anymore thoughts on what to do?

I am looking into the MSDN.

The Bull Dog is still winning.
 
Y

Yura2000

Hi,
Check all references that your form has, all unmanged
resources , like open files, connections to DB and etc.
Remember, that CLR will take decision by himself, when to
free memory.
Free memory by GC.Collect() is not a good approach .
One more point: CLR does not need to free memory
immediately when you fihishing to use any resource.
This may be a reason for different behavior in VB6.

Regards
 

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


Top