Author Topic: Memory management & memory leak  (Read 351 times)

rlibaert

  • Posts: 3
Memory management & memory leak
« on: 31 Aug '23 - 15:03 »
Hi there!

First of all, thank you for the work with the Bass library, it's really powerful!
I am having some troubles concerning the memory management of the various objects managed by the library and I am wondering whether or not my application has some memory leakage due to either the Bass library(ies) or a misuse of it.
 
My application is Docker based and the various tools available shows that the container keeps eating memory until complete depletion (takes a couple of hours). The application does not seem to mind not being able to allocate anymore and can keeps working for several days even in this state. However, it still gets OOM killed in the end.

I mostly use mp3 files as input and use BASS_StreamCreateFile with flags STREAM_DECODE, STREAM_PRESCAN & SAMPLE_FLOAT. Note that I don't use STREAM_AUTOFREE.
I also use BassFX for fine tuning the playback time by altering the Stream tempo (TempoCreate w/ STREAM_DECODE & FX_FREE_SOURCE).
The Streams are then added to a Mixer using BASS_Mixer_StreamAddChannel with flags MIXER_CHAN_DOWNMIX & MIXER_CHAN_PAUSE (mostly for fade in/out sequencing).
I extensively make use of the Syncs POS, END & SETPOS for aligning playback dates and detecting end of playback.
Once the Stream playback is over, the Stream is removed from the Mixer and free'd.

It seems that every time a new audio file is opened there is a bump in memory consumption. The amount of new memory required could not be correlated to the the size of the opened file.
I am able to run multiple instances of my application and feed them the same input, yielding to an (almost) identical output. However even in such cases one instance could be leaking while another does not.

The only consistent hint I could get is that disabling the STREAM_PRESCAN flag reduces the amount of memory consumed and when opening audio files. The application thus requires quite more time to reach its memory limit.

I understand this is a difficult issue as there is not much reliable information for troubleshooting. What I am looking for now is some advices of what to check next and perhaps some basic verifications on my library usage.

Thank you for reading & your time :)

Ian @ un4seen

  • Administrator
  • Posts: 25880
Re: Memory management & memory leak
« Reply #1 on: 31 Aug '23 - 16:05 »
One thing you could try is monitoring the BASS_CONFIG_HANDLES value (via BASS_GetConfig), which is the total number of HMUSIC/HRECORD/HSAMPLE/HSTREAM handles. If it's ever-increasing then you may have a handle leak, eg. some streams not being freed.

rlibaert

  • Posts: 3
Re: Memory management & memory leak
« Reply #2 on: 4 Sep '23 - 15:03 »
Thank you for the reply,

I tried and monitored the number of handles as you suggested, and it stays constant over time while I can still see an increase of memory consumption.
So I guess I can conclude that no handler is leaked (either FX handle, Mixer, Encoder nor Stream).

If you have any other ideas they are welcome :)

Ian @ un4seen

  • Administrator
  • Posts: 25880
Re: Memory management & memory leak
« Reply #3 on: 4 Sep '23 - 17:48 »
Encoders aren't actually included in the BASS_CONFIG_HANDLES value, so if you're using any them, you will need to track them separately or be sure to use the BASS_ENCODE_AUTOFREE flag. If encoders are all accounted for, then perhaps you can next try disabling parts of your code to narrow down what/where the apparent memory leak is?