Shortage of Heap memroy

Hello All,

I am running my application on Marvell MW300 board, using FreeRTOS V9.0.0.
In my application when I try to connect HTTPS server, mbedtls shows error.

[wm_mbedtls] ssl_tls.c:5431: |1| 0x00121188: alloc(4429 in bytes) (4429 out bytes) failed.

While debugging it is observer that this is due to the shortage of heap memory. Here is the heap memory stat

Heap size ---------------------- : 305536
Free size ---------------------- : 17888
Peak Heap Usage since bootup --- : 291048
Total allocations -------------- : 136
Failed allocations ------------- : 0
Min overhead per allocation ---- : 16
Biggest free block available now : 8040

I print this heap memory info before try to connect my HTTPS server.
It is observed that when a device trying to connect HTTPS server, mbedtls want to allocate two 4429 byte buffer (in and out), but it gets failed because of Biggest free block available is 8040
Here is the code of mbedtls.

  * Prepare base structures
     if( ( ssl-> in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN( ssl->conf ) ) ) == NULL ||
         ( ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN( ssl->conf ) ) ) == NULL )
                 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d in bytes) (%d out  bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN( ssl->conf ),
                                MBEDTLS_SSL_OUT_BUFFER_LEN( ssl->conf ) ) );
              mbedtls_free( ssl->in_buf );
                 ssl->in_buf = NULL;
             return( MBEDTLS_ERR_SSL_ALLOC_FAILED );

The Free memory available onboard is 17888.
Is it possible to add some “Free memory” into “free blocks available”?
Or any suggestion, how to handle this issue?

I am using the heap 4 scheme.

Thanks in advance.

There is no way to combine to combine the smaller free blocks. There are couple of things you can try though:

  • Is anything other than mbedTLS using dynamic memory? If yes, you can try to move them to use statically allocated memory. All the FreeRTOS APIs have a static memory version.
  • Do you have the RAM available to be able to increase heap size?


Heap 4 will automatically combine contiguous free blocks.

That report indicates that you are using a LOT of heap. And one issue with using heap is it DOES get fragmented and you may have enough space, but not all together.

One thought is to examine where all that memory is going. As aggarg suggests, make everything FreeRTOS that you can use static memory rather than the heap, and check your stack sizes of tasks.

You may need to put your program on a lower memory diet to make things work.