Largest contiguous RAM for HEAP

rma-31
Posts: 15
Joined: Sun Oct 08, 2017 5:07 pm

Largest contiguous RAM for HEAP

Postby rma-31 » Tue Oct 17, 2023 3:30 pm

Hi,

On ESP32:

Is there any way to tell malloc to first use memory fragments for heap allocation from the D/IRAM region instead of the DRAM region?
It seems to first consume the DRAM which in turn reduces the size of the largest available contiguous block.
This disallows some critical allocations in my application later on.

Thx for any help :)

username
Posts: 528
Joined: Thu May 03, 2018 1:18 pm

Re: Largest contiguous RAM for HEAP

Postby username » Wed Oct 18, 2023 2:26 am

Have you tried menuconfig ?

rma-31
Posts: 15
Joined: Sun Oct 08, 2017 5:07 pm

Re: Largest contiguous RAM for HEAP

Postby rma-31 » Wed Oct 18, 2023 4:59 am

Which entries in menuconfig? I don't want to place code in the PSRAM, there is an option to allow malloc to use PSRAM, but that is RAM is much slower.

Any hints? Thx :)

Alberk
Posts: 55
Joined: Sat Jun 19, 2021 1:49 am

Re: Largest contiguous RAM for HEAP

Postby Alberk » Wed Oct 18, 2023 5:39 am

Refer to this for memory allocation https://docs.espressif.com/projects/esp ... alloc.html

rma-31
Posts: 15
Joined: Sun Oct 08, 2017 5:07 pm

Re: Largest contiguous RAM for HEAP

Postby rma-31 » Wed Oct 18, 2023 7:39 am

That's a good resource for heap analysis.

Unfortunately nothing is said about heap fragmentation and how to best avoid it in order to get a large as possible contiguous block.

Any more ideas? :)

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

Re: Largest contiguous RAM for HEAP

Postby MicroController » Wed Oct 18, 2023 8:48 am

heap fragmentation and how to best avoid it
General recommendation: Reduce the number of allocations from the heap.
- Use the stack/local variables instead of heap when possible
- Use statically allocated variables
- Avoid reallocations/resizing of allocations (C++ string concatenation, std::vector, ...)
- Allocate one larger block instead of multiple smaller ones.

Alberk
Posts: 55
Joined: Sat Jun 19, 2021 1:49 am

Re: Largest contiguous RAM for HEAP

Postby Alberk » Wed Oct 18, 2023 9:03 am

rma-31 wrote:
Wed Oct 18, 2023 7:39 am
That's a good resource for heap analysis.

Unfortunately nothing is said about heap fragmentation and how to best avoid it in order to get a large as possible contiguous block.

Any more ideas? :)
This should get the largest free block for the different memory types.

Code: Select all

size_t heap_caps_get_largest_free_block(uint32_t caps)

rma-31
Posts: 15
Joined: Sun Oct 08, 2017 5:07 pm

Re: Largest contiguous RAM for HEAP

Postby rma-31 » Wed Oct 18, 2023 9:07 am

MicroController wrote:
Wed Oct 18, 2023 8:48 am
heap fragmentation and how to best avoid it
General recommendation: Reduce the number of allocations from the heap.
- Use the stack/local variables instead of heap when possible
- Use statically allocated variables
- Avoid reallocations/resizing of allocations (C++ string concatenation, std::vector, ...)
- Allocate one larger block instead of multiple smaller ones.
These are good strategies, I guess there are no possibilities to change the heap allocation strategy of malloc to first get D/IRAM and then switch to DRAM, reason ist that the DRAM section is larger than the D/IRAM section.
So if malloc would first allocate from D/IRAM, the large contiguous block from DRAM would still be available.

Is there any possibility to achieve this?

Alberk
Posts: 55
Joined: Sat Jun 19, 2021 1:49 am

Re: Largest contiguous RAM for HEAP

Postby Alberk » Wed Oct 18, 2023 9:10 am

These are good strategies, I guess there are no possibilities to change the heap allocation strategy of malloc to first get D/IRAM and then switch to DRAM, reason ist that the DRAM section is larger than the D/IRAM section.
So if malloc would first allocate from D/IRAM, the large contiguous block from DRAM would still be available.

Is there any possibility to achieve this?
[/quote]

This will allow you to achieve the RAM allocation by hierarchy https://docs.espressif.com/projects/esp ... _t6size_tz

rma-31
Posts: 15
Joined: Sun Oct 08, 2017 5:07 pm

Re: Largest contiguous RAM for HEAP

Postby rma-31 » Wed Oct 18, 2023 9:17 am

heap_caps_malloc_prefer

only allows to separate internal from external (PSRAM) memory, but not DRAM from D/IRAM as far as I know.

For my application it is even more difficult as I use std::unique_ptr and would possible have to reimplement the allocator of std::unique_ptr.

Thanks for your help!

Who is online

Users browsing this forum: No registered users and 251 guests