Forced garbage collection - only in Debug not Release?

I’ve a simple method to write bytes to a file on an SD card:

public void WriteBytes(string filename, byte[] bytes, int offset = 0, int count = -1, FileMode mode = FileMode.Create)
    count = (count >= 0) ? count : bytes.Length;
    using (var fs = File.Open(GetPath(filename), mode, FileAccess.Write))
        fs.Write(bytes, offset, count);

Because I have very little memory to play with I’ve added a forced garbage collection to the write method. Am I correct in thinking that this method only works in ‘debug’ and not in ‘release’?

Are you saying that because it is part of the Debug class? If so, I would not make that assumption. In fact, the Debug.Print is not even optimized out of Release mode. There are two ways to know. Put a print statement showing the free RAM before and one after the Debug.GC(true). Or check the firmware source.

1 Like

Also, when you connect to your board with MFDeploy, you can see the GC Output, even in release mode.

Thanks both - I indeed thought that the Debug class methods would not run in ‘release’. On this occasion I am glad to be wrong :slight_smile: