[GH-ISSUE #141] [BUG] Memory Leak #21

Closed
opened 2026-02-27 08:11:28 +03:00 by kerem · 8 comments
Owner

Originally created by @owlwang on GitHub (Jul 15, 2022).
Original GitHub issue: https://github.com/kokarare1212/librespot-python/issues/141

Hi, thank you for making this very useful project, it's really awesome.

I'm experiencing a suspected memory leak during my use.

When load a large batch of songs, I can see the memory usage slowly growing.

I'd like to ask you if there are any areas I can look at to help locate a fix for this memory leak?

My environment is Ubuntu 20 and python 3.8.

sample code:


import sys

from librespot.core import Session
from librespot.metadata import TrackId
from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality

account_user = ''
account_passwd = ''
session = Session.Builder().user_pass(account_user, account_passwd).create()


def load_data_from_spotify(track_id):
    spotify_track_id = TrackId.from_uri('spotify:track:' + track_id)
    stream = session.content_feeder().load(spotify_track_id, VorbisOnlyAudioQuality(AudioQuality.NORMAL), False, None)
    audio_buffer = stream.input_stream.stream().read()

    # dosomething



Originally created by @owlwang on GitHub (Jul 15, 2022). Original GitHub issue: https://github.com/kokarare1212/librespot-python/issues/141 Hi, thank you for making this very useful project, it's really awesome. I'm experiencing a suspected memory leak during my use. When load a large batch of songs, I can see the memory usage slowly growing. I'd like to ask you if there are any areas I can look at to help locate a fix for this memory leak? My environment is Ubuntu 20 and python 3.8. sample code: ```python import sys from librespot.core import Session from librespot.metadata import TrackId from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality account_user = '' account_passwd = '' session = Session.Builder().user_pass(account_user, account_passwd).create() def load_data_from_spotify(track_id): spotify_track_id = TrackId.from_uri('spotify:track:' + track_id) stream = session.content_feeder().load(spotify_track_id, VorbisOnlyAudioQuality(AudioQuality.NORMAL), False, None) audio_buffer = stream.input_stream.stream().read() # dosomething ```
kerem 2026-02-27 08:11:28 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@kokarare1212 commented on GitHub (Jul 15, 2022):

Side note:

We DO NOT encourage piracy and DO NOT support any form of downloader/recorder designed with the help of this repository and in general anything that goes against the Spotify ToS.

<!-- gh-comment-id:1185462562 --> @kokarare1212 commented on GitHub (Jul 15, 2022): Side note: > We DO NOT encourage piracy and DO NOT support any form of downloader/recorder designed with the help of this repository and in general anything that goes against the Spotify ToS.
Author
Owner

@owlwang commented on GitHub (Jul 15, 2022):

Okay.

<!-- gh-comment-id:1185463329 --> @owlwang commented on GitHub (Jul 15, 2022): Okay.
Author
Owner

@owlwang commented on GitHub (Jul 21, 2022):

Hi, @kokarare1212
Hope you can notice this issue.
I've located the memory leak.
Python's GC doesn't seem to recycle the used buffer, so every time a song is loaded it stays in memory permanently.

Temporary solution

Add a method to clear the buffer in the InternalStream class

  class InternalStream(AbsChunkedInputStream):
         streamer: CdnManager.Streamer
+        def clear_buffer(self):
+            del self.streamer.buffer

Call the clear_buffer() method in the close method of the AbsChunkedInputStream class

 def close(self) -> None:
+    self.closed = True
     self.clear_buffer()
     with self.wait_lock:
         self.wait_lock.notify_all()

Then after each read(), call close() method.

def load_data_from_spotify(track_id):
    spotify_track_id = TrackId.from_uri('spotify:track:' + track_id)
    stream = session.content_feeder().load(spotify_track_id, VorbisOnlyAudioQuality(AudioQuality.NORMAL), False, None)
    input_stream = stream.input_stream.stream()
    audio_buffer = input_stream.read()
    # dosomething
    input_stream.close() # must add this, to clear buffer

Hope you can fix this problem in main branch code.

<!-- gh-comment-id:1191304017 --> @owlwang commented on GitHub (Jul 21, 2022): Hi, @kokarare1212 Hope you can notice this issue. I've located the memory leak. Python's GC doesn't seem to recycle the used buffer, so every time a song is loaded it stays in memory permanently. ## Temporary solution Add a method to clear the buffer in the **InternalStream** class ```python class InternalStream(AbsChunkedInputStream): streamer: CdnManager.Streamer + def clear_buffer(self): + del self.streamer.buffer ``` Call the clear_buffer() method in the close method of the **AbsChunkedInputStream** class ```python def close(self) -> None: + self.closed = True self.clear_buffer() with self.wait_lock: self.wait_lock.notify_all() ``` Then after each read(), call close() method. ```python def load_data_from_spotify(track_id): spotify_track_id = TrackId.from_uri('spotify:track:' + track_id) stream = session.content_feeder().load(spotify_track_id, VorbisOnlyAudioQuality(AudioQuality.NORMAL), False, None) input_stream = stream.input_stream.stream() audio_buffer = input_stream.read() # dosomething input_stream.close() # must add this, to clear buffer ``` Hope you can fix this problem in main branch code.
Author
Owner

@kokarare1212 commented on GitHub (Jul 21, 2022):

Yes, that is because the buffer has not been released.

<!-- gh-comment-id:1191321973 --> @kokarare1212 commented on GitHub (Jul 21, 2022): Yes, that is because the buffer has not been released.
Author
Owner

@owlwang commented on GitHub (Jul 21, 2022):

Thanks for your work!

<!-- gh-comment-id:1191327525 --> @owlwang commented on GitHub (Jul 21, 2022): Thanks for your work!
Author
Owner

@staniel359 commented on GitHub (Aug 30, 2022):

@kokarare1212 When will these changes be released?

<!-- gh-comment-id:1232049025 --> @staniel359 commented on GitHub (Aug 30, 2022): @kokarare1212 When will these changes be released?
Author
Owner

@kokarare1212 commented on GitHub (Aug 30, 2022):

OK, I have published.
https://github.com/kokarare1212/librespot-python/releases/tag/v0.0.6

<!-- gh-comment-id:1232195157 --> @kokarare1212 commented on GitHub (Aug 30, 2022): OK, I have published. https://github.com/kokarare1212/librespot-python/releases/tag/v0.0.6
Author
Owner

@staniel359 commented on GitHub (Aug 31, 2022):

@kokarare1212 Thank you.

<!-- gh-comment-id:1232759915 --> @staniel359 commented on GitHub (Aug 31, 2022): @kokarare1212 Thank you.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/librespot-python-kokarare1212#21
No description provided.