Cutting the cord EPG – ESP8266 Memory allocation overhead (Part 6.5)

Thought I would let people know about an experience I had with memory allocation on the ESP8266.  Below is a post I made to the ESP8266 forum.  Link to the original post is here.

I have been writing a sketch that performs a SOAP web service request and returns data….lots of text data that I wish to store.
My hope was that I could use allocated char * vs fixed size char arrays to save memory.
Unfortunately I noticed that my free ram was declining at an alarming rate when allocating the strings.
Although I only have a total of 2k of text strings spread across 150 strings, that at the end of the allocation my heap had gone down by almost double that. So I wrote a small test program (below) and noticed something alarming (at least to me).
The overhead of allocating a 1 byte is 23 bytes of heap. The program below shows that the overhead varies between 16 bytes at minimum and 23 bytes at maximum depending on the number of bytes allocated. Unfortunately most of the strings I am trying to store are less then 32 bytes so my overhead is huge.
Is this a problem with memory allocation on the ESP8266 or normal for a microprocessor? I should try it on the Uno but have not had a chance yet.
What do you think?


const int MAX_SIZE=100;
char *saveit[MAX_SIZE];

void setup()


  for (int loop = 1; loop < MAX_SIZE; loop++)
    uint32_t freeHeapStart = ESP.getFreeHeap();
    saveit[loop] = new char[loop];
    uint32_t freeHeapEnd = ESP.getFreeHeap();

Serial.print("new size=");
Serial.print(", alloc=");
Serial.print(freeHeapStart - freeHeapEnd);
Serial.print(", overhead=");
Serial.println((freeHeapStart - freeHeapEnd) - loop);

void loop() {
  // put your main code here, to run repeatedly:


Sufficient to say that I have written a small String Buffer class and manage the memory myself with a single memory allocation for all the strings in my program.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s