Electron memory and heap fragmentation

Hello,

I’ve been running into a problem where I run my Electron datalogger project for about 4 days and then all of a sudden it starts being unable to communicate with the cloud (rapid cyan then red blinking).

I have been told that this can be due to fragmentation of the heap memory resulting from over-use of String class objects and poor string-building techniques.

I am trying to figure out why the Electron would fail to communicate with the cloud in the event that the heap is fragmented. I would have thought that the buffers that are used to communicate with the modem are statically allocated when the program first starts, and should therefore be unaffected by heap fragmentation. Is this accurate?

As soon as there is not enough contiguous storage for a malloc or object construction request to not be able to be fulfilled, the system device will throw the SOS code and restart. There is only one heap so it doesn’t matter which code is making the request, user or system. The system code uses static allocation as much as is practical but some things still must be heap allocated.

If you know you need memory, you should statically allocate it at the maximum reasonable size you need to avoid problems. See the answers you got in the other two threads.

2 Likes