garbage col. vs manual cleaning

S

Sput

I have a program with many forms which users are often opening and closing.

Now, I have noticed that after opening 10 different forms and the closing
them, memory consumption increases (eg. it has 25mb before opening 10 forms,
then while they are open it has 26mb, after closing it has 25.8mb)

After user is working for several hours, mem. usage jumpes to 70-100mb.

Should I manually clean up after form closes or does the garbage collector
clean everything?
(ie there is a lot of graphics loaded from outside on forms and maybe gc
does not dispose of them - entirely?)
 
C

Chad Z. Hower aka Kudzu

Sput said:
Should I manually clean up after form closes or does the garbage
collector clean everything?
(ie there is a lot of graphics loaded from outside on forms and maybe gc
does not dispose of them - entirely?)

There is no way to manaully clean up. Just make sure all your references are
set to null, or are dereferenced. The GC will take care of the rest.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
D

Daniel O'Connell

Chad Z. Hower aka Kudzu said:
There is no way to manaully clean up. Just make sure all your references are
set to null, or are dereferenced. The GC will take care of the rest.
Well, depending on what you are using, you really should be calling Dispose
on objects like Graphics and Image, that likely isn't the cause of the
memory issue however. You need to make sure you aren't maintaining
references to objects you no longer need(probably Image or Graphics
classes), a memory profiler will help here.
 
M

Manish Singh

Sput said:
I have a program with many forms which users are often opening and closing.

Now, I have noticed that after opening 10 different forms and the closing
them, memory consumption increases (eg. it has 25mb before opening 10 forms,
then while they are open it has 26mb, after closing it has 25.8mb)

After user is working for several hours, mem. usage jumpes to 70-100mb.

Should I manually clean up after form closes or does the garbage collector
clean everything?
(ie there is a lot of graphics loaded from outside on forms and maybe gc
does not dispose of them - entirely?)

Garbage collector won't collect memory till it doesn't feel the need
of it. Internally, how that feel is decided, We don't know. If you
have lot's of ram and few programs running, it may not tend to free up
resources.

Few points:

- Just check if you are using destructurs in your forms or classes
Internally they get converted to Finalize method, and such classes
gets promoted to longer generation, remaining in memory for more time.
If it is so, try overriding Dispose method also, and call it when you
are done with resources.

- You can manually call GC.
As you are working with manual interaction, you must have plenty of
time, when much is not being done and you call GC as static method
System.GC.Collect();
System.GC.WaitForPendingFinalizers();

Have a nice day
Manish Singh
 
N

Nicholas Paldino [.NET/C# MVP]

Daniel, Chad and Sput,

When closing the form, you should be calling Dispose on the Form. This
should call dispose on the Form, and any control references it contains.
While you might not see a difference in the memory consumption (because the
object representation still might be around in .NET), you will properly
dispose of window handles that are no longer being used.

Hope this helps.
 

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