Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Available Memory

The microcontrollers used on Arduino boards (ATmega8, ATmega168 and ATmega328) have a small amount of RAM. Running out of RAM due to the complexity of the sketch or a memory leak may be the cause of bugs. Here are a few options for finding the amount of free RAM in your Arduino.


MemoryFree Library

MemoryFree library - In order to determine the amount of memory currently available the most accurate result can be found by using the MemoryFree library. It is based on code posted in the forum (here), extended to include walking the free list:


Revised MemoryFree Library

Github link: https://github.com/McNeight/MemoryFree


MemoryFree.h:

// MemoryFree library based on code posted here:
// https://forum.arduino.cc/index.php?topic=27536.msg204024#msg204024
// Extended by Matthew Murdoch to include walking of the free list.

#ifndef MEMORY_FREE_H
#define MEMORY_FREE_H

#ifdef __cplusplus
extern "C" {
#endif

int freeMemory();

#ifdef  __cplusplus
}
#endif

#endif
MemoryFree.cpp:

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

extern unsigned int __heap_start;
extern void *__brkval;

/*
 * The free list structure as maintained by the
 * avr-libc memory allocation routines.
 */

struct __freelist {
  size_t sz;
  struct __freelist *nx;
};

/* The head of the free list structure */
extern struct __freelist *__flp;

#include "MemoryFree.h"

/* Calculates the size of the free list */
int freeListSize() {
  struct __freelist* current;
  int total = 0;
  for (current = __flp; current; current = current->nx) {
    total += 2; /* Add two bytes for the memory block's header  */
    total += (int) current->sz;
  }
  return total;
}

int freeMemory() {
  int free_memory;
  if ((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__heap_start);
  } else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
    free_memory += freeListSize();
  }
  return free_memory;
}

Example sketch:

#include <MemoryFree.h>

// On Arduino Duemilanove with ATmega328:
//
// Reported free memory with str commented out:
// 1824 bytes
//
// Reported free memory with str and Serial.println(str) uncommented:
// 1810
//
// Difference: 14 bytes (13 ascii chars + null terminator)

// 14-bytes string
//char str[] = "Hello, world!";

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

void loop() {
    //Serial.println(str);

    Serial.print("freeMemory()=");
    Serial.println(freeMemory());

    delay(1000);
}


Functions

Other simpler functions that compute how much memory you have free (but use dynamic allocation and deallocation of memory which may not be ideal):


// this function will return the number of bytes currently free in RAM
// written by David A. Mellis
// based on code by Rob Faludi https://www.faludi.com
int availableMemory() {
  int size = 1024; // Use 2048 with ATmega328
  byte *buf;

  while ((buf = (byte *) malloc(--size)) == NULL)
    ;

  free(buf);

  return size;
}
Note: For my 2K ATMega328 system, the above code gave me bogus random results and caused the system to hang.


/* This function places the current value of the heap and stack pointers in the
 * variables. You can call it from any place in your code and save the data for
 * outputting or displaying later. This allows you to check at different parts of
 * your program flow.
 * The stack pointer starts at the top of RAM and grows downwards. The heap pointer
 * starts just above the static variables etc. and grows upwards. SP should always
 * be larger than HP or you'll be in big trouble! The smaller the gap, the more
 * careful you need to be. Julian Gall 6-Feb-2009.
 */

uint8_t * heapptr, * stackptr;
void check_mem() {
  stackptr = (uint8_t *)malloc(4);          // use stackptr temporarily
  heapptr = stackptr;                     // save value of heap pointer
  free(stackptr);      // free up the memory again (sets stackptr to 0)
  stackptr =  (uint8_t *)(SP);           // save value of stack pointer
}
Note: For my 2K ATMega328 system, the above code gave me consistent and I think accurate results.


Another method that seems to be even simpler is the following: Declare this function:

int freeRam () {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
and call it anywhere in your program like that: Serial.println(freeRam());

In my test the results were quite similar with the first method, the one that uses the library. I found this function here: http://www.controllerprojects.com/2011/05/23/determining-sram-usage-on-arduino/