Main Site Documentation

How to handle OutOfMemoryException?


Is it possible to catch OutOfMemoryException, make some cleaning (especially Debug.GC(true)) and re-run function?

In other words is there any way to survive OutOfMemoryException?

Watchdog thread is still running correctly but other thread is dead.

And on the other hand are there any tips to avoid it (OutOfMemoryException) besides using/Dispose and Debug.GC(true) mechanizms?




remember to think small. The reason GC is running or that GC can’t keep up with your memory loss is that you’re not handling things well. You don’t have the resources that you do on a PC or on something with more memory, you have to be much more frugal with memory.

There are a couple of common areas people are dealing with when they get into this kind of question, it’s either large string handling, serial ports, or large files. its worth telling us what you’re working on so that we can also suggest other approaches.



thanks for reply.

I must tell You that I think small, I am using:
-StringBuilder with precised buffer to build String
-I am using streams to read html site from SD card (with small buffers to read it)
-Streams to read and write to sockets

But still using Panda 2 for home automation including WWW server for controlling, SD logger for logging events I sometimes have OutOfMemoryException and I would like to handle it properly, still trying very much to avoid it.


Hello Jack,

I worked for weeks to make a simple and yet stable http server for the smaller fezs using the Wiz5100 chip and the httplistener class. Those seems at first to be very simple to use, but it is very easy to leak memory from a little ererywhere. It took me weeks to make it stable. Now it can work for weeks without leaking a single memory byte. I encourage you to try my code or at least get inspired by it…

and a first plugin:

Have fun :wink: