Arduino Memory Management

Presentation on July 11, 2016

No recording was made of the presentation.  Slides are here.

I promised a code snippet for dumping the .data, .bss, heap, and stack.

//**************************************************
//*    http://www.nongnu.org/avr-libc/user-manual/malloc.html
//*    thanks to John O for the pointer to this info and the insperation to do it
void Ram_TableDisplay(void)
{
    extern uint8_t __stack;
    extern uint8_t _end;
    extern char *__data_start;
    extern char *__data_end;

    extern char *__bss_start;
    extern char *__bss_end;
    extern char *__brkval;
    extern char *__heap_start;
    extern char *__heap_end;
    uint8_t *topOfHeap = NULL;

    topOfHeap = (uint8_t*)malloc(sizeof(uint8_t));

    int data_size  = (int)&__data_end - (int)&__data_start;
    int bss_size   = (int)&__bss_end - (int)&__data_end;
    int heap_end   = (int)topOfHeap;
    int heap_size  = (int)topOfHeap - (int)&__heap_start;
    int stack_size = RAMEND - (int)SP;
    int available  = (RAMEND - (int)&__data_start + 1);

    free(topOfHeap);
    topOfHeap = NULL;
    available -= data_size + bss_size + heap_size + stack_size;

    Serial.print(F("+----------------+  __data_start  = "));   Serial.println((int)&__data_start);
    Serial.print(F("+      data      +"));                     Serial.println();
    Serial.print(F("+    variables   +  data_size     = "));   Serial.println(data_size);
    Serial.print(F("+                +"));                     Serial.println();
    Serial.print(F("+----------------+  __data_end-1  = "));   Serial.println((int)&__data_end-1);
    Serial.print(F("+----------------+  __bss_start   = "));   Serial.println((int)&__bss_start);
    Serial.print(F("+       bss      +"));                     Serial.println();
    Serial.print(F("+    variables   +  bss_size      = "));   Serial.println(bss_size);
    Serial.print(F("+                +"));                     Serial.println();
    Serial.print(F("+----------------+  __bss_end-1   = "));   Serial.println((int)&__bss_end-1);
    Serial.print(F("+----------------+  __heap_start  = "));   Serial.println((int)&__heap_start);
    Serial.print(F("+                +"));                     Serial.println();
    Serial.print(F("+       heap     +  heap_size     = "));   Serial.println(heap_size);
    Serial.print(F("+                +"));                     Serial.println();
    Serial.print(F("+----------------+  heap_end      = "));   Serial.println(heap_end-1);
    Serial.print(F("available = "));
    Serial.println(available);
    Serial.print(F("+----------------+  Current STACK = "));   Serial.println((int)SP);
    Serial.print(F("+                +"));                     Serial.println();
    Serial.print(F("+      stack     +  stack_size    = "));   Serial.println(stack_size);
    Serial.print(F("+                +"));                     Serial.println();
    Serial.print(F("+----------------+  RAMEND        = "));   Serial.println(RAMEND);

    Serial.println();
    Serial.println();
}

Triangle Embedded Interest Group