Page 1 of 2
Largest contiguous RAM for HEAP
Posted: Tue Oct 17, 2023 3:30 pm
by rma-31
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
Re: Largest contiguous RAM for HEAP
Posted: Wed Oct 18, 2023 2:26 am
by username
Have you tried menuconfig ?
Re: Largest contiguous RAM for HEAP
Posted: Wed Oct 18, 2023 4:59 am
by rma-31
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
Re: Largest contiguous RAM for HEAP
Posted: Wed Oct 18, 2023 5:39 am
by Alberk
Re: Largest contiguous RAM for HEAP
Posted: Wed Oct 18, 2023 7:39 am
by rma-31
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?
Re: Largest contiguous RAM for HEAP
Posted: Wed Oct 18, 2023 8:48 am
by MicroController
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.
Re: Largest contiguous RAM for HEAP
Posted: Wed Oct 18, 2023 9:03 am
by Alberk
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)
Re: Largest contiguous RAM for HEAP
Posted: Wed Oct 18, 2023 9:07 am
by rma-31
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?
Re: Largest contiguous RAM for HEAP
Posted: Wed Oct 18, 2023 9:10 am
by Alberk
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
Re: Largest contiguous RAM for HEAP
Posted: Wed Oct 18, 2023 9:17 am
by rma-31
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!