View memory available on nanoFramework Esp32
Once I started on the C# journey it really is hard to stop... so here I am again showcasing one of the most amazing finds in my micro controller development career, nanoFramework. This bad boy allows you to write C# code for low-level micro controllers, specifically an ESP32 in my case.
As my journey continues one of the main issues I have is keeping track of memory, I normally don't care about it when developing lets say a Asp.net Core Web API application, but when dealing with ESP32 controllers with KB of memory, it makes a huge difference managing memory!
I didn't find much online in how to do this, but upon browsing discord and getting some feedback from other users (thanks @AlbertK) I have this little snippet!
using nanoFramework.Hardware.Esp32;
using System.Diagnostics;
namespace Project.Helpers
{
public class Diagnostics
{
public static void PrintMemory(string msg)
{
NativeMemory.GetMemoryInfo(NativeMemory.MemoryType.Internal, out uint totalSize, out uint totalFree, out uint largestFree);
Debug.WriteLine($"{msg} -> Internal Mem: Total Internal: {totalSize} Free: {totalFree} Largest: {largestFree}");
Debug.WriteLine($"nF Mem: {nanoFramework.Runtime.Native.GC.Run(false)}");
}
}
}
So the main one you care about is the nF Mem, this is going to tell you how much memory is available to your nanoFramework application. I personally have a heartbeat service that runs every 15 seconds toggling the internal LED and printing out this call so I know how much memory my application is consuming!