NAudio icon indicating copy to clipboard operation
NAudio copied to clipboard

Memory consumption of SampleProviderConverterBase instances

Open exGensImpl opened this issue 1 year ago • 1 comments

When reading audio using classes inherited from SampleProviderConverterBase, it allocates a persistent buffer (sourceBuffer) that can be of unlimited size, depending on the requested sample count.

After even a single reading of a large number of samples, the buffer created for this remains in memory until the ISampleProvider will be collected by the GC, even if a buffer of this size is no longer required. In some use cases, it may result to a kind of memory leak.

Would it make sense to limit maximum size of the sourceBuffer and read data from the IWaveProvider buffer by buffer if the requested sample count is greater?

Of course, it simply can be done at ISampleProvider call side, but since the buffer is an implementation detail of SampleProviderConverterBase, I think it would be better to do it in this class

exGensImpl avatar Dec 02 '23 01:12 exGensImpl

It's designed that way to minimise the need for allocating memory or requiring garbage collection during playback. The expected way to use the Read method on ISampleProvider is to make many calls for fairly short buffers (e.g. 10-200ms) rather than trying to read many minutes of audio in one call.

markheath avatar Dec 18 '23 11:12 markheath