Using malloc() in SPIRAM


I’m new to the Arduino world, so excuse my lack of kowledge. I’m using the Wemos D1 mini (basically an esp8266) with the Arduino IDE.

For my project I’m using the very good ESP8266Audio library (GitHub - earlephilhower/ESP8266Audio: Arduino library to play MOD, WAV, FLAC, MIDI, RTTTL, MP3, and AAC files on I2S DACs or with a software emulated delta-sigma DAC on the ESP8266 and ESP32) which can stream an HTTP stream to the I2S pin of my board. Everything works fine, it’s just that I’m short of RAM. I’m already using the version of the library which use external SPIRAM and what I’d like to make is the following :

We’re already using the external RAM to buffer the MP3 and the other thing using lots of RAM is the decoding library. Searching in the examples and library, I’ve found that it declares the variables using the malloc() function.

Would it be possible to reserve the space allocated for the buffer with a malloc() function that goes in the external RAM ? (a function that would go like this : malloc(SizeOfCodec, SPIRAM).

Sorry if I’m unclear… But any help is welcome !


Although it's possible to write such a call, it won't help in your situation. What you're trying to do is using the SPI RAM as an extension of the internal RAM. But that would mean that every routine that would access that portion of memory allocated with the new malloc routine must be modified to read and write to that RAM using the library for your chip. So you cannot have a simple pointer to that memory location and reference that pointer in the code. But that's what your decoding library is expecting.

So to solve your problem, get a processor with more real RAM as the ESP-32 (512kB) (p.e. Wemos LOLIN32).