Change memory allocating behavior for a certain component (C++)

wbice123
Posts: 3
Joined: Tue Jul 30, 2019 8:33 am

Change memory allocating behavior for a certain component (C++)

Postby wbice123 » Tue Jul 30, 2019 9:18 am

Hi, All:

I got stuck in a problem when I'm integrating a component written in c++ into the project.
This component consume a lot memory, both in heap and in stack.
I've tried to move variables allocated in stack to heap, but it still consume too much memory in internal RAM.
So, is there a viable method for me to move memory allocated in internal RAM into external RAM(SPIRAM), only for this component, without affecting other components or the App?
By the way, it uses tons of stl containers, including vector, list, queue and so on.
Thanks !

ESP_Sprite
Posts: 2813
Joined: Thu Nov 26, 2015 4:08 am

Re: Change memory allocating behavior for a certain component (C++)

Postby ESP_Sprite » Wed Jul 31, 2019 2:15 am

No, not really as the heap allocator is a runtime thing. The only way I can imagine you can do this is to give all c++ objects used a custom allocator that manually allocates off the psram heap using heap_alloc_caps.

jcsbanks
Posts: 269
Joined: Tue Mar 28, 2017 8:03 pm

Re: Change memory allocating behavior for a certain component (C++)

Postby jcsbanks » Wed Jul 31, 2019 9:57 am

Not a direct solution, but sharing an experience that may help, or maybe you've covered all this and more already...

I'm using heap in PSRAM with std::map and std::vector, but had some issues with std::vector where even after erase() the memory was still reserved on the heap. C++ 11 shrink_to_fit did the trick. Some other things I did were to refactor some code to no copy/less copy to get things to fit, processing some things in chunks and fetching pointers to access flash directly with dynamic use of mmap but much faster than a standard file system. You may also be able to move other stuff to PSRAM like mbed_tls if using that as it is a memory hog in IRAM. Also it is worth running your application for some time and many operations to check the low water mark for various heap capabilities and that the minimum contiguous free block is not degrading in size progressively.

wbice123
Posts: 3
Joined: Tue Jul 30, 2019 8:33 am

Re: Change memory allocating behavior for a certain component (C++)

Postby wbice123 » Thu Aug 01, 2019 1:55 am

Thank you so much @ jcsbanks
The shrink_to_fit really helps, and it's a great idea for certain containers.
At last, I tried to set a custom allocator for all container in this component. The work was boring, but helpful, and decreased about 10K bytes in internal memory.
Happy to share my code if anyone needs.

wbice123
Posts: 3
Joined: Tue Jul 30, 2019 8:33 am

Re: Change memory allocating behavior for a certain component (C++)

Postby wbice123 » Thu Aug 01, 2019 1:57 am

ESP_Sprite wrote:
Wed Jul 31, 2019 2:15 am
No, not really as the heap allocator is a runtime thing. The only way I can imagine you can do this is to give all c++ objects used a custom allocator that manually allocates off the psram heap using heap_alloc_caps.
I've done this work, and it do helps. The work was so boring, though. Thanks a lot.

Who is online

Users browsing this forum: No registered users and 31 guests