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 !
Change memory allocating behavior for a certain component (C++)
-
- Posts: 9769
- Joined: Thu Nov 26, 2015 4:08 am
Re: Change memory allocating behavior for a certain component (C++)
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.
Re: Change memory allocating behavior for a certain component (C++)
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.
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.
Re: Change memory allocating behavior for a certain component (C++)
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.
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.
Re: Change memory allocating behavior for a certain component (C++)
I've done this work, and it do helps. The work was so boring, though. Thanks a lot.ESP_Sprite wrote: ↑Wed Jul 31, 2019 2:15 amNo, 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.
Who is online
Users browsing this forum: Baidu [Spider], ESP_ondrej, MicroController and 125 guests