Main Site Documentation

Plugging the Memory Leak


On the desktop when I am looking for memory leaks, I profile the program to find them.

Is there a tool/strategy that is used to find memory leaks in a .netmf application?


As a first step I would try regular GC output to check what kind of memory is leaking.

Then I will look where that particular memory is allocated.


Actually, I’ve enabled that but I don’t see any GC messages.

When I get the exception I am able to call Debug.GC(true) to free some memory. This allows the program to continue. Could the GC thread not be running?


@ Mr. John Smith - I have found that since .NET MF 4.2, the EnableGCMessages method (which was the only method that gave me any detailed memory use insight), is disabled in the firmware (at least for Cerb-family). I actually figured out how to re-enable it about 6 months ago, but I had a disk failure and lost that work. I would NOT do GC.Debug(true). If you have a real leak, this will temporarily hide the problem. The Garbage Collector is always running automatically, and there is nothing you need to do to invoke it. I would do Debug.GC(false) to print the amount of ram remaining and scatter that throughout your code. Look for static collections first. Then look at the lines called when it runs out of memory. Usually those are prime suspects. If you need to use the EnableGCMessages method, you’re going to have to build your own firmware and search for the EnableGCMessages method. Then follow the caller until you see an option. I just hardcoded it. Although there may be a setting in the firmware to enable it, but I never found it.

I thought I’d posted a write-up about it, but for the life of me, I cannot find it anywhere on the internets…



@ Valkyrie-MT - I think I found the post and it was not very instructive:


@ Mr. John Smith - Are you using an InterruptPort by chance? I ask because I believe there is a memory leak in that class. I switched to polling an InputPort, which is ugly, but works fine for me.


@ Valkyrie, I am using InteruptPort actually, thanks for the head’s up.

I found my memory leak in one of my collections; sad but true. So I’ve put in hard limits to how many items it can hold.

I’ll have to watch for memory leaks in InterruptPort as per your advice.