# FreeRTOS TCP/IP pdFREERTOS\_ERRNO\_ENOMEM (Nucleo-F767Zi)

**URL:** <https://forums.freertos.org/t/freertos-tcp-ip-pdfreertos-errno-enomem-nucleo-f767zi/9219>\
**Category:** Libraries\
**Created:** [March 11, 2020, 1:47pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-pdfreertos-errno-enomem-nucleo-f767zi/9219 "2020-03-11T13:47:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stud\_David](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@Stud\_David](https://forums.freertos.org/u/Stud_David)\
**Post date:** [March 11, 2020, 1:47pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-pdfreertos-errno-enomem-nucleo-f767zi/9219/1 "2020-03-11T13:47:28Z")

</div>

Hi,  
I’m currently working on getting the FreeRTOS TCP/IP stack running on the Nucleo-F767Zi. The goal is to communicate with my PC over an ethernet cable. It is currently running so far that I can connect successfully with my Pc. If I want to send or receive a message I get the error code pdFREERTOS\_ERRNO\_ENOMEM back from the commands FreeRTOS\_recv and FreeRTOS\_send. Even if I want to send or receive a simple 1. I’m a bit at a loss as to how to fix this error. In the API References there is the following sentence : **If there was not enough memory for the socket to be able to create either an Rx or Tx stream then -pdFREERTOS\_ERRNO\_ENOMEM is returned.**  
But unfortunately I can’t do anything with it. What does this error tell me and how can I fix it.  
I set the StackDepth to 400, ipconfigTCP\_MSS is set to 1460 and ipconfigNETWORK\_MTU to 1526. For further information I use the BufferAllocation\_2 as advised and in the FreeRTOSConfig.h I set  
#define configSUPPORT\_DYNAMIC\_ALLOCATION to 1.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 11, 2020, 2:32pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-pdfreertos-errno-enomem-nucleo-f767zi/9219/2 "2020-03-11T14:32:56Z")

</div>

Where is pdFREERTOS\_ERRNO\_ENOMEM getting set? If I look in FreeRTOS\_send() I see it calls prvTCPSendCheck(), and inside prvTCPSendCheck() pdFREERTOS\_ERRNO\_ENOMEM gets set if either “pxSocket-\>u.xTCP.bits.bMallocError != pdFALSE\_UNSIGNED” or “pxSocket-\>u.xTCP.txStream == NULL”. That would seem to be indicative of a malloc() failure.

Which [heap allocation scheme](https://www.freertos.org/a00111.html) are you using? What is configTOTAL\_HEAP\_SIZE set to? Do you have a [malloc failed hook](https://www.freertos.org/a00016.html) defined?

---

<div class="post-metadata">

**Author:** ![Stud\_David](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@Stud\_David](https://forums.freertos.org/u/Stud_David)\
**Post date:** [March 11, 2020, 2:47pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-pdfreertos-errno-enomem-nucleo-f767zi/9219/3 "2020-03-11T14:47:36Z")

</div>

Thank you for responding so quickly.  
pdFREERTOS\_ERRNO\_ENOMEM is set at send and receive with the if condition pxSocket-\>u.xTCP.bits.bMallocError != pdFALSE\_UNSIGNED.  
I use the heap\_4 allocation scheme and my whole heap is set as  
#define configTOTAL\_HEAP\_SIZE ((size\_t)15360)  
I have not defined the malloc failed hook.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 11, 2020, 5:51pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-pdfreertos-errno-enomem-nucleo-f767zi/9219/4 "2020-03-11T17:51:24Z")

</div>

> [@Stud\_David](#):
>
> I have not defined the malloc failed hook.

Recommend you do define this as it would give you quick insight to where you issue was as as far as I can see bMallocError can only get set here [https://github.com/FreeRTOS/FreeRTOS/blob/V10.3.1/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS\_Sockets.c#L2958](https://github.com/FreeRTOS/FreeRTOS/blob/V10.3.1/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_Sockets.c#L2958) which would have triggered the malloc failed hook.

So you have run out of heap, which means you either need to tune your system, [including the TCP stack](https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/TCP_IP_Configuration_Examples.html), to use less memory or you need to increase the heap site.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [March 12, 2020, 5:42am UTC](https://forums.freertos.org/t/freertos-tcp-ip-pdfreertos-errno-enomem-nucleo-f767zi/9219/5 "2020-03-12T05:42:49Z")

</div>

Here is a short overview of the usage of RAM in a mid-size MCU:

| ipconfig item | Value | | |
| --- | --- | --- | --- |
| NETWORK\_MTU | 1500 | | |
| TCP\_MSS | 1460 | | |
| USE\_TCP | 1 | | |
| USE\_TCP\_WIN | 1 | | |
| TCP\_RX\_BUFFER\_LENGTH | 2920 | | |
| TCP\_TX\_BUFFER\_LENGTH | 2920 | | |
| USE\_DHCP | 0 | | |
| | | | |
| RAM that is always allocated (statically or on the heap): | | | |
| ipconfig item | Value | PerUnit | Total |
| NUM\_NETWORK\_BUFFER\_DESCRIPTORS | 8 | 1572 | 12576 |
| TCP\_WIN\_SEG\_COUNT | 32 | 64 | 2048 |
| EVENT\_QUEUE\_LENGTH | 25 | 8 | 200 |
| IP\_TASK\_STACK\_SIZE\_WORDS | 512 | 4 | 2048 |
| ARP\_CACHE\_ENTRIES | 6 | 12 | 72 |
| DNS\_CACHE\_ENTRIES | 4 | 28 | 112 |
| Total | | | 17056 |

In the first column you see the `ipconfig` item, like e.g. `ipconfigNETWORK_MTU`.

Beside the above, malloc will be used to allocate sockets and their TCP stream buffers.  
The length of the stream buffers depends on `ipconfigTCP_RX_BUFFER_LENGTH` and `ipconfigTCP_TX_BUFFER_LENGTH`, unless overridden by the socket options `FREERTOS_SO_RCVBUF` and `FREERTOS_SO_SNDBUF`.

| Object | Size |
| --- | --- |
| UDP-Socket | 100 |
| TCP-Socket | 432 |
| TX-Buffer | 2920 |
| RX-Buffer | 2920 |

When a TCP socket is only used in one direction, it will only create a single stream buffer.  
Note that BufferAllocation\_1.c is used, which means that all network buffers are allocated statically (8 x1572 bytes).

---

<div class="post-metadata">

**Author:** ![Stud\_David](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@Stud\_David](https://forums.freertos.org/u/Stud_David)\
**Post date:** [March 12, 2020, 9:34am UTC](https://forums.freertos.org/t/freertos-tcp-ip-pdfreertos-errno-enomem-nucleo-f767zi/9219/6 "2020-03-12T09:34:08Z")

</div>

Many thanks for the listing. Since I have only used the LwIP STACK to establish ethernet connections and the standard heap size of :  
#define configTOTAL\_HEAP\_SIZE ((size\_t)15360)  
for this always sufficed, I did not think that the heap size could be too small.  
But because of its listing I soon realized that I had to increase my TOTAL\_HEAP\_SIZE.  
Thanks for the fast and helpful tips.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [March 12, 2020, 10:09am UTC](https://forums.freertos.org/t/freertos-tcp-ip-pdfreertos-errno-enomem-nucleo-f767zi/9219/7 "2020-03-12T10:09:16Z")

</div>

The utility that created the above table is now available in a [PR #1828](https://github.com/aws/amazon-freertos/pull/1828)

Beside increasing `configTOTAL_HEAP_SIZE` you can also decrease the memory usage, especially with these macros:

```auto
NUM_NETWORK_BUFFER_DESCRIPTORS
TCP_RX_BUFFER_LENGTH
TCP_TX_BUFFER_LENGTH
TCP_WIN_SEG_COUNT
NETWORK_MTU

```

They are all described on [FreeRTOS.org](https://freertos.org)
