Memory Leak information

I recently have had some problems with my gadgeteer device in that it just stops after running fine for X number of minutes. As I am a full sized .Net software guy that is relatively new to the Micro framework space I posted a question asking about if this could be a memory leak. After some responses I have come to the conclusion that perhaps I as well as others do not have a good set of tools and practices for handling memory at the micro framework level.

I am hoping that some of the Gurus around here could give short suggestions about best practices for coding in the micro world that may be different or at least a lot more important than in the Macro framework world. I am also hoping for tips or tricks in how to approach finding and curing typical Micro framework problems that may be different that in the full sized world.

So please take a minute or 2 and jot down some things that you do differently in micro land.

thanks in advance


You can enable GC output using Debug.GC.EnableMessages(true)… or something similar. Then you can monitor those to see if it is memory leak or GC has collected an object you are still using.

The major difference is you want to minimize the creation and deletion of objects. This results in
longer garbage collection times, which impacts performance.

The other difference is you have less memory with MF, and have to consider this in your
program design.

Actually, I have found that the EnableMessages method broke in .NET MF 4.2. I have not seen it work for some time. First thing I would suggest is make sure you don’t have any static collection objects (like an ArrayList). Those can grow quickly and will never be garbage collected. But, reusing static variables can save on memory, but be careful. Also, try to avoid overly using inheritance and classes to allow flexibility that you will never need – keep it simple. You have a very limited amount of space and there is no extra room for fancy code.

The only way I know of to find memory leaks is to use Debug.GC.Collect(false). This command returns the amount of free RAM. if that number gets below 5000 bytes, an Out of Memory exception is likely imminent.

@ valkyrie are you saying don’t use ArrayLists or don’t use Static ArrayLists?

Also, are you saying that you should cut and paste code not use inheritance? Please could you give an example of “Fancy” code and what you would do to make it simpler Micro framework code?


Collections can be safely used, if you understand how they work. A collection usually has a Capacity property, which should be set to what you think is the maximum size it will grow. This will stop the collections from repeatedly increasing its internal storage. Of course, the internal storage of the collection will remain at the Capacity value regardless of it contents.

I am not sure about the statement about static collection objects not getting garbage collected. If you remove an object from a collection, and no longer have a reference to it, it should be GC’d.

With C#, there are really no memory leaks, as there are in C or C++.

On a PC, with lots of processor power and memory, we often do not think about the
resource impact of how we code. With MF we need to think about how we code, and
what will be the impact of an operation.

If you have done any high performance propgramming on a PC, the techniques used apply to MF.

@ valkyrie
What do I need to reference for : Debug.GC.Collect(false) ?

GC class not implemented in MF.

Check Debug Members | Microsoft Learn for members of Debug class, which provides memory info.

Debug.GC(false) returns available memory. If parameter set to true, forces GC.