Is there somewrong with CG?

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

I'm try to check how CG free up memory in intesive
allocation situation. But it seems that after an
collection, the memory is a bit larger than last time.

For example, from I run the code below for 100,000 cycle
after the first collection the size is '28,228'
after the last collection the size is '149,880'

This continue increase of memory worries me.
Do you know why this happens?

Thanks.



// The main flow ... ---------------------------

for(int i=0; i<100000; i++)
{
cv = new CV( 1024 );
cv = null;
Console.WriteLine( System.GC.GetTotalMemory(false ) );
}


// CV is defined as ... -----------------------
class CV
{
public CV( int n )
{
list = new byte[n];
}
public byte[] list;
}
 
Peter said:
I'm try to check how CG free up memory in intesive
allocation situation. But it seems that after an
collection, the memory is a bit larger than last time.

For example, from I run the code below for 100,000 cycle
after the first collection the size is '28,228'
after the last collection the size is '149,880'

This continue increase of memory worries me.
Do you know why this happens?

You're giving false as the parameter to GetTotalMemory, which means it
*isn't* doing a garbage collection before reporting back. Change that
parameter to true and see if it helps.

Note that you don't need the line saying

cv = null;
 
// The main flow ... ---------------------------
for(int i=0; i<100000; i++)
{
cv = new CV( 1024 );
cv = null;
Console.WriteLine( System.GC.GetTotalMemory(false ) );

try Console.WriteLine( System.GC.GetTotalMemory(true) );

Make it wait until the garbage collection finishes before printing the total
memory.

/m

}


// CV is defined as ... -----------------------
class CV
{
public CV( int n )
{
list = new byte[n];
}
public byte[] list;
}
 
Back
Top