ESP8266 NodeMCU running out of heap


I'm trying to create SMS alarm using Twilio, Firebase and Arduino IDE. Using sensors I am measuring the temperature. If the temperature is higher than a specified value (ESP reads that value from Firebase Realtime Database), a SMS should be sent using twilio. I am also storing the measures in Firebase Realtime Database. The code worked well, until I tried to retrieve data from Firebase one more time and set data two more times. I believe that I'm running out of heap. I have only 10k heap left and cannot send SMS via Twilio. It only prints: connecting to, but I don't receive the message.

I deallocate the Firebase Data objects after every loop, however, I believe, I still don't have enough heap. Do you have any suggestions how to optimize the code and gain more memory?

I posted the same question with code included on StackOverflow ( because it exceeds the maximum allowed length on Arduino Forum.

Any help would be appreciated!

Please post your code according to forum rules. Some people will not go to another website to access the code.

The highest probability for youcode running out of heap-memory is global defined variables of type String.
The Arduino-Crew added the variable-type String to the core but this is of a highly doubtful benefit.

Global defined Strings eat up memory over time. There are two main alternatives:

  1. only use variable-type String inside functions that get called in a regular manner.
    variables defined inside functions don't use heap-memory

  2. replace all variables of type String with c_string, array of char or PString.

anyway for analysing your code post your code in code-tags

best regards Stefan