Debug.Print causing out of memory

I have a simple application running on a Cerbuino Bee that is decoding a serial telegram from a Gyro Compass and then outputting a Modbus message on another serial port.

I have 1 debug output as part of the main loop that show the gyro heading as a raw value (12 bit)



When I use a Gyro Simulator that I developed on the PC, it works find and the debug output shows the raw value as expected. Data input from the gyro is every 20ms at 9600 bps and 18 characters per message.

If I now hook up the real gyro I get an out of memory exception. If I comment out the above debug line, it now runs with no exceptions. The LED on the board is now flashing as expected.

Could the Debug.Print cause these out of memory issues?

One last thing, the deployed size is 34K and I am only using integer variables.

Sounds more likely to me that its the heading.ToString() that’s the problem. I can’t imagine a Debug.Print() problem like this going unnoticed for so long. You might try replacing heading.ToString() with just a constant string value and see if it still runs out of memory.

I think the .ToString() funktion allocate each time a new string.

Here you can find one solution:
http://www.dotnetperls.com/tostring-integer-optimization