Chat freely about anything...

User avatar
By tomte76
#57977 Hi,

I wrote some code using the nativ NON-OS SDK 2 and mbedtls to poll sensor-data and send them to an Influx-DB using HTTPS. On of the measurements is the free heap as reported by system_get_free_heap_size(). As you can see in the attached image the free heap wobbles between 45 and below 25K. Sometimes it ramps down and recovers after a while. Looks like a garbage collection. As I do the same stuff all the time it is unlikely my code. Also I cannot see any errors or stuck processes on the serial console. I poll NTP every 10 seconds and I poll the sensors every minute, prepare the update and send it using HTTPS. No errors, same timing measured using a stop-watch and the output on the serial console and the influx-db logs. But different heap-usage all the time. I would not mind that, but if the heap lowers to 25K or below HTTPS wont run any more and I need to implement something like a software reset.

Any hints how to track this down? Or run the "garbage collection" more often to keep heap above 25K for sure?
Attachments
heap-usage.png
User avatar
By piersfinlayson
#57981 I haven't used v2 of the SDK, but with v1.5.x the heap size on my applications is very stable. But then I've optimised all of my code to do static memory allocation rather than dynamic, and do its own memory management within those static allocations.

I would guess the varying free heap size is down to your application rather than the SDK. And as you've said HTTPS won't run when the heap size goes below 25k I'd guess that the HTTPS stack is doing something funky around memory management.
User avatar
By tomte76
#57992 Thank you for the feedback. I also suspected my code in the beginning. But I don't understand why heap is stable for e.g. 2 hours and afterwards it ramps down to 25K and then recovers. Then it may be stable for some time or even immediately ramp down again. While the taken action of my code is always the same, and it is always successful, no measurable delay or e.g. errors on polling sensor data, getting ntp or sending data using HTTPS. I guess, I will comment the HTTPS stuff and just poll the data and output it on the serial and check the heap's behavior then.
User avatar
By piersfinlayson
#58005 Have just done a quick skim of mbedtls and I would hazard that's your culprit. I see it seems to be able to operate in two modes - either fully dynamic memory allocation (calling calloc directly) or with a built in memory manager, in which case you initial it with a single block of memory it'll then manage this space itself.

The ESP8266 demo code I found and looked seems to operate in the dynamic allocation mode (it never calls mbedtls_memory_buffer_alloc_init and MBEDTLS_PLATFORM_C isn't defined).

I would guess you're also using it in the former mode - so it's dynamically allocating and freeing memory as connections come and go (time out, etc).