M
Martin Law
Hi all,
I am using both "perfmon" and GC.GetTotalMemory() to keep track of the heap
memory space usage of the following simple program. Both ways suggest that
the garbage collector cannot reclaim all "dead" memory. Any idea?
using System;
using System.IO;
namespace test_leak
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void t0()
{
System.Collections.ArrayList ar = new System.Collections.ArrayList();
for (int i=0; i<1000; i++) ar.Add(i);
// GC.Collect();
//Console.WriteLine("6 Memory used now is {0}", GC.GetTotalMemory(true));
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int i = 0;
GC.Collect();
Console.WriteLine("Memory used now is {0}", GC.GetTotalMemory(true));
t0();
System.GC.WaitForPendingFinalizers();
GC.Collect();
i ++;
GC.Collect();
//Console.WriteLine("************************************");
Console.WriteLine("Memory used now is {0}", GC.GetTotalMemory(true));
return;
}
}
}
and the output is
Memory used now is 34924
Memory used now is 35308
So some memory is lost. :-(
While this sounds small, this can really add up. Sigh....
Can anybody give me a hand? Many thanks!
I am using both "perfmon" and GC.GetTotalMemory() to keep track of the heap
memory space usage of the following simple program. Both ways suggest that
the garbage collector cannot reclaim all "dead" memory. Any idea?
using System;
using System.IO;
namespace test_leak
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void t0()
{
System.Collections.ArrayList ar = new System.Collections.ArrayList();
for (int i=0; i<1000; i++) ar.Add(i);
// GC.Collect();
//Console.WriteLine("6 Memory used now is {0}", GC.GetTotalMemory(true));
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int i = 0;
GC.Collect();
Console.WriteLine("Memory used now is {0}", GC.GetTotalMemory(true));
t0();
System.GC.WaitForPendingFinalizers();
GC.Collect();
i ++;
GC.Collect();
//Console.WriteLine("************************************");
Console.WriteLine("Memory used now is {0}", GC.GetTotalMemory(true));
return;
}
}
}
and the output is
Memory used now is 34924
Memory used now is 35308
So some memory is lost. :-(
While this sounds small, this can really add up. Sigh....
Can anybody give me a hand? Many thanks!