Memory Usage of keyword "new"


I am trying to rap my head around memory usage when I dynamically allocate memory via the “new” operator.
My intention is to create an array of type byte.
I will not know the value “numInts” but I have chosen 400 for the small program below.
The code is as follows

#include <pgmStrToRAM.h>
#include <MemoryFree.h>

byte* SideArray;
int numInts = 400;

void setup() {
    Serial.println(" ");

    Serial.print("Free memory is (Begin): ");

    SideArray = new byte[numInts];
    Serial.print("Free memory is (after byte Array are created): ");

    for (int i = 0; i < numInts; i++)
        SideArray[i] = 1;

    delete[] SideArray;
    SideArray = nullptr;
    Serial.print("Free memory is (after byte Array are deleted): ");


void loop() {

The Output is the following

Free memory is (Begin): 8698
Free memory is (after byte Array are created): 7346
Free memory is (after byte Array are deleted): 7748

The reduction in memory is 1352 bytes when the array is created. I was expecting 400 bytes.
When I delete the array though, it appears to recover 400 bytes.

Is there any explanation for the big dip at the beginning.
Am I doing something wrong?

Any insight would be appreciated.


NB: I have attached that memory library (5.14 KB)

The explanation is very platform specific but you failed to tell us which Arduino you’re using.

I guess you’re on the AVR platform so the reason for this behavior is in the way the free memory is calculated. It simply shows the difference between the upper limit of the heap and the lower limit if the stack. So it also shows changes of the stack pointer as well as changes in the heap size (later is probably what you actually expected).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.