c# compiler mem"leakage"



Lets consider following code:

while (true)
{
string[] t = new String[1024 * 1024 * 20];
}

You probably thing this is the good solution because "narrow scope of variable to the minimum". But wait. Mem usage is at level of 300MiB!

Now. Lets see what happens if we use:

string[] t;
while (true)
{
t = null;
t = new String[1024 * 1024 * 20];
}
Wow! Now mem is something at 30MiB. Who could know that?
So this is either compiler bug or framework's thing.

By the way: Add GC.Collect() for your VS not to hang up for a moment when you stop or pause. Do it like that:
string[] t;
while (true)
{
t = null;
GC.Collect();
t = new String[1024 * 1024 * 20];
}
.



Relevant Pages

  • Re: Byte Array OutOfMemory
    ... dann waren im String str bereits 450 Mio Zeichen - und da String Unicode unterstützt, ... wieder OutOf Mem. ... Die Datei ist immer eine Binäre mit Fixer Länge und Gruppen und Zeilen Delimiter, ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: A little ASM 6809 program
    ... CMPA MEM; is it smaller the the current min value ... yes, store new min value ... and the pointer to the byte in the string ...
    (alt.lang.asm)
  • Re: A little ASM 6809 program
    ... CMPA MEM; is it smaller the the current min value ... yes, store new min value ... and the pointer to the byte in the string ...
    (alt.lang.asm)
  • Re: remove regex matched line question
    ... >> You're not rejecting anything complicated, so it is just as easy to ... > efficient than reading into a single String: ... Muscle memory. ... String mem when you have all in one String. ...
    (comp.lang.ruby)
  • RE: Date not coming up!
    ... It will just be seen as a string. ... Dave Hargis, Microsoft Access MVP ... "mem" wrote: ... Dim FName As String ...
    (microsoft.public.access.queries)

Loading