Depending on the board, an ESP32 has anywhere from 2 to 8 MB of PSRAM. Meanwhile std::vector, std::map and std::string all fight over a few hundred KB of internal RAM until something crashes. I had a tiny allocator on GitHub that only really worked for std::vector. std::map never behaved with it, and it turns out it was missing two things node-based containers need. So I rebuilt it into a proper library: ```cpp include psram::vector samples(250'000); // 1 MB, lives in PSRAM psram::map names; // nodes and long strings in PSRAM auto frame = psram::make_unique(); // your own objects too psram::fallback::vector safe; // PSRAM first, internal RAM if it fills up ``` Same std:: API you already use, just a different namespace. Nothing new to learn. How it works on the ESP32 Every container gets an allocator that calls heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT) directly, so it doesn't depend on Arduino's ps_malloc() and behaves the same in plain ESP-IDF. The allocator is stateless and always compares equal, which is exactly what std::map, std::set and the unordered containers need to rebind it to their internal node types (that was the bug in my old version). Over-aligned types go through heap_caps_aligned_alloc. The container header stays wherever you declare it, and everything it allocates lands in PSRAM. What you get every std container: vector, deque, list, map, set, the unordered ones, string make_unique, make_shared, and a base class so new YourThing lands in PSRAM pick a policy: PSRAM only, or PSRAM first with internal RAM as a safety net diagnostics: free space, largest block, "is this pointer in PSRAM?", heap integrity check header only, C++17, Arduino-ESP32 3.x and ESP-IDF 5.x Why I trust it Memory bugs are the worst kind, so I went a bit overboard. Sanitizers on the host, 11 device tests on ESP32 and ESP32-S3 in Espressif's QEMU with heap poisoning on (it fills PSRAM to the very last block and has both cores hammering maps at the same time), and every Arduino example gets booted in the emulator on every push. All green. Where you can help The emulator can't tell me how fast it is. If you have a WROVER or an S3 N8R8 on your desk, a quick benchmark would be super useful, especially quad vs octal PSRAM. Repo (MIT): https://github.com/aeljandro2/esp32-psram-stl I'm the author, happy to answer anything in the comments.   submitted by   /u/aeljandro2   to   r/esp32 [link]   [comments]