Memory leak using UserControl

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.");
}
}
}
 
C

Chris Tacke, MVP

Well the obvious answer is "don't do that". I mean in the context of
reality, what is the problem? You certainly don't just have a loop that
creates 20k controls in your app. We have no idea what your control is
doing, allocationg, etc. That might keep a root alive. Does CF 3.5 exhibit
the same behavior? Why cn't you call Dispose?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
T

ThomasW

Hi Chris,

thanks for your fast response!

We have an CF application of 8.5MB (exe and dll) code. Because of our
experience in programming with .NET Framework we decided to use .NET CF
instead of program it nativly on WinCE 6.0 (especially the GC instead of
deallocating the memory manually).

We have no problem that .NET CF is a subset of .NET. But it would be a
problem when the subset interface seems to be the same but the behavior is
different.

We have a memory leak in our application. And I "reduced" the problem (which
takes really a lot of time) to the core in the example. We don't use any of
our classes in the example. We only use Microsoft's core classes.

If the GC works differently on .NET CF than it might be useful to document
this in the MSDN!? Do you think that this behavior is a bug?


Greetings from Germany

Thomas
 
C

Chris Tacke, MVP

I don't know if it's a bug or not, but the GC certainly behaves differently.
It's a simple mark-and-sweep GC, not generational, so it's quite different.
There is plenty of info in MSDN and the CF team blogs on how the CF GC
works.

If you have a leak in your app, a profiler might help. And again, if a
class exposes a Dispose method, the general rule, even on the desktop, is
that when you're done with the object you should call Dispose. Sure the GC
*should* be able to handle it for you, but why make it guess?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
L

Lcubed

Although I don't think our app has any memory leaks, I decided to
profile it for good practice.

However, when I ran the CLR Profiler that comes with Power Toys, it
connected, started the app and then popped up a window with the
error : "Unable to connect for 45 seconds, please verify the remote
device is connected"

I'm using the .NET CF Profiler came with the .NET CF 3.5 Power Toys on
a real device that has .NETCF 3.5 installed on it.

Any ideas how to fix it?

Thanks!
 
L

Lcubed

Any ideas?

The .NETCF Remote Performance Monitor works, but if I click on "GC
Heap", an hour glass cursor appears, nothing else happens and the hour
glass cursor remains until I restart the monitor application.

Has anyone else had these issues? It would be great to be able to use
these tools.

Thanks!
 

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