measure SRAM memory use

Hi, I've been using arduino about two months and today has emerged me a question related to memory use.

Program data in execution are stored in SRAM right? During execution, the memory usage varies depending on the funtions you use etc?

It is possible to measure the memory usage?

Thank you very much in advance.

This link will get you started.

Yes, totally.

int freeRAM() {
  int value = 0;
  int result = 0;
  extern int *__brkval;
  extern int __heap_start;
  result = (int)&value - ((int)__brkval == 0 ? (int)&__heap_start : (int)__brkval);
  return result;
}

@Arank
Your code is not correct. It is little bit more, see the link posted in #1 by Arctic_Eddie.
Here is detailed info: avr-libc: Memory Areas and Using malloc().

Maybe I did not make myself correctly. I mean I want to know if when I call a extern library function increases memory consumption and it decreases again after the call. Or otherwise, Arduino fixed the total amount of memory to be used when sketch will be ran, and reserve all the time that memory even if it is not used at all times.

I want to know specifically memory use from a specifically function from an external library. Is it possible?

@Budvar10

If you check the Arduino reference mentioned by Arctic_Eddie, on the end there is a simpler code, where I got this one, I just renamed some variables to more meaningfull ones.

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

When I tested both codes (the first one, using a library) and this, the results were always the same...

jesussotofan:
Maybe I did not make myself correctly. I mean I want to know if when I call a extern library function increases memory consumption and it decreases again after the call. Or otherwise, Arduino fixed the total amount of memory to be used when sketch will be ran, and reserve all the time that memory even if it is not used at all times.

I want to know specifically memory use from a specifically function from an external library. Is it possible?

That's exactly why this function exists...

When a function or a variable goes out of scope, the RAM used by it are free again. The Arduino doesn't reserve any memory for anything automatically, unless you say so.

If you want to check the memory usage from an specific function, or library, or variable, or anything else, just print the freeRAM result, before, during and after (when it goes out of scope) and the difference will be the amount used by that specific call.

@Arank
Here is my implementation:

// --------------------------------------------------------------------------------------
//
// **************
// *  FreeSRAM  *
// **************
//
// Evaluate an amount of SRAM free space (include free fragments in heap)

#ifdef _DEBUG_FREE_SRAM

/* free list structure as maintained by the avr-libc memory allocation routines */
struct __freelist {
	size_t sz;
	struct __freelist *nx;
};

unsigned int FreeSRAM()
{
unsigned int free_memory = (unsigned int)&free_memory + sizeof(free_memory);
										// the last element in stack, so its address is
										// current pointer to end of stack

	#if defined (__AVR__)

	extern unsigned int __heap_start;	// beginning of allocation heap
	extern void *__brkval;				// current end of heap (0 if no allocation yet)
	extern struct __freelist *__flp;	// head of the free list structure

		if(__brkval == 0) free_memory -= (unsigned int)&__heap_start;
		  else {          free_memory -= (unsigned int)__brkval;

		/* add all free memory fragments if any */
			for(struct __freelist *current = __flp; current; current = current->nx) {
				free_memory += 2;		// two bytes for the memory block's header
				free_memory += (unsigned int)current->sz;
			}
		}

	#endif

	return(free_memory);
}

/*                           *** ATmega1284P Data Memory ***

	0x0000 - 0x001F		32 Registers (1 byte each)
	0x0020 - 0x005F		64 I/O Registers
	0x0060 - 0x00FF		160 Ext I/O Registers
	0x0100 - 0x40FF		Internal SRAM 16384 bytes (RAMSTART - RAMEND)

	Internal SRAM:

	0x0100 - bottom of RAM			.data variables (contains static data which was
									defined in code)
	__data_end == __bss_start		.bss variables  (uninitialized global or static
									variables end up in the .bss section)
	__bss_end  == __heap_start		heap for dynamic allocation - malloc()
	__brkval						end of heap
	...								free memory
	SP (stack pointer)				local variables
	0x40FF - top of RAM (RAMEND)	stack is started from the top of internal RAM
									(backward)
 */

@Budvar10

I'm at work now, have you tested both?
The results are different? :o

Arank:
If you want to check the memory usage from an specific function, or library, or variable, or anything else, just print the freeRAM result, before, during and after (when is goes out of scope) and the difference will be the amount used by that specific call.

I think that 'during' is op's problem. It requires modification of the library functions.

sterretje:
I think that 'during' is op's problem. It requires modification of the library functions.

Yes, that is exactly my problem.
Do you know what I need to do in the library to acquire this functionality?

In every library method / function that you use you need calls to your freeRam function at strategic places. It's a lot of work.

I think that freeRam implementations are not useful to give a real time view of memory usage but more useful for detection of memory leaks.