ESP32-audioI2S
ESP32-audioI2S copied to clipboard
Memory leak when playing MP3 from SPIFFS multiple times
Hi @schreibfaul1,
I'm encountering a memory leak issue when playing MP3 files from SPIFFS using the example similar to plays_all_files_in_a_directory.ino
💻 System Info:
Board: P32-WROOM-32
Audio Library: ESP32-audioI2S
Filesystem: SPIFFS
PlatformIO Version: (e.g., 6.1.6 or 5.4.0)
Arduino Core Version: (e.g., 3.0.0 or 2.0.x)
PSRAM: Disable Test both with: 3.2.0 and 3.0.12 ESP32-audioI2S lib
🧪 Issue Description: I modified the example to play all files from a folder /voices on SPIFFS.
After each MP3 file finishes playing, I noticed the free heap memory drops by ~200 bytes, even though I'm calling audio.connecttoFS() again and freeing up the vector memory using free().
I confirmed this by logging ESP.getFreeHeap() at each audio_eof_mp3 callback.
This leak accumulates over time and causes the ESP32 to eventually run out of RAM.
🔍 Logs:
Listing directory: /voices
FILE: 0.mp3 SIZE: 7056
FILE: 1.mp3 SIZE: 7056
FILE: 2.mp3 SIZE: 7632
FILE: 3.mp3 SIZE: 7488
FILE: 4.mp3 SIZE: 7488
FILE: 5.mp3 SIZE: 7488
FILE: 6.mp3 SIZE: 7488
FILE: 7.mp3 SIZE: 7920
FILE: 9.mp3 SIZE: 7200
FILE: beginBdsd.mp3 SIZE: 82454
FILE: DaNhan.mp3 SIZE: 9072
FILE: endBdsd.mp3 SIZE: 21312
FILE: linh.mp3 SIZE: 7200
FILE: mario.mp3 SIZE: 65975
FILE: moi.mp3 SIZE: 7632
FILE: muoi.mp3 SIZE: 7632
FILE: nghin.mp3 SIZE: 7488
FILE: tram.mp3 SIZE: 7200
FILE: trieu.mp3 SIZE: 7200
num files 19playing /voices/0.mp3
[608835][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 111428 bytes memory beffore
[608849][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23368 bytes memory was freed
info buffers freed, free Heap: 134796 bytes
info Reading file: "/voices/0.mp3"
info MP3Decoder has been initialized, free Heap: 106988 bytes , free stack 6112 DWORDs
info Content-Length: 7056
info file has no ID3 tag, skip metadata
info Audio-Length: 7056
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "0.mp3"
eof_mp3 0.mp3
[RAM Check] Free heap: 111268 bytes
playing /voices/1.mp3
[610046][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 111268 bytes memory beffore
[610065][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23368 bytes memory was freed
info buffers freed, free Heap: 134636 bytes
info Reading file: "/voices/1.mp3"
info MP3Decoder has been initialized, free Heap: 106828 bytes , free stack 6112 DWORDs
info End of file "0.mp3"
info Content-Length: 7056
info file has no ID3 tag, skip metadata
info Audio-Length: 7056
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "1.mp3"
eof_mp3 1.mp3
[RAM Check] Free heap: 111124 bytes
playing /voices/2.mp3
[611264][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 111124 bytes memory beffore
[611283][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23368 bytes memory was freed
info buffers freed, free Heap: 134492 bytes
info Reading file: "/voices/2.mp3"
info MP3Decoder has been initialized, free Heap: 106668 bytes , free stack 6112 DWORDs
info End of file "1.mp3"
info Content-Length: 7632
info file has no ID3 tag, skip metadata
info Audio-Length: 7632
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "2.mp3"
eof_mp3 2.mp3
[RAM Check] Free heap: 110968 bytes
playing /voices/3.mp3
[612564][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 110968 bytes memory beffore
[612583][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23380 bytes memory was freed
info buffers freed, free Heap: 134348 bytes
info Reading file: "/voices/3.mp3"
info MP3Decoder has been initialized, free Heap: 106536 bytes , free stack 6112 DWORDs
info End of file "2.mp3"
info Content-Length: 7488
info file has no ID3 tag, skip metadata
info Audio-Length: 7488
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "3.mp3"
eof_mp3 3.mp3
[RAM Check] Free heap: 110836 bytes
playing /voices/4.mp3
[613861][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 110836 bytes memory beffore
[613880][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23368 bytes memory was freed
info buffers freed, free Heap: 134204 bytes
info Reading file: "/voices/4.mp3"
info MP3Decoder has been initialized, free Heap: 106392 bytes , free stack 6112 DWORDs
info End of file "3.mp3"
info Content-Length: 7488
info file has no ID3 tag, skip metadata
info Audio-Length: 7488
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "4.mp3"
eof_mp3 4.mp3
[RAM Check] Free heap: 110692 bytes
playing /voices/5.mp3
[615157][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 110692 bytes memory beffore
[615176][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23368 bytes memory was freed
info buffers freed, free Heap: 134060 bytes
info Reading file: "/voices/5.mp3"
info MP3Decoder has been initialized, free Heap: 106232 bytes , free stack 6112 DWORDs
info End of file "4.mp3"
info Content-Length: 7488
info file has no ID3 tag, skip metadata
info Audio-Length: 7488
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "5.mp3"
eof_mp3 5.mp3
[RAM Check] Free heap: 110544 bytes
playing /voices/6.mp3
[616454][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 110544 bytes memory beffore
[616473][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 133916 bytes
info Reading file: "/voices/6.mp3"
info MP3Decoder has been initialized, free Heap: 106088 bytes , free stack 6112 DWORDs
info End of file "5.mp3"
info Content-Length: 7488
info file has no ID3 tag, skip metadata
info Audio-Length: 7488
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "6.mp3"
eof_mp3 6.mp3
[RAM Check] Free heap: 110400 bytes
playing /voices/7.mp3
[617751][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 110400 bytes memory beffore
[617770][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 133772 bytes
info Reading file: "/voices/7.mp3"
info MP3Decoder has been initialized, free Heap: 105944 bytes , free stack 6112 DWORDs
info End of file "6.mp3"
info Content-Length: 7920
info file has no ID3 tag, skip metadata
info Audio-Length: 7920
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "7.mp3"
eof_mp3 7.mp3
[RAM Check] Free heap: 110256 bytes
playing /voices/9.mp3
[619095][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 110256 bytes memory beffore
[619114][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 133628 bytes
info Reading file: "/voices/9.mp3"
info MP3Decoder has been initialized, free Heap: 105800 bytes , free stack 6112 DWORDs
info End of file "7.mp3"
info Content-Length: 7200
info file has no ID3 tag, skip metadata
info Audio-Length: 7200
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "9.mp3"
eof_mp3 9.mp3
[RAM Check] Free heap: 110112 bytes
playing /voices/beginBdsd.mp3
[620317][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 110112 bytes memory beffore
[620336][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 133484 bytes
info Reading file: "/voices/beginBdsd.mp3"
info MP3Decoder has been initialized, free Heap: 105648 bytes , free stack 6112 DWORDs
info End of file "9.mp3"
info Content-Length: 82454
info ID3 framesSize: 37316
info ID3 version: 2.4
info ID3 normal frames
id3data Title: Mix by audio-joiner.com
id3data Artist: TiếngĐộng.com
id3data SettingsForEncoding: Lavf61.7.100
info Audio-Length: 45138
info stream ready
info syncword found at pos 0
info MPEG-2.5, Layer I
info Channels: 2
info SampleRate: 44100
info BitsPerSample: 16
info BitRate: 128000
info Closing audio file "beginBdsd.mp3"
eof_mp3 beginBdsd.mp3
[RAM Check] Free heap: 109944 bytes
playing /voices/DaNhan.mp3
[623269][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 109944 bytes memory beffore
[623279][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23368 bytes memory was freed
info buffers freed, free Heap: 133312 bytes
info Reading file: "/voices/DaNhan.mp3"
info MP3Decoder has been initialized, free Heap: 105484 bytes , free stack 6112 DWORDs
info End of file "beginBdsd.mp3"
info Content-Length: 9072
info file has no ID3 tag, skip metadata
info Audio-Length: 9072
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "DaNhan.mp3"
eof_mp3 DaNhan.mp3
[RAM Check] Free heap: 109816 bytes
playing /voices/endBdsd.mp3
[624816][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 109816 bytes memory beffore
[624836][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23368 bytes memory was freed
info buffers freed, free Heap: 133184 bytes
info Reading file: "/voices/endBdsd.mp3"
info MP3Decoder has been initialized, free Heap: 105352 bytes , free stack 6112 DWORDs
info End of file "DaNhan.mp3"
info Content-Length: 21312
info file has no ID3 tag, skip metadata
info Audio-Length: 21312
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "endBdsd.mp3"
eof_mp3 endBdsd.mp3
[RAM Check] Free heap: 109668 bytes
playing /voices/linh.mp3
[628439][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 109668 bytes memory beffore
[628459][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 133040 bytes
info Reading file: "/voices/linh.mp3"
info MP3Decoder has been initialized, free Heap: 105208 bytes , free stack 6112 DWORDs
info End of file "endBdsd.mp3"
info Content-Length: 7200
info file has no ID3 tag, skip metadata
info Audio-Length: 7200
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "linh.mp3"
eof_mp3 linh.mp3
[RAM Check] Free heap: 109524 bytes
playing /voices/mario.mp3
[629662][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 109524 bytes memory beffore
[629681][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 132896 bytes
info Reading file: "/voices/mario.mp3"
info MP3Decoder has been initialized, free Heap: 105064 bytes , free stack 6112 DWORDs
info End of file "linh.mp3"
info Content-Length: 65975
info ID3 framesSize: 2110
info ID3 version: 2.3
info ID3 normal frames
info Audio-Length: 63865
info stream ready
info syncword found at pos 0
info MPEG-2.5, Layer I
info Channels: 2
info SampleRate: 44100
info BitsPerSample: 16
info BitRate: 320000
info ID3 Version 1.1
id3data Comment: QuickSounds.com
id3data Track Number: 0
info Closing audio file "mario.mp3"
eof_mp3 mario.mp3
[RAM Check] Free heap: 109380 bytes
playing /voices/moi.mp3
[631322][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 109380 bytes memory beffore
[631339][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 132752 bytes
info Reading file: "/voices/moi.mp3"
info MP3Decoder has been initialized, free Heap: 104924 bytes , free stack 6112 DWORDs
info End of file "mario.mp3"
info Content-Length: 7632
info file has no ID3 tag, skip metadata
info Audio-Length: 7632
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "moi.mp3"
eof_mp3 moi.mp3
[RAM Check] Free heap: 109248 bytes
playing /voices/muoi.mp3
[632622][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 109248 bytes memory beffore
[632641][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 132620 bytes
info Reading file: "/voices/muoi.mp3"
info MP3Decoder has been initialized, free Heap: 104788 bytes , free stack 6112 DWORDs
info End of file "moi.mp3"
info Content-Length: 7632
info file has no ID3 tag, skip metadata
info Audio-Length: 7632
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "muoi.mp3"
eof_mp3 muoi.mp3
[RAM Check] Free heap: 109092 bytes
playing /voices/nghin.mp3
[633922][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 109092 bytes memory beffore
[633941][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 132464 bytes
info Reading file: "/voices/nghin.mp3"
info MP3Decoder has been initialized, free Heap: 104632 bytes , free stack 6112 DWORDs
info End of file "muoi.mp3"
info Content-Length: 7488
info file has no ID3 tag, skip metadata
info Audio-Length: 7488
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "nghin.mp3"
eof_mp3 nghin.mp3
[RAM Check] Free heap: 108948 bytes
playing /voices/tram.mp3
[635219][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 108948 bytes memory beffore
[635239][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 132320 bytes
info Reading file: "/voices/tram.mp3"
info MP3Decoder has been initialized, free Heap: 104488 bytes , free stack 6112 DWORDs
info End of file "nghin.mp3"
info Content-Length: 7200
info file has no ID3 tag, skip metadata
info Audio-Length: 7200
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "tram.mp3"
eof_mp3 tram.mp3
[RAM Check] Free heap: 108804 bytes
playing /voices/trieu.mp3
[636441][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 108804 bytes memory beffore
[636460][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 132176 bytes
info Reading file: "/voices/trieu.mp3"
info MP3Decoder has been initialized, free Heap: 104344 bytes , free stack 6112 DWORDs
info End of file "tram.mp3"
info Content-Length: 7200
info file has no ID3 tag, skip metadata
info Audio-Length: 7200
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "trieu.mp3"
eof_mp3 trieu.mp3
[637663][E][main.cpp:131] vector_clear_and_shrink(): [RAM Check] Free heap: 108812 bytes
info End of file "trieu.mp3"
Listing directory: /voices
FILE: 0.mp3 SIZE: 7056
FILE: 1.mp3 SIZE: 7056
FILE: 2.mp3 SIZE: 7632
FILE: 3.mp3 SIZE: 7488
FILE: 4.mp3 SIZE: 7488
FILE: 5.mp3 SIZE: 7488
FILE: 6.mp3 SIZE: 7488
FILE: 7.mp3 SIZE: 7920
FILE: 9.mp3 SIZE: 7200
FILE: beginBdsd.mp3 SIZE: 82454
FILE: DaNhan.mp3 SIZE: 9072
FILE: endBdsd.mp3 SIZE: 21312
FILE: linh.mp3 SIZE: 7200
FILE: mario.mp3 SIZE: 65975
FILE: moi.mp3 SIZE: 7632
FILE: muoi.mp3 SIZE: 7632
FILE: nghin.mp3 SIZE: 7488
FILE: tram.mp3 SIZE: 7200
FILE: trieu.mp3 SIZE: 7200
num files 19playing /voices/0.mp3
[637825][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 108020 bytes memory beffore
[637839][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23372 bytes memory was freed
info buffers freed, free Heap: 131392 bytes
info Reading file: "/voices/0.mp3"
info MP3Decoder has been initialized, free Heap: 103592 bytes , free stack 6112 DWORDs
info Content-Length: 7056
info file has no ID3 tag, skip metadata
info Audio-Length: 7056
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "0.mp3"
eof_mp3 0.mp3
[RAM Check] Free heap: 107864 bytes
playing /voices/1.mp3
[639035][E][mp3_decoder.cpp:1643] MP3Decoder_FreeBuffers(): MP3Decoder: 107864 bytes memory beffore
[639054][E][mp3_decoder.cpp:1654] MP3Decoder_FreeBuffers(): MP3Decoder: 23360 bytes memory was freed
info buffers freed, free Heap: 131224 bytes
info Reading file: "/voices/1.mp3"
info MP3Decoder has been initialized, free Heap: 103408 bytes , free stack 6112 DWORDs
info End of file "0.mp3"
info Content-Length: 7056
info file has no ID3 tag, skip metadata
info Audio-Length: 7056
info stream ready
info syncword found at pos 0
info MPEG-1, Layer I
info Channels: 1
info SampleRate: 24000
info BitsPerSample: 16
info BitRate: 48000
info Closing audio file "1.mp3"
eof_mp3 1.mp3
[RAM Check] Free heap: 107704 bytes
📂 Code Snippet:
void audio_eof_mp3(const char *info)
{
Serial.print("eof_mp3 ");
Serial.println(info);
if (v_audioContent.size() == 0)
{
vector_clear_and_shrink(v_audioContent); // free memory
return;
}
const char *s = (const char *)v_audioContent[v_audioContent.size() - 1];
uint32_t freeHeap = ESP.getFreeHeap();
Serial.printf("[RAM Check] Free heap: %u bytes\n", freeHeap);
Serial.printf("playing %s\n", s);
audio.connecttoFS(SPIFFS, s);
v_audioContent.pop_back();
}
✅ What I've Tried:
Verified I’m freeing strduped memory.
Verified audio.loop() is called continuously.
Tried calling audio.stopSong() before connecttoFS() (but still leaks).
Monitored heap memory over multiple files.
🙏 Request: Could this be a memory leak inside connecttoFS() or the decoder object that doesn’t get fully cleaned after playback?
Would appreciate if you could take a look or suggest a workaround.
Thanks again for your awesome work!