This is a generic .Net question - I never understood this.
I have a winform with one button on it.
The form has NO global variables.
When I start the application, it takes 22 Mb, but the I click the button. This starts a real heavy string routine.
All data is local in this routine; strings, lists and integers, so when the routine ends, all varables are out of scope and should be garbage collected. I should expect a memory usage of 22Mb again.
But reality is somewhere between 300 and 700Mb.
Running a forced GC.Collect won’t free the memory.
Edit: The result is written to disk, not stored anywhere.
How is this possible?
Could be, but seems unlikely. Also note though that maybe the process just holds on to memory it already allocated “because it can”, w/o that meaning the memory is not actually available (by your app to reuse, or to be freed fully when other recesses need it and there’s shortage. Having “free” memory is not really a goal, on modern OSs, optimal usage of all available memory is.
Only proper profiling will show you real memory problems — not going by what TaskMan shows.
String constants are always interned, and interned strings are never released.It is faster, but cost a lot of memory. I use a string builder (with preallocated memory - speed and peak memory usage is no problem).
But I see the problem. I do the GC Collect at the end of the routine, where the variables are not yet out of scope. If I create an extra calling function - that make the variables go out of scope, GC.Collect(3) is enough to get the memory back (at least to 47 Mb, started at 22 Mb, so there is still 25Mb gone) - but is stays on 47 after 10 times running.