Go Down

Topic: Memory usage statistics (Read 2130 times) previous topic - next topic


Does anyone have any code snippets for getting the amount of memory in use and also the total size of the memory dynamically? I've found the links to the AVR way, but couldn't find much for the Due.



I would find this useful too.



Hi Greg,

do you have the link about the AVR way?


AVR function:

Code: [Select]
// Returns the number of bytes currently free in RAM
static int freeRAM(void)
extern int  __bss_end;
extern int* __brkval;
int free_memory;
if (reinterpret_cast<int>(__brkval) == 0) {
   free_memory = reinterpret_cast<int>(&free_memory) - reinterpret_cast<int>(&__bss_end);
} else  {
   free_memory = reinterpret_cast<int>(&free_memory) - reinterpret_cast<int>(__brkval);
return free_memory;


The Due seems to be using Newlib: http://sourceware.org/newlib/ There are a few functions and symbols relating to used and free memory. Ram organization appears unix-like. Here is a quick sketch as a starting point:

Code: [Select]
// Due free ram by stimmer - needs more testing

#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>

//these might be defined in headers somewhere but I can't find where.
extern char _end;
extern "C" char *sbrk(int i);
char *ramstart=(char *)0x20070000;
char *ramend=(char *)0x20088000;

int dummy[42]={1,2,3};
void setup() {

int total=0;
int count=0;
void loop() {
 int r=random(1,100)*8;
 char *memoryalloc=(char *)malloc(r);
 char *memoryleak =(char *)malloc(r);
 printf("\nI have malloced %d\n",total);
 printf("total+8*count = %d\n",total+8*count);

 struct mallinfo mi=mallinfo();

 printf("    arena=%d\n",mi.arena);
 printf("  ordblks=%d\n",mi.ordblks);
 printf(" uordblks=%d\n",mi.uordblks);
 printf(" fordblks=%d\n",mi.fordblks);
 printf(" keepcost=%d\n",mi.keepcost);
 char *heapend=sbrk(0);
 register char * stack_ptr asm ("sp");
//char *stack_ptr=(char *)alloca(0); // also works

 printf("\nram start %lx\n", ramstart);
 printf("data/bss end %lx\n", &_end);  
 printf("heap end %lx\n", heapend);
 printf("stack ptr %lx\n",stack_ptr);  
 printf("ram end %lx\n", ramend);
 printf("\nDynamic ram used: %d\n",mi.uordblks);
 printf("Program static ram used %d\n",&_end - ramstart);
 printf("Stack ram used %d\n\n",ramend - stack_ptr);
 printf("My guess at free mem: %d\n",stack_ptr - heapend + mi.fordblks);

Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131