Memory Problem. Memory freed only if external program launched ???

P

Palm Kevin

I discovered a strange behaviour.
When I execute the following code :

StringBuilder res = new StringBuilder(6000000);
res.Remove(0,res.Length);
for(int i = 0; i < 500;i++)
res.Append("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
3456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012
4567890");

then memory is allocated on the Pocket PC.
And it is not freed!
Even not if I launch the Garbage Collector programmatically.

I found out that the only way to free the memory is to launch an
external
programm. If you launch any other programm, than the memory is freed.

I find the behaviou very strange!

Has somebody an explication for this?
 
P

Peter Foot [MVP]

calling GC.Collect() will not automatically free the memory, unless it is
actually required by the system - hence when you run another app and it
requires some of this free memory the Garbage Collector will release some.
This is normal behaviour, normally you won't need to call GC.Collect
manually as this can be detrimental to performance and you should leave the
garbage collector to just do it's thing.

Peter
 
P

Palm Kevin

OK, but I NEED to run instructions like these several times!
And between them I must free Memory!!

Ich the Garbage Collector doesn't do this!?!
How can I do it???
 
G

Guest

The point is that you don't need to. The garbage collector will free the memory as needed. If you try to run one of these and it needs to free up some memory to do it, it will free it automatically for you.
 
C

Chris Tacke, eMVP

Once again I'm going to say that if you aren't getting out of memory
exceptions, don't worry about it. The GC has been fully optimised and it
does it's own thing. Quit worrying about trying to profile or optimize it.
You don't have the ability to control it, and in most cases, any attempt to
control the GC will result in worse perfromance than if you let it be.
 
P

Palm Kevin

But I get an OutOfMemoryException, because I have to create big objects
in memory.

After I created and used an object, I set it to null, and the next big
object can be created.

But there is no more free memory, because the GC still has not freed the
memory used by the previous object.
Do you understand now my problem?
 

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