Memory problem with MQTT and new()

Jonathan2892
Posts: 45
Joined: Tue Dec 07, 2021 4:04 pm

Memory problem with MQTT and new()

Postby Jonathan2892 » Wed Oct 02, 2024 2:27 pm

Hi,

I have a project which is mainly written in C++ and uses a lot of

Code: Select all

new()
. Somehow this causes the MQTT-client to not be able to allocate memory. I have tried to fix this problem by using different sdkconfig options, but I wasn't able to solve the problem. The free HEAP before the mqtt-client tries to allocate its memory is around 4MB, so should be more than enough.

In sdkconfig I have:

Code: Select all

CONFIG_SPIRAM_USE_MALLOC=y
CONFIG_SPIRAM_MEMTEST=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=2048
CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP=y
CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=32768
CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY=y

CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC=y
CONFIG_MQTT_TASK_CORE_SELECTION_ENABLED=y
CONFIG_MQTT_USE_CORE_0=y
Now I wonder, if

Code: Select all

new()
always goes into internal memory and mqtt needs some space there and can not be completely allocated in external memory?

Do I need to tell

Code: Select all

new()
to allocate specifically in external memory?

Setup:
- esp32s3-N16R8
- esp-idf 5.3.1

Hope someone can help...

Best

MicroController
Posts: 1605
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Memory problem with MQTT and new()

Postby MicroController » Wed Oct 02, 2024 4:57 pm

Jonathan2892 wrote:
Wed Oct 02, 2024 2:27 pm

Code: Select all

CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=2048
Now I wonder, if

Code: Select all

new()
always goes into internal memory
In this case it does for all objects smaller than 2KB.
and mqtt needs some space there and can not be completely allocated in external memory?
That is likely the case.
Do I need to tell

Code: Select all

new()
to allocate specifically in external memory?
Yes, if you explicitly want an object in external RAM.
You can do this by manually allocating sizeof(X) bytes from the heap in external RAM, then use the placement new operator with that memory.

Who is online

Users browsing this forum: Bing [Bot] and 64 guests