System.freeMemory() not working?

above setup() I have :- uint32_t freemem;
in setup() I have :- Spark.variable(“free” , freemem);
and this which gets called once per second :- freemem = System.freeMemory();

Which says 54284 , now the strange bit !!!
I add a extra big int arrary :- int store{1000}; and get guess what 54284

@peter_a, can you share your code so we can have a better look?

#include "application.h"

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(AUTOMATIC);
//STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));

int store[1000];
int led1 = D7;
uint32_t freemem;


void setup()
{
  pinMode(led1,OUTPUT);
  Spark.variable("free" , freemem);
}

void loop()
{
  digitalWrite(led1, HIGH);
  delay(500);
  digitalWrite(led1, LOW);
  delay(500);
  freemem = System.freeMemory();
}

That should show 54760 with or without the large arrary

AND i`m using DEV not web ide build .
But the #include “application.h” may give that away.

If you’re not using the array for anything the compiler may optimize it out.

Do you get a warning that tells you something like "variable store[] declared but never used"
Try writing to the store array in your main loop or declaring it volatile.

1 Like

@peter_a, this may be a silly recommendation but can you try declaring freemem as an int instead in uint32_t?

@jakeypoo , Spot on !!! , I added a couple of line which used the arrary and it went down to 46732 , which I worked out .
2000 arrary x 4 bytes for a int , so 8000 bytes on top of 46732 = 28 bytes short which was my extra code .

@peekay123 , I replaced my int with the uint32_t from the example when it didn`t work.

It still would be nice to know how much flash is used .

1 Like

Having problem with lock up when I do a OTA update with my code .

Please check the docs - they say System.freeMemory()

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

So it's a minimum amount - not the exact amount.