I'm using Librosa and I have a memory problem.
I have a lot of audio files, let's say a hundred. I process audio files one by one. Each audio file is loaded by chunks of 1 minute. Each chunk is processed before I move to the next chunk. This way, I know that I never have more than 60s of audio in memory at a given time. This allows me to avoid using too much memory during the whole process.
For some reason, the memory used by the process is growing over time.
Here is a simpler version of the code:
import librosa
import matplotlib.pyplot as plt
import os
import psutil
SAMPLING_RATE = 22050
N_FFT = 2048
HOP_LENGTH = 1024
def foo():
y, sr = librosa.load("clip_04.wav", sr=SAMPLING_RATE, offset=600, duration=60)
D = librosa.stft(y, n_fft=N_FFT, hop_length=HOP_LENGTH)
spec_mag = abs(D)
spec_db = librosa.amplitude_to_db(spec_mag)
return 42 # I return a constant to make sure the memory is (or should be) released
def main():
process = psutil.Process(os.getpid())
array = []
for i in range(100):
foo()
m = int(process.memory_info().rss / 1024**2)
array.append(m)
plt.figure()
plt.plot(array)
plt.xlabel('iterations')
plt.ylabel('MB')
plt.show()
if __name__ == '__main__':
main()
Using this code, the memory increases like this:
Is that normal? And if it is, is there a way to clear Librosa memory at each iteration?