Shrinking SRAM

Hello

I’ve tested Arduinos’ UNO free memory. The result of this code is strange for me:

#include <MemoryFree.h>


void setup() 
{
Serial.begin(9600);
}


void loop() 
{
function1();
}


void function1()
{
Serial.println(freeMemory());
function2();  
}


void function2()
{
Serial.println(freeMemory());
function1(); 
}

The result is shrinking RAM:
1835
1833
1831
1829
1827
1825
1823
1821
1819
1817
1815

131
129
127
125
123
121
119
117
111
984
30553

What is reason of this?
The Serial.print() function, freeMemory() function or links?

You are recursively calling the function1 and 2 therefore it eats up the memory, I think..

I've remarked that this way corrected code is better:

void function1()
{
static int memory=freeMemory();
Serial.println(memory);
function2();  
}


void function2()
{
static int memory=freeMemory();
  Serial.println(memory);
function1(); 
}

The result: 1815 1813 1815 1813 1815 1813 1815 1813 1815 1813 1815 1813 1815 1813 1815

@pito - You are correct, each of the recursive function adds to the stack using it up.

The second version of the code appeares to be misreporting the free memory as the function calls are still recursive.

Mark

OK - I can see what's happening in the second example. You need to look at initialisation of static vars. Basically your freememory() are only made the first time your 2 functions are called. Try this

void function1()
{
static int memory=freeMemory();
Serial.println(memory);
Serial.println(freeMemory());
function2();  
}


void function2()
{
static int memory=freeMemory();
  Serial.println(memory);
Serial.println(freeMemory());
function1(); 
}

Mark

Thank you Mark. This code works stable. There is smaller shrinking RAM, but I don't understand why this method use memory? If I have lots of functions linked each other...that mean the RAM will be consecutive shrunk?

Look up recursion. Every time you call a function from another, you consume stack space. Although there are circumstances when a function may call itself or a group of functions may make mutual calls to each other, it’s not something you see often if you’re using an UNO, precisely because it does not have much RAM.

In your case, function1 calls function2 and consumes a little space on the stack, then function2 calls function2 and uses some more. Then function1 calls function2 etc. etc and the depth of the call staqck increases until you consume all the RAM you have and crash.

highpiotr: If I have lots of functions linked each other...that mean the RAM will be consecutive shrunk?

Each time your code makes a function call, it uses up a bit of memory on the stack to keep track of the call. Your code makes an unbounded number of function calls since you have two functions that call each other recursively without any limits. Inevitably, that code will keep making more and more calls until the stack has used up all available memory, and then fail.

Each time a function is called the return address is added to the stack, any vars local to the function are allso created on the stack. When you return from the function the stack used by the function is freed up.

You are calling the functions recursively and never allowing them to return. If your program where any more complex then it would crash!

Mark

I understand now why my code was wrong. My program was crashed, but now I know that I have to change its logical structure. Thank you!