Skip to content
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

Apparent memory leak during normal MP3 playback #1827

Closed
jrowberg opened this issue Oct 23, 2019 · 37 comments · Fixed by #1929
Closed

Apparent memory leak during normal MP3 playback #1827

jrowberg opened this issue Oct 23, 2019 · 37 comments · Fixed by #1929
Labels
A-audio Area: Audio layer C-bug Category: This is a bug

Comments

@jrowberg
Copy link

I have what appears to be a memory leak on a Mopidy installation on a RasPi, booting and running over NFS. Initially I thought the problem was that I had no swap set up, so I added some, but it continues to happen.

The extremely odd thing about this is that the NFS root image is identical to three other RasPi servers handling sound systems in other rooms of the house, but those other three systems have run for weeks without exhibiting any issues. The failing unit freezes up nearly once a day. The only difference among all four servers is the exact content of the music library, but all libraries are pretty small...less than 2k songs, far less in some cases. All songs are in MP3 format, in case it makes a difference.

Here's a screenshot of top that I had left running to see the system state when it freezes. Note the insane load and 0k free swap, and the fact that kswapd is pegging the CPU, but also that mopidy is the one using all the memory. Normally the mopidy process is down around 5-6% mem usage.

image

I'm already using a local SQLite db. I can run a local scan from the command line with no issues.

After the above screenshot, I power-cycled the RasPi and got back into a top session so I could pay more attention to it. The %MEM value reported has slowly crept upward from about 5% to 42%, where it is now. I expect it will continue to rise if I do nothing.

In contrast, one of the stable RasPi audio servers has been running for over two weeks solid and playing audio for at least 72 hours without stopping, and the mopidy process is at 6.7% usage. It even has a slightly larger library.

...and after a while longer, the memory use did indeed continue to creep upwards (at 50% when I acted), and I ran sudo service mopidy force-reload as a test to see if that would help. It did--mopidy memory usage dropped down near zero for a moment, and then back to the expected ~5%. No full service restart needed, though I assume that also would have done the same.

The different RasPis involved all report Raspberry Pi 3 Model B Rev 1.2 from the output of cat /sys/firmware/devicetree/base/model. They are running over NFS mounted root folders that are exact copies of each other, except for the hostname. The contents of /etc/mopidy/mopidy.conf are identical. The memory usage takes many hours to grow significantly, but it will eventually eat up all available RAM and swap space.

I have confirmed as well that the memory use stops increasing when audio is stopped (not sure about paused though). The following set of graphs shows RAM and SWAP usage on the problem system over a 24-hour period, including two full PLAY-STOP cycles (hours long) with one final PLAY event at the end to confirm. I did another force-reload immediately after this to avoid a complete system freeze and drop RAM/swap usage back down to normal levels.

image

You can see how the RAM is eaten up first (from 50% to 80%) during the first cycle, then shortly after the second cycle starts, it eats the last bit of safe RAM (from 80% to 85%) and then switches to consuming swap (from 20% to 80%) until stopped. Finally, it ticks up again at a consistent rate about 15 minutes before the end of the graph, which is where I started playback again.

Note, as far as I can tell, pausing playback in the Mopidy/moped web interface has the same effect as stopping it in this regard, i.e. it stops eating memory.

Host platform is Raspbian on a RasPi 3B v1.2:

Linux lucas-audio 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux

It is possible that this issue is related to either #1750 (most likely) or #1648 (less likely).

@kingosticks kingosticks added A-audio Area: Audio layer C-bug Category: This is a bug labels Oct 24, 2019
@kingosticks
Copy link
Member

Thanks for this great description. Could you also include the output of mopidyctl deps so we know exactly what versions of software you have.

The different RasPis involved all report Raspberry Pi 3 Model B Rev 1.2

Are they all using the same type of output device e.g. all using the Pi's onboard audio?

I ran sudo service mopidy force-reload as a test to see if that would help. It did--mopidy memory usage dropped down near zero for a moment, and then back to the expected

FYI Mopidy doesn't support reload so this will simply restart the process.

Note, as far as I can tell, pausing playback in the Mopidy/moped web interface has the same effect as stopping it in this regard, i.e. it stops eating memory.

This is interesting and potentially important.

We need to be able to reproduce this so it would be good to know if this is reliably reproducible with the same n set of tracks. Can you then reproduce by repeating a single track? Are you able to get a debug log and maybe even a GStreamer log? It's much easier to get these when running Mopidy as your user than as a service.

@jrowberg
Copy link
Author

jrowberg commented Oct 30, 2019

Here's the output of sudo mopidtyctl deps:

pi@lucas-audio:~ $ sudo mopidyctl deps
Running "/usr/bin/mopidy --config /usr/share/mopidy/conf.d:/etc/mopidy/mopidy.conf deps" as user mopidy
/usr/lib/python2.7/dist-packages/mopidy/ext.py:202: PkgResourcesDeprecationWarning: Parameters to load are deprecated.  Call .resolve and .require separately.
  extension_class = entry_point.load(require=False)
Executable: /usr/bin/mopidy
Platform: Linux-4.19.66-v7+-armv7l-with-debian-10.1
Python: CPython 2.7.16 from /usr/lib/python2.7
Mopidy: 2.2.2 from /usr/lib/python2.7/dist-packages
Mopidy-ALSAMixer: 1.1.1 from /usr/lib/python2.7/dist-packages
Mopidy-TuneIn: 0.4.1 from /usr/lib/python2.7/dist-packages
Mopidy-Local-SQLite: 1.0.0 from /usr/lib/python2.7/dist-packages
Mopidy-Youtube: 2.0.0 from /usr/lib/python2.7/dist-packages
  requests>=2.2.1: 2.21.0 from /usr/lib/python2.7/dist-packages
  pafy>=0.3.35: not found
  Mopidy>=1.0: 2.2.2 from /usr/lib/python2.7/dist-packages
  Pykka>=1.1: 1.2.1 from /usr/lib/python2.7/dist-packages
Mopidy-SomaFM: 1.1.0 from /usr/lib/python2.7/dist-packages
Mopidy-Moped: 0.7.1 from /usr/local/lib/python2.7/dist-packages
  setuptools: 41.4.0 from /usr/local/lib/python2.7/dist-packages
  Mopidy>=1.0.0: 2.2.2 from /usr/lib/python2.7/dist-packages
Mopidy-SoundCloud: 2.1.0 from /usr/lib/python2.7/dist-packages
GStreamer: 1.14.4.0 from /usr/lib/python2.7/dist-packages/gi
  Detailed information:
    Python wrapper: python-gi 3.30.4
    Relevant elements:
      Found:
        uridecodebin
        souphttpsrc
        appsrc
        alsasink
        osssink
        oss4sink
        pulsesink
        id3demux
        id3v2mux
        lamemp3enc
        mpegaudioparse
        mpg123audiodec
        vorbisdec
        vorbisenc
        vorbisparse
        oggdemux
        oggmux
        oggparse
        flacdec
        flacparse
        shout2send
      Not found:
        flump3dec
        mad
pi@lucas-audio:~ $

EDIT: I confirmed that the output is identical on another RasPi where the problem does not occur.

I installed mopidy using sudo apt install mopidy (along with other packages), and there were a few plugins that required the use of pip since they aren't in the Raspbian package database.

All of the RasPis in question are using the HiFiBerry AMP+ for audio output, driving some 8-ohm ceiling-mounted speakers. Volume levels are reasonably quiet, and all RasPis share the same fixed 12V/30A DC power supply, so I doubt there are any power issues.

I haven't been able to dig into replicating it with or without particular tracks, but I'll see what I can do in the next few days.

@jrowberg
Copy link
Author

Two notable updates on this. First, despite changing nothing whatsoever (media library, Ubuntu packages, hardware, usage style), the problem seems to have disappeared on the previously failing RasPi. I have no idea why. I'm 99% sure the system isn't auto-updating itself, and nobody except for me would have done it on purpose.

Second, it came to my attention that v2.2.2 is not the latest, and my apt-based install was using Raspbian repos instead of the official Mopidy repos. I've updated to v2.3.1, and it looks like it's still working fine (better, in fact, since the web interface was finicky before w/r/t track changes and playback state).

I'll keep my eyes on it for a while longer, but this may be a non-issue. 🤷‍♂

@jodal
Copy link
Member

jodal commented Nov 21, 2019

Thank you for the update!

@liamw9534
Copy link

liamw9534 commented Nov 21, 2019 via email

@kingosticks
Copy link
Member

I spent some time trying to reproduce this and while at the beginning, I thought I did (never to the point of memory exhaustion, just increasing beyond the normal amount), when I tried to employ the gst tracing tools they didn't show anything. I played around with some more tracing/logging but then was definitely unable to reproduce it. After restoring everything back to default I found I was then entirely enable to reproduce anything like this. In other words, a total failure.

@liamw9534
Copy link

liamw9534 commented Nov 21, 2019 via email

@LeoCal
Copy link

LeoCal commented Mar 15, 2020

Hello,

I've recently noticed the same thing, namely a steady increase of the used memory as the playback goes. In particular, memory usage starts at about 30MB; I add about 110 songs to the tracklist from a playlist and start playing. At each song being played, memory increases of about 2-3MB (I guess because mopidy is loading it into RAM). However, GC never seems to kick in as I've easily seen the memory grow up to 270MB. Even if I stop playback and resume the day after, it keeps growing from the same level. At some point, it even starts using swap.
Another data point: even if I reproduce again a track that I recently played from the tracklist, the memory goes a little bit up (like 0.5MB), which was a bit unexpected to me.

As I'm using the same system to perform other activities that keep the device on 24/7, the easy workaround I've found is to restart mopidy process when I quit music playback (to leave it clean for when it starts next time). However, I think Mopidy is quite cool idea, so it would deserve a real fix for this.

I'm using Mopidy v.3.0.1.

@kingosticks
Copy link
Member

Please provide more detailed information about the file types they are seeing this with? Are these mp3, wav, aac? As I said, I was unable to reproduce. We also need, full mopidy deps and mopidy config output for any report.

@LeoCal
Copy link

LeoCal commented Mar 15, 2020

Hello,

thanks for your quick reply on this. I only have mp3s in my playlist.

Here you are the output from the two commands you asked:

$ sudo mopidyctl deps
Running "/usr/bin/mopidy --config /usr/share/mopidy/conf.d:/etc/mopidy/mopidy.conf deps" as user mopidy
Executable: /usr/bin/mopidy
Platform: Linux-4.4.50-v7+-armv7l-with-debian-10.3
Python: CPython 3.7.3 from /usr/lib/python3.7
Mopidy: 3.0.1 from /usr/lib/python3/dist-packages
Mopidy-ALSAMixer: 2.0.0 from /usr/local/lib/python3.7/dist-packages
  Pykka: 2.0.2 from /usr/lib/python3/dist-packages
  Mopidy: 3.0.1 from /usr/lib/python3/dist-packages
  setuptools: 40.8.0 from /usr/lib/python3/dist-packages
  pyalsaaudio: 0.8.4 from /usr/local/lib/python3.7/dist-packages
Mopidy-Mobile: 1.9.1 from /usr/local/lib/python3.7/dist-packages
  setuptools: 40.8.0 from /usr/lib/python3/dist-packages
  Mopidy: 3.0.1 from /usr/lib/python3/dist-packages
Mopidy-MPD: 3.0.0 from /usr/local/lib/python3.7/dist-packages
  Pykka: 2.0.2 from /usr/lib/python3/dist-packages
  Mopidy: 3.0.1 from /usr/lib/python3/dist-packages
  setuptools: 40.8.0 from /usr/lib/python3/dist-packages
GStreamer: 1.14.4.0 from /usr/lib/python3/dist-packages/gi
  Detailed information:
    Python wrapper: python-gi 3.30.4
    Relevant elements:
      Found:
        uridecodebin
        souphttpsrc
        appsrc
        alsasink
        osssink
        oss4sink
        pulsesink
        id3demux
        id3v2mux
        lamemp3enc
        mpegaudioparse
        mpg123audiodec
        vorbisdec
        vorbisenc
        vorbisparse
        oggdemux
        oggmux
        oggparse
        flacdec
        flacparse
        shout2send
      Not found:
        flump3dec
        mad
$ sudo mopidyctl config
Running "/usr/bin/mopidy --config /usr/share/mopidy/conf.d:/etc/mopidy/mopidy.conf config" as user mopidy
[core]
cache_dir = /var/cache/mopidy
config_dir = /etc/mopidy
data_dir = /var/lib/mopidy
max_tracklist_length = 10000
restore_state = false

[logging]
verbosity = 0
format = %(levelname)-8s [%(threadName)s] %(name)s %(message)s
color = false
config_file =

[audio]
mixer = software
mixer_volume =
output = autoaudiosink
buffer_time =

[proxy]
scheme =
hostname =
port =
username =
password =

[mpd]
enabled = true
hostname = 127.0.0.1
port = 6600
password =
max_connections = 20
connection_timeout = 60
zeroconf = Mopidy MPD server on $hostname
command_blacklist =
  listall
  listallinfo
default_playlist_scheme = m3u

[mobile]
enabled = true
title = Mopidy Mobile on $hostname
ws_url =

[alsamixer]
enabled = true
card = 1
control = Master
min_volume = 0
max_volume = 100
volume_scale = cubic

[file]
enabled = true
media_dirs =
  $XDG_MUSIC_DIR|Music
  ~/|Home
excluded_file_extensions =
  .directory
  .html
  .jpeg
  .jpg
  .log
  .nfo
  .pdf
  .png
  .txt
  .zip
show_dotfiles = false
follow_symlinks = false
metadata_timeout = 1000

[http]
enabled = true
hostname = 0.0.0.0
port = 6680
zeroconf = Mopidy HTTP server on $hostname
allowed_origins =
csrf_protection = true
default_app = mopidy

[m3u]
enabled = true
base_dir =
default_encoding = latin-1
default_extension = .m3u8
playlists_dir =

[softwaremixer]
enabled = true

[stream]
enabled = true
protocols =
  http
  https
  mms
  rtmp
  rtmps
  rtsp
metadata_blacklist =
timeout = 5000

@kingosticks
Copy link
Member

And are using the file extension or local extension to play these mp3 files?

@LeoCal
Copy link

LeoCal commented Mar 15, 2020

To be honest, I don't know. Is there a way to find out what's going on under the hood? Any specific command I need to run or anything special to look in the logs to find out?

I think it doesn't make much difference, but just to describe my setup better - I'm playing files from a locally mounted folder from my NAS. To start reproducing I usually use mpc, which is interfacing with Mopidy-MPD extension. After that, I fully control the Mopidy instance using its embedded simple HTTP interface.

@kingosticks
Copy link
Member

kingosticks commented Mar 15, 2020

If you just want to provide a full debug log somewhere that'd be useful. Although what is the "embedded simple http interface" you mentioned?

@LeoCal
Copy link

LeoCal commented Mar 15, 2020

Sorry, I meant I'm using the Mopidy-HTTP bundled with Mopidy. On top of that, I'm using the extension Mopidy-Mobile.

Back to your initial question (file vs local), I quickly glanced through the logs and it seems I'm using the former. Here's an excerpt of my log (I'm running mopidy as a systemd service if that matters):

Mar 15 12:10:09 pihome systemd[1]: Starting Mopidy music server...
Mar 15 12:10:09 pihome systemd[1]: Started Mopidy music server.
Mar 15 12:10:11 pihome mopidy[25099]: INFO     [MainThread] mopidy.__main__ Starting Mopidy 3.0.1
Mar 15 12:10:11 pihome mopidy[25099]: INFO     [MainThread] mopidy.config Loading config from builtin defaults
Mar 15 12:10:11 pihome mopidy[25099]: INFO     [MainThread] mopidy.config Loading config from file:///usr/share/mopidy/conf.d/mopidy.conf
Mar 15 12:10:11 pihome mopidy[25099]: INFO     [MainThread] mopidy.config Loading config from file:///etc/mopidy/mopidy.conf
Mar 15 12:10:11 pihome mopidy[25099]: INFO     [MainThread] mopidy.config Loading config from command line options
Mar 15 12:10:12 pihome mopidy[25099]: INFO     [MainThread] mopidy.__main__ Enabled extensions: alsamixer, file, softwaremixer, http, m3u, stream, mobile, mpd
Mar 15 12:10:12 pihome mopidy[25099]: INFO     [MainThread] mopidy.__main__ Disabled extensions: none
Mar 15 12:10:13 pihome mopidy[25099]: INFO     [MainThread] mopidy.commands Starting Mopidy mixer: SoftwareMixer
Mar 15 12:10:13 pihome mopidy[25099]: INFO     [MainThread] mopidy.commands Starting Mopidy audio
Mar 15 12:10:13 pihome mopidy[25099]: INFO     [MainThread] mopidy.commands Starting Mopidy backends: FileBackend, M3UBackend, StreamBackend
Mar 15 12:10:13 pihome mopidy[25099]: INFO     [Audio-2] mopidy.audio.actor Audio output set to "autoaudiosink"
Mar 15 12:10:13 pihome mopidy[25099]: INFO     [MainThread] mopidy.commands Starting Mopidy core
Mar 15 12:10:13 pihome mopidy[25099]: INFO     [MainThread] mopidy.commands Starting Mopidy frontends: MpdFrontend, HttpFrontend
Mar 15 12:10:13 pihome mopidy[25099]: INFO     [MainThread] mopidy_mpd.actor MPD server running at [::ffff:127.0.0.1]:6600
Mar 15 12:10:13 pihome mopidy[25099]: INFO     [HttpFrontend-9] mopidy.http.actor HTTP server running at [::ffff:0.0.0.0]:6680
Mar 15 12:10:13 pihome mopidy[25099]: INFO     [MainThread] mopidy.commands Starting GLib mainloop
Mar 15 13:28:07 pihome mopidy[25099]: INFO     [MpdSession-10] mopidy_mpd.session New MPD connection from [::ffff:127.0.0.1]:53536
Mar 15 13:28:07 pihome mopidy[25099]: WARNING  [FileBackend-3] mopidy.file.library Failed looking up file:///var/lib/mopidy/media/Varie/Dance%20Monkey%20-%20Tones%20And%20I.mp3: gst-resource-error-quark: Resource not found. (3)
Mar 15 13:28:07 pihome mopidy[25099]: WARNING  [FileBackend-3] mopidy.file.library Failed looking up file:///var/lib/mopidy/media/Varie/RITMO%20%28Bad%20Boys%20For%20Life%29%20-%20The%20Black%20Eyed%20Peas%2C%20J%20Balvin.mp3: gst-resource-error-quark: Resource not found. (3)
Mar 15 13:28:07 pihome mopidy[25099]: WARNING  [FileBackend-3] mopidy.file.library Failed looking up file:///var/lib/mopidy/media/Varie/Someone%20You%20Loved%20-%20Lewis%20Capaldi.mp3: gst-resource-error-quark: Resource not found. (3)
Mar 15 13:28:07 pihome mopidy[25099]: WARNING  [FileBackend-3] mopidy.file.library Failed looking up file:///var/lib/mopidy/media/Varie/Don%27t%20Start%20Now%20-%20Dua%20Lipa.mp3: gst-resource-error-quark: Resource not found. (3)
Mar 15 13:28:07 pihome mopidy[25099]: WARNING  [FileBackend-3] mopidy.file.library Failed looking up file:///var/lib/mopidy/media/Varie/Memories%20-%20Maroon%205.mp3: gst-resource-error-quark: Resource not found. (3)
Mar 15 13:28:07 pihome mopidy[25099]: WARNING  [FileBackend-3] mopidy.file.library Failed looking up file:///var/lib/mopidy/media/Varie/Yummy%20-%20Justin%20Bieber.mp3: gst-resource-error-quark: Resource not found. (3)
Mar 15 13:28:07 pihome mopidy[25099]: WARNING  [FileBackend-3] mopidy.file.library Failed looking up file:///var/lib/mopidy/media/Varie/Trampoline%20-%20Shaed%20ft.%20Zaym.mp3: gst-resource-error-quark: Resource not found. (3)
Mar 15 13:28:07 pihome mopidy[25099]: WARNING  [FileBackend-3] mopidy.file.library Failed looking up file:///var/lib/mopidy/media/Varie/Chandelier%20-%20SIA.mp3: gst-resource-error-quark: Resource not found. (3)
Mar 15 13:28:08 pihome mopidy[25099]: WARNING  [FileBackend-3] mopidy.file.library Failed looking up file:///var/lib/mopidy/media/Varie/7%20rings%20-%20Ariana%20Grande.mp3: gst-resource-error-quark: Resource not found. (3)
...

That repeated warning about "Resource not found" related to gstreamer looks suspicious; however, despite the warning, I can confirm all files are playing absolutely fine.

@LeoCal
Copy link

LeoCal commented Mar 16, 2020

Hello,

I've just spent 15 minutes looking at the mopidy code for the first time, therefore I cannot call myself an expert :-)

However, I see that mopidy is using the GStreamer python bindings to drive media playback. By looking at the code at mopidy/audio/actor.py, I see where you allocate resources for the playbin, but I could not find where you "unreference" it to free resources... am I missing something?

Again, this issue is not a blocker for my deployment as I decided to restart mopidy process every time I stop using the player. However, I'd be glad to help you finding the root cause of the memory leak of this beautiful product.

@kingosticks
Copy link
Member

We setup the playbin once as part of the Audio actor starting during Mopidy startup. We reuse the playbin between songs. The playbin is "torn down" when Mopidy shuts down (not that deallocating it matters at that point). Audio.on_start() does not run every time a track starts playing, if that is what you are thinking.

Keep in mind that I tried to reproduce this issue before and I failed. I'm guessing it's related to the media types you are playing.

@LeoCal
Copy link

LeoCal commented Mar 16, 2020

Ok, please let me know if I can do something to help you identify the issue.

As I said, mine is a very common use case, where regular MP3s are played from a locally mounted NAS folder.

@kingosticks
Copy link
Member

An experiment where you moved the files locally would be helpful. And an experiment where you use the Local backend rather than the File backend. And if you could include the full debug log that might also be helpful for me to try again to reproduce.

@LeoCal
Copy link

LeoCal commented Mar 17, 2020

Hello,

I have experimented copying the same MP3 files locally and I see exactly the same behaviour, namely memory increases by 2-3 MBs every time a song gets played. I have collected the memory usage of mopidy after playing each song - you can find all the numbers with File and Local here below. I have also collected the logs as you requested.

From the data I gathered, I'm under the impression the leak doesn't happen in the file/local backends, but rather in mopidy itself (or maybe in GStreamer).

One more thing: all my MP3s contain a JPG artwork that displays correctly on other media player (e.g. iTunes). Not sure if mopidy/Gstreamer leaks might be due to the presence of the artwork in the songs.

My 2 cents

Mopidy-File (with local files)

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  553 mopidy    20   0  138652  33592  14608 S   0.0   3.5   0:04.42 mopidy
  553 mopidy    20   0  168608  40224  14968 S   0.0   4.2   0:11.52 mopidy
  553 mopidy    20   0  242244  45704  18108 S  12.2   4.8   0:19.50 mopidy
  553 mopidy    20   0  244676  48360  18108 S  19.2   5.1   0:22.86 mopidy
  553 mopidy    20   0  246124  49680  18108 S  17.8   5.2   0:26.04 mopidy
  553 mopidy    20   0  246900  50472  18108 S  11.6   5.3   0:28.10 mopidy
  553 mopidy    20   0  248272  51792  18108 S  27.7   5.5   0:29.73 mopidy
  553 mopidy    20   0  248272  51792  18108 S  16.6   5.5   0:32.02 mopidy
  553 mopidy    20   0  250284  52848  18108 S  10.9   5.6   0:33.56 mopidy
  553 mopidy    20   0  250620  53112  18108 S  11.6   5.6   0:35.47 mopidy
  553 mopidy    20   0  252160  54696  18108 S  19.9   5.8   0:37.12 mopidy
  553 mopidy    20   0  252608  55488  18108 S  18.2   5.9   0:50.58 mopidy
  553 mopidy    20   0  252608  55488  18108 S  11.9   5.9   0:53.85 mopidy

--- Start again same tracklist

  553 mopidy    20   0  252608  56300  18436 S   8.3   5.9   0:56.98 mopidy
  553 mopidy    20   0  253184  56828  18436 S   9.3   6.0   1:00.00 mopidy
  553 mopidy    20   0  255572  59452  18436 S  23.1   6.3   1:01.60 mopidy
  553 mopidy    20   0  257020  61036  18436 S  28.1   6.4   1:03.85 mopidy
  553 mopidy    20   0  257796  61828  18436 S   8.3   6.5   1:05.40 mopidy
  553 mopidy    20   0  259168  63148  18436 S  29.9   6.7   1:08.08 mopidy
  553 mopidy    20   0  259168  63148  18436 S  12.1   6.7   1:10.38 mopidy
  553 mopidy    20   0  260156  63940  18436 S  30.9   6.8   1:12.39 mopidy
  553 mopidy    20   0  260492  64468  18436 S  19.5   6.8   1:13.86 mopidy
  553 mopidy    20   0  262032  66052  18436 S  30.4   7.0   1:15.14 mopidy
  553 mopidy    20   0  262480  66316  18436 S  10.9   7.0   1:17.23 mopidy
  553 mopidy    20   0  262480  66316  18436 S   9.6   7.0   1:18.74 mopidy

--- Clear tracklist and add the same songs again

  553 mopidy    20   0  209336  71404  18508 S   0.0   7.5   1:27.68 mopidy
  553 mopidy    20   0  244496  73372  18512 S  23.1   7.7   1:31.36 mopidy
  553 mopidy    20   0  245708  74652  18664 S  30.0   7.9   1:34.30 mopidy

After that, I've disabled "file" backend and have switched to "local" backend - same behaviour in terms of memory:

Mopidy-Local (with local files)

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 2533 mopidy    20   0  140568  34872  14876 S   0.0   3.7   0:06.06 mopidy
 2533 mopidy    20   0  213588  41164  18940 S  12.9   4.3   0:07.94 mopidy
 2533 mopidy    20   0  214264  43024  19364 S  30.7   4.5   0:09.58 mopidy
 2533 mopidy    20   0  215244  44064  19404 R  28.1   4.7   0:11.04 mopidy
 2533 mopidy    20   0  216660  44624  19404 S  26.4   4.7   0:12.86 mopidy
 2533 mopidy    20   0  217680  45680  19404 S  30.7   4.8   0:14.44 mopidy
 2533 mopidy    20   0  218180  46208  19404 S  28.6   4.9   0:15.66 mopidy
 2533 mopidy    20   0  219676  47528  19404 S  25.1   5.0   0:17.27 mopidy
 2533 mopidy    20   0  220280  48200  19404 S  26.0   5.1   0:18.40 mopidy
 2533 mopidy    20   0  220280  48200  19404 S  27.7   5.1   0:19.88 mopidy
 2533 mopidy    20   0  221332  49256  19404 S  29.7   5.2   0:24.60 mopidy
 2533 mopidy    20   0  222828  50840  19404 S  15.4   5.4   0:26.13 mopidy

--- reproduce tracklist again

 2533 mopidy    20   0  251852  54004  19404 S  11.5   5.7   0:30.76 mopidy
 2533 mopidy    20   0  251996  54268  19404 S  28.3   5.7   0:32.96 mopidy
 2533 mopidy    20   0  253512  55848  19420 S  27.6   5.9   0:34.14 mopidy
 2533 mopidy    20   0  253512  55848  19420 S  28.2   5.9   0:35.27 mopidy
 2533 mopidy    20   0  254504  56904  19420 S  30.3   6.0   0:36.50 mopidy
 2533 mopidy    20   0  254836  57168  19420 S  29.8   6.0   0:37.75 mopidy
 2533 mopidy    20   0  256376  58752  19420 S  29.4   6.2   0:38.90 mopidy
 2533 mopidy    20   0  256796  59016  19420 S  27.0   6.2   0:40.35 mopidy
 2533 mopidy    20   0  256796  59280  19420 S  27.0   6.3   0:41.53 mopidy
 2533 mopidy    20   0  256796  59280  19420 S  11.8   6.3   0:43.41 mopidy
 2533 mopidy    20   0  259520  62184  19420 S  30.4   6.6   0:45.85 mopidy
 2533 mopidy    20   0  261736  64560  19420 S  32.0   6.8   0:47.47 mopidy

--- clear tracklist and add the same songs again

 2533 mopidy    20   0  261736  64560  19420 S  32.0   6.8   0:47.47 mopidy
 2533 mopidy    20   0  237148  66264  19624 S  26.1   7.0   0:50.03 mopidy

Mopidy Logs

Mar 17 17:48:07 pihome systemd[1]: Started Mopidy music server.
Mar 17 17:48:10 pihome mopidy[2429]: INFO     2020-03-17 17:48:09,862 [2429:MainThread] mopidy.__main__
Mar 17 17:48:10 pihome mopidy[2429]:   Starting Mopidy 3.0.1
Mar 17 17:48:10 pihome mopidy[2429]: INFO     2020-03-17 17:48:10,075 [2429:MainThread] mopidy.config
Mar 17 17:48:10 pihome mopidy[2429]:   Loading config from builtin defaults
Mar 17 17:48:10 pihome mopidy[2429]: INFO     2020-03-17 17:48:10,089 [2429:MainThread] mopidy.config
Mar 17 17:48:10 pihome mopidy[2429]:   Loading config from file:///etc/mopidy/mopidy.conf
Mar 17 17:48:10 pihome mopidy[2429]: INFO     2020-03-17 17:48:10,092 [2429:MainThread] mopidy.config
Mar 17 17:48:10 pihome mopidy[2429]:   Loading config from file:///etc/mopidy/mopidy.conf
Mar 17 17:48:10 pihome mopidy[2429]: INFO     2020-03-17 17:48:10,096 [2429:MainThread] mopidy.config
Mar 17 17:48:10 pihome mopidy[2429]:   Loading config from command line options
Mar 17 17:48:10 pihome mopidy[2429]: INFO     2020-03-17 17:48:10,128 [2429:MainThread] mopidy.internal.path
Mar 17 17:48:10 pihome mopidy[2429]:   Creating dir file:///var/lib/mopidy/.cache/mopidy
Mar 17 17:48:10 pihome mopidy[2429]: INFO     2020-03-17 17:48:10,130 [2429:MainThread] mopidy.internal.path
Mar 17 17:48:10 pihome mopidy[2429]:   Creating dir file:///var/lib/mopidy/.config/mopidy
Mar 17 17:48:10 pihome mopidy[2429]: INFO     2020-03-17 17:48:10,131 [2429:MainThread] mopidy.internal.path
Mar 17 17:48:10 pihome mopidy[2429]:   Creating dir file:///var/lib/mopidy/.local/share/mopidy
Mar 17 17:48:11 pihome mopidy[2429]: INFO     2020-03-17 17:48:11,334 [2429:MainThread] mopidy.__main__
Mar 17 17:48:11 pihome mopidy[2429]:   Enabled extensions: alsamixer, http, m3u, stream, softwaremixer, local, mpd, mobile
Mar 17 17:48:11 pihome mopidy[2429]: INFO     2020-03-17 17:48:11,338 [2429:MainThread] mopidy.__main__
Mar 17 17:48:11 pihome mopidy[2429]:   Disabled extensions: file
Mar 17 17:48:11 pihome mopidy[2429]: INFO     2020-03-17 17:48:11,836 [2429:MainThread] mopidy.commands
Mar 17 17:48:11 pihome mopidy[2429]:   Starting Mopidy mixer: SoftwareMixer
Mar 17 17:48:11 pihome mopidy[2429]: INFO     2020-03-17 17:48:11,847 [2429:MainThread] mopidy.commands
Mar 17 17:48:11 pihome mopidy[2429]:   Starting Mopidy audio
Mar 17 17:48:11 pihome mopidy[2429]: INFO     2020-03-17 17:48:11,861 [2429:MainThread] mopidy.commands
Mar 17 17:48:11 pihome mopidy[2429]:   Starting Mopidy backends: LocalBackend, M3UBackend, StreamBackend
Mar 17 17:48:11 pihome mopidy[2429]: INFO     2020-03-17 17:48:11,867 [2429:MainThread] mopidy.internal.path
Mar 17 17:48:11 pihome mopidy[2429]:   Creating dir file:///var/lib/mopidy/.local/share/mopidy/local
Mar 17 17:48:11 pihome mopidy[2429]: INFO     2020-03-17 17:48:11,922 [2429:MainThread] mopidy.internal.path
Mar 17 17:48:11 pihome mopidy[2429]:   Creating dir file:///var/lib/mopidy/.local/share/mopidy/m3u
Mar 17 17:48:11 pihome mopidy[2429]: INFO     2020-03-17 17:48:11,949 [2429:Audio-2] mopidy.audio.actor
Mar 17 17:48:11 pihome mopidy[2429]:   Audio output set to "autoaudiosink"
Mar 17 17:48:12 pihome mopidy[2429]: INFO     2020-03-17 17:48:12,003 [2429:MainThread] mopidy.commands
Mar 17 17:48:12 pihome mopidy[2429]:   Starting Mopidy core
Mar 17 17:48:12 pihome mopidy[2429]: INFO     2020-03-17 17:48:12,055 [2429:MainThread] mopidy.commands
Mar 17 17:48:12 pihome mopidy[2429]:   Starting Mopidy frontends: MpdFrontend, HttpFrontend
Mar 17 17:48:12 pihome mopidy[2429]: INFO     2020-03-17 17:48:12,061 [2429:MainThread] mopidy_mpd.actor
Mar 17 17:48:12 pihome mopidy[2429]:   MPD server running at [::ffff:127.0.0.1]:6600
Mar 17 17:48:12 pihome mopidy[2429]: INFO     2020-03-17 17:48:12,070 [2429:HttpFrontend-9] mopidy.http.actor
Mar 17 17:48:12 pihome mopidy[2429]:   HTTP server running at [::ffff:0.0.0.0]:6680
Mar 17 17:48:12 pihome mopidy[2429]: INFO     2020-03-17 17:48:12,073 [2429:MainThread] mopidy.commands
Mar 17 17:48:12 pihome mopidy[2429]:   Starting GLib mainloop
Mar 17 17:48:12 pihome mopidy[2429]: INFO     2020-03-17 17:48:12,132 [2429:HttpServer] mopidy.internal.path
Mar 17 17:48:12 pihome mopidy[2429]:   Creating dir file:///var/lib/mopidy/.local/share/mopidy/http
Mar 17 17:49:45 pihome mopidy[2429]: ERROR    2020-03-17 17:49:45,330 [2429:LocalBackend-3] mopidy_local.library
Mar 17 17:49:45 pihome mopidy[2429]:   Error browsing local:directory?type=track: no such table: track
Mar 17 17:50:43 pihome mopidy[2429]: INFO     2020-03-17 17:50:43,114 [2429:MainThread] mopidy.commands
Mar 17 17:50:43 pihome mopidy[2429]:   GLib mainloop got SIGTERM. Exiting...
Mar 17 17:50:43 pihome mopidy[2429]: INFO     2020-03-17 17:50:43,116 [2429:MainThread] mopidy.commands
Mar 17 17:50:43 pihome mopidy[2429]:   Stopping Mopidy frontends
Mar 17 17:50:43 pihome systemd[1]: Stopping Mopidy music server...
Mar 17 17:50:43 pihome mopidy[2429]: INFO     2020-03-17 17:50:43,140 [2429:MainThread] mopidy.commands
Mar 17 17:50:43 pihome mopidy[2429]:   Stopping Mopidy core
Mar 17 17:50:43 pihome mopidy[2429]: INFO     2020-03-17 17:50:43,145 [2429:MainThread] mopidy.commands
Mar 17 17:50:43 pihome mopidy[2429]:   Stopping Mopidy backends
Mar 17 17:50:43 pihome mopidy[2429]: INFO     2020-03-17 17:50:43,152 [2429:MainThread] mopidy.commands
Mar 17 17:50:43 pihome mopidy[2429]:   Stopping Mopidy audio
Mar 17 17:50:43 pihome mopidy[2429]: INFO     2020-03-17 17:50:43,158 [2429:MainThread] mopidy.commands
Mar 17 17:50:43 pihome mopidy[2429]:   Stopping Mopidy mixer
Mar 17 17:50:43 pihome systemd[1]: mopidy.service: Succeeded.
Mar 17 17:50:43 pihome systemd[1]: Stopped Mopidy music server.
Mar 17 17:50:43 pihome systemd[1]: Starting Mopidy music server...
Mar 17 17:50:43 pihome systemd[1]: Started Mopidy music server.
Mar 17 17:50:46 pihome mopidy[2533]: INFO     2020-03-17 17:50:45,833 [2533:MainThread] mopidy.__main__
Mar 17 17:50:46 pihome mopidy[2533]:   Starting Mopidy 3.0.1
Mar 17 17:50:46 pihome mopidy[2533]: INFO     2020-03-17 17:50:46,045 [2533:MainThread] mopidy.config
Mar 17 17:50:46 pihome mopidy[2533]:   Loading config from builtin defaults
Mar 17 17:50:46 pihome mopidy[2533]: INFO     2020-03-17 17:50:46,059 [2533:MainThread] mopidy.config
Mar 17 17:50:46 pihome mopidy[2533]:   Loading config from file:///etc/mopidy/mopidy.conf
Mar 17 17:50:46 pihome mopidy[2533]: INFO     2020-03-17 17:50:46,063 [2533:MainThread] mopidy.config
Mar 17 17:50:46 pihome mopidy[2533]:   Loading config from file:///etc/mopidy/mopidy.conf
Mar 17 17:50:46 pihome mopidy[2533]: INFO     2020-03-17 17:50:46,066 [2533:MainThread] mopidy.config
Mar 17 17:50:46 pihome mopidy[2533]:   Loading config from command line options
Mar 17 17:50:47 pihome mopidy[2533]: INFO     2020-03-17 17:50:47,366 [2533:MainThread] mopidy.__main__
Mar 17 17:50:47 pihome mopidy[2533]:   Enabled extensions: http, mobile, mpd, m3u, softwaremixer, alsamixer, local, stream
Mar 17 17:50:47 pihome mopidy[2533]: INFO     2020-03-17 17:50:47,367 [2533:MainThread] mopidy.__main__
Mar 17 17:50:47 pihome mopidy[2533]:   Disabled extensions: file
Mar 17 17:50:47 pihome mopidy[2533]: INFO     2020-03-17 17:50:47,883 [2533:MainThread] mopidy.commands
Mar 17 17:50:47 pihome mopidy[2533]:   Starting Mopidy mixer: SoftwareMixer
Mar 17 17:50:47 pihome mopidy[2533]: INFO     2020-03-17 17:50:47,894 [2533:MainThread] mopidy.commands
Mar 17 17:50:47 pihome mopidy[2533]:   Starting Mopidy audio
Mar 17 17:50:47 pihome mopidy[2533]: INFO     2020-03-17 17:50:47,908 [2533:MainThread] mopidy.commands
Mar 17 17:50:47 pihome mopidy[2533]:   Starting Mopidy backends: LocalBackend, M3UBackend, StreamBackend
Mar 17 17:50:47 pihome mopidy[2533]: INFO     2020-03-17 17:50:47,993 [2533:Audio-2] mopidy.audio.actor
Mar 17 17:50:47 pihome mopidy[2533]:   Audio output set to "autoaudiosink"
Mar 17 17:50:48 pihome mopidy[2533]: INFO     2020-03-17 17:50:48,046 [2533:MainThread] mopidy.commands
Mar 17 17:50:48 pihome mopidy[2533]:   Starting Mopidy core
Mar 17 17:50:48 pihome mopidy[2533]: INFO     2020-03-17 17:50:48,098 [2533:MainThread] mopidy.commands
Mar 17 17:50:48 pihome mopidy[2533]:   Starting Mopidy frontends: MpdFrontend, HttpFrontend
Mar 17 17:50:48 pihome mopidy[2533]: INFO     2020-03-17 17:50:48,103 [2533:MainThread] mopidy_mpd.actor
Mar 17 17:50:48 pihome mopidy[2533]:   MPD server running at [::ffff:127.0.0.1]:6600
Mar 17 17:50:48 pihome mopidy[2533]: INFO     2020-03-17 17:50:48,111 [2533:HttpFrontend-9] mopidy.http.actor
Mar 17 17:50:48 pihome mopidy[2533]:   HTTP server running at [::ffff:0.0.0.0]:6680
Mar 17 17:50:48 pihome mopidy[2533]: INFO     2020-03-17 17:50:48,114 [2533:MainThread] mopidy.commands
Mar 17 17:50:48 pihome mopidy[2533]:   Starting GLib mainloop
Mar 17 17:52:29 pihome mopidy[2533]: ERROR    2020-03-17 17:52:29,384 [2533:LocalBackend-3] mopidy_local.library
Mar 17 17:52:29 pihome mopidy[2533]:   Error browsing local:directory?type=album: no such table: album
Mar 17 17:52:37 pihome mopidy[2533]: ERROR    2020-03-17 17:52:37,898 [2533:LocalBackend-3] mopidy_local.library
Mar 17 17:52:37 pihome mopidy[2533]:   Error browsing local:directory?type=artist: no such table: artist
Mar 17 17:53:17 pihome mopidy[2533]: ERROR    2020-03-17 17:53:17,684 [2533:LocalBackend-3] mopidy_local.library
Mar 17 17:53:17 pihome mopidy[2533]:   Error browsing local:directory?max-age=604800: no such table: track
Mar 17 17:53:19 pihome mopidy[2533]: ERROR    2020-03-17 17:53:19,909 [2533:LocalBackend-3] mopidy_local.library
Mar 17 17:53:19 pihome mopidy[2533]:   Error browsing local:directory?max-age=2592000: no such table: track
Mar 17 17:53:22 pihome mopidy[2533]: ERROR    2020-03-17 17:53:22,210 [2533:LocalBackend-3] mopidy_local.library
Mar 17 17:53:22 pihome mopidy[2533]:   Error browsing local:directory?type=artist&role=performer: no such table: artist
Mar 17 17:53:24 pihome mopidy[2533]: ERROR    2020-03-17 17:53:24,206 [2533:LocalBackend-3] mopidy_local.library
Mar 17 17:53:24 pihome mopidy[2533]:   Error browsing local:directory?type=genre: no such table: search
Mar 17 17:53:26 pihome mopidy[2533]: ERROR    2020-03-17 17:53:26,324 [2533:LocalBackend-3] mopidy_local.library
Mar 17 17:53:26 pihome mopidy[2533]:   Error browsing local:directory?type=artist&role=composer: no such table: artist

@LeoCal
Copy link

LeoCal commented Mar 26, 2020

Hello, I was just wondering if the info and logs I provided helped in the end?

I managed to reproduce this with any MP3 of my library, so I doubt this is related to the format. Have you tried with your MP3s? I could ship a couple of MP3s not covered by copyright for you to try if needed.

@matevzperko
Copy link

Hello. I am experiencing the same memory leak issues.
I am playing only local mp3 files.

I switched from Raspberry PI 3 1GB to Raspberry PI 4 1GB.

All testing was performed with clean install of raspbian and mopidy with mostly default settingss. Only file:media_dirs was changed to local folder and core:restore_state was enabled.

Because memory limitations, problem starts after a few hours of playing. I tested with quickly skipping tracks and pause/resume.

On RPI3 with Stretch Raspbian, with version 2.2.3 installed, I had no problems at all. Memory consumption stayed around 50-60MB.

On RPI4 and RPI3 with Buster Raspbian I tried the latest version 3.0.2 and older 2.2.2 version. With both, there is an extreme memory leak issue. If I add 90 tracks to tracklist, memory jumps to around 150MB. If mopidy is restarted it starts with around 40MB.
Then after every track starts playing, even if only pausing and resuming, consumption jumps and eventualy raspberry runs out of memory and starts swapping or mopidy just crashes.

On RPI3 (Stretch) with mopidy 2.2.3, python 2.7.13 and gstreamer 1.10.4.0 is used.
On RPI4/RPI3 (Buster) with mopidy 2.2.2, python 2.7.16 and gstreamer 10.14.4.0 is used.

@Rhalah
Copy link

Rhalah commented Aug 2, 2020

Hello!

I'm trying to use an old RPi 1 B+ as a mopidy music player and it seems I'm having a similar (if not the same) problem. I'm new to it, but can help with testing (I can reinstall and test things here, if it helps).

The problem: RPi plays some songs just ok (I use an android MPD app), but suddenly after some time the song hangs and the green led (I/O activity) keeps bright on (without even blinking). Can't login through ssh but the RPi answers to ping.

After reading the above messages, I tried htop and can also see that if I keep changing to the next song and play/pause some times, the memory keeps going up. In the first test it went to almost 80% (from 35%), but I couldn't hang the system.
I'll read more and try to see if I can reproduce the problem.

@Commod0re
Copy link

Commod0re commented Aug 6, 2020

I am experiencing the same problem with Mopidy 3.0.2 on a Raspberry Pi Zero W with a Phat Beat DAC (which ultimately uses the hifiberry alsa drivers). It's a fresh install of Raspbian 10, with Mopidy installed from the apt.mopidy.com repo

This has been happening consistently since I set this thing up a week ago

in my case I'm using the Mopidy-Subidy backend primarily. Watching mopidy's memory use in htop it seems like garbage collection is happening inconsistently - on some track changes memory use will reduce by a bit, but often it just grows. If I leave it playing for a few hours, it consistently runs out of memory and begins to fail at playback. By that point there's not enough memory for ssh to spawn a shell for me. Today I'm going to try to let it get to that point while I'm already connected to ssh.

here's the output of sudo mopidyctl deps:

Running "/usr/bin/mopidy --config /usr/share/mopidy/conf.d:/etc/mopidy/mopidy.conf deps" as user mopidy
Executable: /usr/bin/mopidy
Platform: Linux-4.19.118+-armv6l-with-debian-10.4
Python: CPython 3.7.3 from /usr/lib/python3.7
Mopidy: 3.0.2 from /usr/lib/python3/dist-packages
Mopidy-TuneIn: 1.0.0 from /usr/lib/python3/dist-packages
Mopidy-MPD: 3.0.0 from /usr/lib/python3/dist-packages
Mopidy-MusicBox-Webclient: 3.1.0 from /usr/local/lib/python3.7/dist-packages
  Pykka: 2.0.2 from /usr/lib/python3/dist-packages
  Mopidy: 3.0.2 from /usr/lib/python3/dist-packages
  setuptools: 40.8.0 from /usr/lib/python3/dist-packages
Mopidy-ALSAMixer: 2.0.0 from /usr/lib/python3/dist-packages
Mopidy-Subidy: 1.0.0 from /usr/local/lib/python3.7/dist-packages
  py-sonic: 0.7.7 from /usr/local/lib/python3.7/dist-packages
  Pykka: 2.0.2 from /usr/lib/python3/dist-packages
  Mopidy: 3.0.2 from /usr/lib/python3/dist-packages
  setuptools: 40.8.0 from /usr/lib/python3/dist-packages
GStreamer: 1.14.4.0 from /usr/lib/python3/dist-packages/gi
  Detailed information: 
    Python wrapper: python-gi 3.30.4
    Relevant elements:
      Found:
        uridecodebin
        souphttpsrc
        appsrc
        alsasink
        osssink
        oss4sink
        pulsesink
        id3demux
        id3v2mux
        lamemp3enc
        mpegaudioparse
        mpg123audiodec
        vorbisdec
        vorbisenc
        vorbisparse
        oggdemux
        oggmux
        oggparse
        flacdec
        flacparse
        shout2send
      Not found:
        flump3dec
        mad

I'm not sure how to use gst-leaks at all - can someone provide an example? I tried the example in this slide deck but it didn't work for me

there's nothing useful so far in journalctl - where can I enable/view real debug logs? this is driving me nuts and I'll gladly provide whatever information you need to help track it down

@kingosticks
Copy link
Member

kingosticks commented Aug 6, 2020

Mopidy's debug logging can be enabled as per https://docs.mopidy.com/en/latest/config/#confval-logging-verbosity. I don't think GST debug logging will be fruitful.

I had another ago a couple of weeks back trying to reproduce this on my desktop with absolutely no luck. I have a possible memory leak theory but without being able to reproduce it's not much good. However, if anyone can reproduce this consistently on their system I do have a simple experiment which you can try.

Edit /usr/lib/python3/dist-packages/mopidy/audio/tags.py line 78 from

    buf = sample.get_buffer()

to

    return None

So the complete function will then read:

def _extract_sample_data(sample):
    return None
    if not buf:
        return None
    return buf.extract_dup(0, buf.get_size())

Then be sure to restart Mopidy.

@Commod0re
Copy link

I'll try it right now

@Commod0re
Copy link

so far it's looking much improved - the resident set shrinks consistently on track change now

I'll keep an eye on it for a couple more hours and see if it holds true

@Rhalah
Copy link

Rhalah commented Aug 6, 2020

It seems this will solve my problem too.
I can reproduce the problem in 10 minutes or less with a little set of 4 flac songs (have no idea why the problem affects some flacs but not others).
Since I changed the line as shown above, no more hangs. I'm past 1h with these set of flacs and with a bigger set that used to show the problem also.
Average memory use is lower too (from usually 20% to 30% before to less than 15% now).

@Commod0re
Copy link

yep after playing a playlist for 2 hours that consistently triggers this memory attrition condition, I can confirm that this completely solved the problem for me. I see Mopidy's resident set fluctuate between 78MB and 86MB now, depending on the size of the track being played

@kingosticks
Copy link
Member

@Rhalah do those particular flac files have embedded album artwork? You can use gst-discover-1.0 <filename> to display the metadata.

@adamcik
Copy link
Member

adamcik commented Aug 6, 2020

Quickly looking at https://lazka.github.io/pgi-docs/index.html#Gst-1.0/classes/Object.html#Gst.Object.get_name and various bits of the GST python bindings, I suspect there are a bunch of places we need to add GLib.free calls :( Not just the big obvious one here. Fixing this particular issue is probably the more important, but we probably should go through all the GST calls and see where we fall short and/or double check if the bindings do any of this for "free" in other cases....

@adamcik
Copy link
Member

adamcik commented Aug 7, 2020

beetbox/audioread#84 looks like the same problem with buf.extract_dup(0, buf.get_size()) should we try the same fix as they did?

@kingosticks
Copy link
Member

Yes. That is the workaround we'd need. Again, I saw zero behaviour different in Ubuntu 20.04 with the original code and that workaround code, but I'm starting to think this is a bug in the Buster bindings that was then fixed later.

@adamcik
Copy link
Member

adamcik commented Aug 9, 2020

Also meant to add https://mail.gnome.org/archives/python-hackers-list/2013-February/msg00002.html which is a bit old but does confirm my assumption that the bindings should really be handling this for us.

@kingosticks
Copy link
Member

kingosticks commented Aug 9, 2020

I got a Pi 4 running Buster and generated a load of fake embedded cover art for some songs and can reproduce now. And I'm pretty sure it's a difference in the 30.x (Buster) to 36.x bindings (Ubuntu 20.04). I had a look again at the changelogs, but this time for pygobject (rather than gst or gst-overrides) and there's a couple of entries there that sound like good candidates (maybe this ??).

Either way, we should go for the workaround but it's worth trying to understand why as there are probably more leaks in the Buster version of the bindings because of this. I still don't understand how gst_buffer_extract_dup was fixed, the other buffer functions have ownership annotations e.g.

* Returns: (transfer full): the new #GstBuffer.

but gst_buffer_extract_dup still has nothing obvious in that department.

EDIT: The gir files show the annotations do somehow work out fine and it gets transfer-ownership="full" in both versions, so maybe the above linked bug was the sole culprit. I should know better than to speculate on GIR/GStreamer and just leave it to the experts.

@Rhalah
Copy link

Rhalah commented Aug 10, 2020

@Rhalah do those particular flac files have embedded album artwork? You can use gst-discover-1.0 <filename> to display the metadata.

Sorry for the delay, @kingosticks !

Both the sets (the one that plays ok and the one that triggers the problem) have algum art, but the ok ones have even more meta-data.

I'll put below the output of mediainfo command from one track from each ser (Bob Marley set is the problematic one).

mediainfo Bob\ Marley\ -\ Africa\ Unite.flac > bob-au.txt

Results in a txt file with:

General
Complete name : Bob Marley - Africa Unite.flac
Format : FLAC
Format/Info : Free Lossless Audio Codec
File size : 20.6 MiB
Duration : 2 min 55 s
Overall bit rate mode : Variable
Overall bit rate : 984 kb/s
Album : Survival
Track name : Africa Unite
Track name/Position : 06
Performer : Bob Marley & The Wailers
Genre : Reggae
Description : 2001 Island remaster UICY-9549
Recorded date : 1979
Cover : Yes / Yes
Cover type : Cover (front) / Cover (back)
Cover MIME : image/jpeg / image/jpeg

Audio
Format : FLAC
Format/Info : Free Lossless Audio Codec
Duration : 2 min 55 s
Bit rate mode : Variable
Bit rate : 877 kb/s
Channel(s) : 2 channels
Channel positions : Front: L R
Sampling rate : 44.1 kHz
Bit depth : 16 bits
Stream size : 18.4 MiB (89%)
Writing library : libFLAC 1.2.1 (UTC 2007-09-17)

mediainfo A-Ha\ -\ Hunting\ High\ And\ Low.flac > aha-hhl.txt

Results in a txt file with:

General
Complete name : A-Ha - Hunting High And Low.flac
Format : FLAC
Format/Info : Free Lossless Audio Codec
File size : 25.1 MiB
Duration : 3 min 43 s
Overall bit rate mode : Variable
Overall bit rate : 944 kb/s
Album replay gain : -3.82 dB
Album replay gain peak : 0.979950
Album : Hunting High and Low
Album/Performer : a-ha
Part : 1
Part/Total : 1 / 1
Track name : Hunting High and Low
Track name/Position : 3
Track name/Total : 10 / 10
Performer : a-ha
Label : Reprise Records / Warner Bros. Records
Genre : Pop; New Wave; Synth Pop
Recorded date : 1985-06-01
Cover : Yes
Cover type : Cover (front)
Cover MIME : image/jpeg
Comment : Norwegian; Oslo (Songs-DB_Custom3) / 1980s (Songs-DB_Custom1) / Male Vocalists; Oldies; Synth; Nostalgia (Songs-DB_Custom2) / Romantic; Love; Chillout (Songs-DB_Occasion)
ACOUSTID_ID : 53de822a-2dbf-4edd-a7a1-7f756ac5b3b6
ALBUMARTISTSORT : a-ha
ARTISTSORT : a-ha
ASIN : B000002L7Y
BARCODE : 075992530026
CATALOGNUMBER : 542894T / W2 25300
COMPILATION : false
FMPS_PLAYCOUNT : 3
FMPS_RATING : -1
FMPS_RATING_AMAROK_SCORE : 0.83
GROUPING : Pop
MEDIA : CD
MOOD : Melancholic; Mellow
MUSICBRAINZ_RELEASEGROUPID : af7347df-ef5a-3c41-826d-d884f8916c17
ORIGINALDATE : 1985-06-01
ORIGINALYEAR : 1985
RELEASECOUNTRY : US
RELEASESTATUS : official
RELEASETYPE : album
SCRIPT : Latn

Audio
Format : FLAC
Format/Info : Free Lossless Audio Codec
Duration : 3 min 43 s
Bit rate mode : Variable
Bit rate : 942 kb/s
Channel(s) : 2 channels
Channel positions : Front: L R
Sampling rate : 44.1 kHz
Bit depth : 16 bits
Replay gain : -3.85 dB
Replay gain peak : 0.979950
Stream size : 25.0 MiB (100%)
Writing library : libFLAC 1.1.2 (UTC 2005-02-05)
Language : English

  • Just to update status of things here: no more problems since the changed line.

@kingosticks
Copy link
Member

I was hoping for output from gst-discover-1.0 as that shows the size of the embedded artwork. But since the audio is 89% of the first file, compared to 100% in the second file, maybe 11% of the first file is artwork data and would tally with it being worse. But I'm really just guessing there.

@Rhalah
Copy link

Rhalah commented Aug 10, 2020

No problem!

Below is the output of gst-discover-1.0:

  • Bob Marley - Africa Unite.flac

Topology:
audio: Free Lossless Audio Codec (FLAC)

Properties:
Duration: 0:02:55.626666666
Seekable: yes
Live: no
Tags:
description: 2001 Island remaster UICY-9549
artist: Bob Marley & The Wailers
title: Africa Unite
album: Survival
datetime: 1979
track number: 6
genre: Reggae
image: buffer of 532122 bytes, type: image/jpeg, width=(int)948, height=(int)948, sof-marker=(int)0
audio codec: Free Lossless Audio Codec (FLAC)

  • A-ha - Hunting High and Low

Topology:
audio: Free Lossless Audio Codec (FLAC)

Properties:
Duration: 0:03:43.053333333
Seekable: yes
Live: no
Tags:
image: buffer of 51691 bytes, type: image/jpeg, width=(int)500, height=(int)498, sof-marker=(int)0
extended comment: ACOUSTID_ID=53de822a-2dbf-4edd-a7a1-7f756ac5b3b6
album: Hunting High and Low
album artist: a-ha
album artist sortname: a-ha
artist: a-ha
artist sortname: a-ha
comment: Norwegian; Oslo (Songs-DB_Custom3)
datetime: 1985-06-01
disc number: 1
disc count: 1
genre: Pop; New Wave; Synth Pop
language code: eng
album artist ID: 7364dea6-ca9a-48e3-be01-b44ad0d19897
album ID: 4ec07fe8-e7c6-3106-a0aa-fdf92f13f7fc
artist ID: 7364dea6-ca9a-48e3-be01-b44ad0d19897
track ID: da291a7e-3d71-4bcf-b5dd-0727d3deb95a
replaygain album gain: -3.8199999999999998
replaygain album peak: 0.97994994999999996
replaygain reference level: 89
replaygain track gain: -3.8500000000000001
replaygain track peak: 0.97994994999999996
title: Hunting High and Low
track count: 10
track number: 3
audio codec: Free Lossless Audio Codec (FLAC)

kingosticks added a commit to kingosticks/mopidy that referenced this issue Aug 23, 2020
…dy#1827)

Embedded cover art, and other tags exposed to us as type `Gst.Sample`, were
causing memory leaks when converted to plain Python types using
`GstBuffer.extract_dup()`. `extract_dup()` expects the caller to free the
memory it allocates but older versions of the python3-gi don't seem to do
this. The workaround is to access samples using `GstMemory` methods instead.

This issue was present on Buster 10 systems (python-gi 3.30.4) but not on
Ubuntu 20.04 (python-gi 3.36.0).
kingosticks added a commit to kingosticks/mopidy that referenced this issue Aug 23, 2020
…dy#1827)

Embedded cover art, and other tags exposed to us as type `Gst.Sample`, were
causing memory leaks when converted to plain Python types using
`GstBuffer.extract_dup()`. `extract_dup()` expects the caller to free the
memory it allocates but older versions of the python3-gi don't seem to do
this. The workaround is to access samples using `GstMemory` methods instead.

This issue was present on Buster 10 systems (python-gi 3.30.4) but not on
Ubuntu 20.04 (python-gi 3.36.0).

Mopidy memory usage after scanning 2861 tracks:
Without fix: 163.2MB
With fix: 54.1MB
kingosticks added a commit to kingosticks/mopidy that referenced this issue Aug 24, 2020
…dy#1827)

Embedded cover art, and other tags exposed to us as type Gst.Sample, were
causing memory leaks when converted to plain Python types using
GstBuffer.extract_dup(). extract_dup() expects the caller to free the
memory it allocates but older versions of Python 3 PyGObject don't seem
to do this. The workaround is to access buffer data using GstMemory
methods instead.

This issue was found to be present on Buster 10 systems
(python-gi 3.30.4) but not on Ubuntu 20.04 (python-gi 3.36.0).

Mopidy memory usage after scanning 2861 tracks:
Without fix: 163.2MB
With fix: 54.1MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-audio Area: Audio layer C-bug Category: This is a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants