I’m trying to use freeMemory to keep track of what I have free, and also to catch leaks and bugs. I’ve noticed however, that after allocating an array of objects, the memory only changes in specific places, and also that after deleting that array, the memory reported by freeMemory doesn’t change. I’m not sure If I can actually trust this method.
Description from Firmware Docs:
Retrieves the amount of memory guaranteed to be available. The actual amount of free memory will be at least as large as the value returned.
I noticed that the number given by freeMemory() never actually increases. And that, in my initial allocation, memory decreases regularly, and then in subsequent allocations, it appears to stay the same. This is leading me to believe that freeMemory() operates on some sort of counter, where it just decrements for allocated memory and never actually increases. It just tells you how much memory you’ve Never used, not how much memory you’re currently not using.
when you compile the program the compiler allocates memory used for an an array, as in it sets aside that amount of memory that WILL be used by the array, even if you delete the contents of the array that amount of memory is STILL set aside to store that array.
Just like it says in the docs - the amount of free memory may be more. This is because of how the memory allocator works, the free memory value can decrease, but never increase, even though there is more free memory.
We’d have to walk the heap to find all the objects in use and subtract that from the current heap size. This involves digging into the internals of ARM gcc malloc implementation than there is time for.