Local variable usage in SRAM not counted
Why is the local variable not counted?


Let us start with the function availableMemory(), what does it do?

int availableMemory()
{
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &amp;v - (__brkval == 0 ? (int) &amp;__heap_start : (int) __brkval); 
}


It returns the amount of memory between the top of stack (address of local variable v) and the heap end. 

The answer lays in how the compiler works. One strategy that is used is function in-lining. This forces the functions test(), printAvailableMemory(), availableMemory(), etc. to be unfolded into loop() (which actually also gets unfolded into main()). 

A common frame with the local variables is created and the variable for the top of the stack becomes one of them. You need a tougher test case to see how the stack top changes, e.g. tell the compiler not to in-line a function. 

int availableMemory() __attribute__ ((noinline));


To check this generate the assembly listing for your sketch. 

Cheers!