-2

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.

beparas
  • 1,927
  • 7
  • 24
  • 30

1 Answers1

0

According to the log you've posted, you have a total of 305536 bytes available for heap, out which you use 287648 bytes, leaving you with only 17888 bytes of free memory. In other words, you use over 94% of available "dynamic" memory.

For applications that connect over TLS (you've mentioned HTTPS which is HTTP over TLS), having ~18KB of free memory is likely not going to be enough. TLS connections in most TCP/IP stack implementations I've encountered tend to be very "memory-heavy", as they tend to allocate internal buffers used for encryption and decryption of packets. Certificate validation step during TLS handshake also tends not to be very "lightweight", as the server may decide to send you a whole chain of certificates for validation, which may in total be kilobytes in size just by itself. All that needs to be at least temporarily in memory.

To answer your question of Is it possible to add some “Free memory” into “free blocks available?, you have two options:

  1. Get hardware with more RAM: either MCU with more internal RAM or use external RAM chip,

  2. Optimize your application.

First option is self-explanatory. I'd first recommend going for second option. I assume that the numbers you've given (17888 bytes free) are for application that's idle / "does nothing". If so - that's a LOT and you should look into what causes so much RAM to be used in this case. This is going to involve you debugging all parts of your application which may at some point dynamically allocate memory.

J_S
  • 2,985
  • 3
  • 15
  • 38