Running out of heap memory

i’m currently running out of heap and i can’t find a way to solve this
i’m using sprintf to populate the buff array which is declared locally in a function (unsigned char buff[512]:wink:
post_1/2/3 are const char post_x declared globally.
Token is a char

sprintf((char *)buff, “%s%d%s%s%s” , post_1, 230, post_2, TOKEN, post_3);

By the time i add 1/2 libraries i see red blinks everywhere at this instruction.

I was wondering if there is a way to put stuff in the stack or if i’m already doing this correctly (in this case i will give up since it is related to the libraries i’m using)

Are you sure this is a heap issue?
How did you check?
What red panic code do you exactly get?
Where (in what thread) are you executing this command?
Are all your char* strings terminated?

For safe measure I’d use snprintf((char*)buff, sizeof(buff), ...)

@itsmaxdd, seeing a little more code to put things in context would be helpful. Declaring buff[512] inside a function would most likely use the stack instead of the heap.

The reason i’m assuming this, is because TLS library is really high demanding.
I have a working code snippet A with a simple webserver + softAP example which works fine and a simple code snippet B with a working TLS connection.
If i merge the two code snippets the photon starts to blink red at the mentioned instruction.
The webserver is started in the setup while the TLS connection is called only one time in the loop() (not at each loop but only once in the lifetime for now).
I opened an issue on github thinking it was a bug of the TlsTcpClient but then it seems related to a memory problem

As @peekay123 already mentioned, for one we don’t know in which of your functions that statement is called, due to lack of context and having 512 byte acquired on the stack may actually end up in a stack overflow rather than point to a heap issue.

And as I said, “blinking red” isn’t very exact, can you be more precise?

1 Like

The TlsTcpClient library consumes a lot of memory because the underlying MBEDTLS library requires it. That is, I think, simply because TLS is a complex protocol (so needs lots of code) and needs lots of buffer space at runtime.

There’s an article here that explains how you can reduce memory requirements.

I’ve tried editing my mbedtls config.h file and adding a line like this. System.freeMemory() significantly increases so it’s having some effect on the runtime requirements for memory.


My current interpretation of how it works is that the normal (default) for that value is 16384 bytes, but you can reduce it if you are never going to send or receive more that that amount of data in one read or write. That includes receiving the certificate chain from the HTTPS web server that you connect to and for that reason you shouldn’t go much lower than about 6KB, so try 8192 for starters. If MBEDTLS_SSL_MAX_CONTENT_LEN is left at its default of 16384 then you can read/write more than 16384 because it sends the data in multiple frames but if you set it to less than 16384 then you are limited to one frame.

I’ve only briefly experimented with this, so don’t rely on it as being a tried-and-trusted solution. :wink:

If you check the github issue you can find all the answers.
You can start from this code
This snippet is leading to red blinks (i honestly don’t know the pattern).
If you move the TlsTcpClient out of the function the photon doesn’t crash yet the TLS connection doesn’t work.

If you start from here instead the TLS works but by the time you add the webserver it fails

This is an interesting point.
Honestly i haven’t found out yet how manually edit libraries with these two awfully terrible IDEs (online and particle dev). I noticed that you can select add to project or copy to project in the particle dev. Will the second options allow me to edit the libraries eventually?

Regarding the System.freeMemory(), can’t i just issue it in the code whenever i like?
Regarding the config.h, can’t i just redefine stuff in my *.ino file?
I’ve read the article and it seems that disabling unused ECP curves would be helpful, my question then is how do i find out what curve/size i need when connecting to a specific site?

Is there a command to see if i’m having problem in the stack or in the heap?

Edit: it seems that the library is already pretty much optimized in terms of “curves”

Thank you all

No one?

I don’t know you are still hacking on this project. I too tried MBEDTLS and it is huge. I also tried axtls and that works, but the I/O needs to get an overhaul. There was a mention of wolfssl that is now ported and available as a library. Please see information over in this post.