views:

20

answers:

1

I have a form with label that is updating by System.Windows.Forms.Timer control every 2 seconds. In task manager i see that memory usage is growing even if program is doing nothing(but label is still updating with latest info, that is memory usage for example) Example code for label text:

tlblRam.Text = string.Format("Ram: {0} MB", ConvertBytesToMegabytes(System.Diagnostics.Process.GetCurrentProcess().PrivateMemorySize64).ToString("0.00"));

But if i do force GC to collect the generations- it returns all the memory to some lower state. Also it is done automatically after ~5 minutes of inactive program's work. Does that mean that GC is waiting for its 2 gen cycle and only then collects all the strings that were tlblRam.Text's values?

+1  A: 

Does that mean that GC is waiting for its 2 gen cycle and only then collects all the strings that were tlblRam.Text's values?

Basically, Yes. There will probably be some more things claiming and releasing memory in a running Windows app.

More important: Don't worry, there is no problem here. Don't try to 'help' by starting y=the GC manually, you'll only make it worse.

Henk Holterman
Thank you for the answer. So 10-20Mb of garbage is normal in this case if its released after some time? The strange thing for me here is that it seems that old label values are marked as garbage, but it takes so much time to release the memory they take, tho strings are immutable and there is no sense in keeping "old" values from heap. Tho it seems .net works this way if the machine, where the program is started, has enough memory.
nihi_l_ist
You can't tell (from TaskMgr or Process.PrivateMemorySize64) what is happening to the strings. You don't want the GC to run to frequent, there is a fine tuned algorithm regulating this for you.
Henk Holterman