-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: decode function leaks memory #19
Comments
I found a work around. I am sharing for those who are in the same boat. It appears import array
from pathlib import Path
from miniaudio import DecodeError, ffi, lib
# get mp3 bytes
audio_bytes = Path('common_voice_en_20603299.mp3').read_bytes()
def mp3_read_f32(data: bytes) -> array:
'''Reads and decodes the whole mp3 audio data. Resulting sample format is 32 bits float.'''
config = ffi.new('drmp3_config *')
num_frames = ffi.new('drmp3_uint64 *')
memory = lib.drmp3_open_memory_and_read_pcm_frames_f32(data, len(data), config, num_frames, ffi.NULL)
if not memory:
raise DecodeError('cannot load/decode data')
try:
samples = array.array('f')
buffer = ffi.buffer(memory, num_frames[0] * config.channels * 4)
samples.frombytes(buffer)
return samples, config.sampleRate, config.channels
finally:
lib.drmp3_free(memory, ffi.NULL)
ffi.release(num_frames) # Release num_frames memory as a precaution.
for i in range(10000):
decoded_audio, sample_rate, channels = mp3_read_f32(audio_bytes) Here are OS stats for executing this.
The maximum memory usage is 196.12 MB. |
Could you perhaps retry with the latest version 1.36? There have been various updates to the miniaudio library the past few months |
I am using a workaround and I haven't tried with updated libraries. Were you able to recreate the bug? I have outlined all the steps to recreate it. |
not sure why you're tagging Cameron here :) |
I believe this is now fixed in the new release. As are the API bugs in the mp3_load functions. |
I am using miniaudio to decode MP3 bytes. I have a ML training task that decodes thousands of MP3 through many thousand training iterations. I have noticed the memory usage blew up and the OS started swapping.
I did a simple test and confirmed indeed miniaudio has memory leak.
Set file: common_voice_en_20603299.zip
10000 iterations uses up 3 GB memory:
I did further testing/debugging and concluded calling
lib.ma_decode_memory
iteratively causes memory leak.According to :
https://github.com/dr-soft/miniaudio/blob/b80f7f949152f93a0af499b4d6d07b8e60d0e673/extras/miniaudio_split/miniaudio.h#L4120
ma_free
probably needs to be called to free allocated memory like in this example: mackron/miniaudio#97 (comment)Maybe also it is a good idea to release frames and memory allocated by ffi.new call. (see https://cffi.readthedocs.io/en/latest/ref.html#ffi-release-and-the-context-manager)
Here is code to prove repeat calls to
ma_decode_memory
yields memory leak.The text was updated successfully, but these errors were encountered: