T
ThomasW
Hello,
we have a WinCE memory leak problem with class UserControl in .NET CF 2.0:
Creating an UserControl instance without using the instance leads obviously
to a memory leak.
In our example source code we call the constructor of class UserControl, but
we do not use its result (the reference to the instance). So, the instance
becomes immediately available for the garbage collector.
Of course, GC does not collect at once, but in our example GC never collects
any UserControl instance. This has been approved also by Microsoft's Remote
Performance Monitor. Instead, the amount of used memory increases and
eventually we get an OutOfMemoryException (after about 20,000 allocations).
GC: 680
GC: 1163048
GC: 2323048
GC: 3483048
GC: 4643048
....
OutOfMemory Exception
Testing the code on Windows with .NET 2.0 we get the expected result: no
memory is leaking at all.
GC: 381764
GC: 392864
GC: 392864
GC: 392864
GC: 392864
....
OK.
So, the question is: What's the reason for the different behaviours? Is it a
..NET CF bug not to release the memory allocated by the UserControl instances?
Thanks in advance
Thomas
----------------------------------------------------
Source code of our example:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace TestUserControlCe
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("GC: " + GC.GetTotalMemory(true));
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5000; j++)
{
new UserControl();
}
Console.WriteLine("GC: " + GC.GetTotalMemory(true));
}
Console.WriteLine("OK.");
}
}
}
we have a WinCE memory leak problem with class UserControl in .NET CF 2.0:
Creating an UserControl instance without using the instance leads obviously
to a memory leak.
In our example source code we call the constructor of class UserControl, but
we do not use its result (the reference to the instance). So, the instance
becomes immediately available for the garbage collector.
Of course, GC does not collect at once, but in our example GC never collects
any UserControl instance. This has been approved also by Microsoft's Remote
Performance Monitor. Instead, the amount of used memory increases and
eventually we get an OutOfMemoryException (after about 20,000 allocations).
GC: 680
GC: 1163048
GC: 2323048
GC: 3483048
GC: 4643048
....
OutOfMemory Exception
Testing the code on Windows with .NET 2.0 we get the expected result: no
memory is leaking at all.
GC: 381764
GC: 392864
GC: 392864
GC: 392864
GC: 392864
....
OK.
So, the question is: What's the reason for the different behaviours? Is it a
..NET CF bug not to release the memory allocated by the UserControl instances?
Thanks in advance
Thomas
----------------------------------------------------
Source code of our example:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace TestUserControlCe
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("GC: " + GC.GetTotalMemory(true));
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5000; j++)
{
new UserControl();
}
Console.WriteLine("GC: " + GC.GetTotalMemory(true));
}
Console.WriteLine("OK.");
}
}
}