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
Possible memory leak. #103
Comments
Hi, thanks for the report. It's possible you have indeed found a memory leak. I'll spin up a long running test task for a few days to see if I can replicate it. |
I don't know how to dive in anymore than htop sorry, if it helps i have 6 sources. I also made it refresh tasks after restarting and it jumped to 900mb straight away. |
When you index a channel or playlist with youtube-dl it crawls away and then returns a massive object of all the videos and a bit of metadata. For large channels this can be a pretty big object and use up quite a lot of RAM. There's not much that can be done to avoid this, potentially with hooking deep into youtube-dl internals which I would probably be against from a stability and ongoing maintenance perspective. Given the way the indexer works it will spike in RAM usage when indexing, probably to several hundred mb or more when indexing a large channel. However, this should eventually be freed by the GC once the indexing is complete. It would be a bug if the worker continually increased in RAM usage, but probably not a bug if it was stable at a moderately high usage. Can you paste links to your 6 channels if you don't mind them being public, I'll set up an instance the directly replicates your setup and check it. |
Sure, https://www.youtube.com/channel/UCp-mnssZd4X2bgIcekaNfgA My setting are 3hour check, Download Cap @ 7 days, Delete after 10days, 4k if available. Yours is the first docker youtubedl implementation that doesn't fall over straight away so great job!! |
Thanks for the channels, I'll have a go at replicating your situation. If the worker is stable at 1.2gb that's probably "normal" unfortunately, but it may be possible to get it a bit lower and have the GC kick in a bit faster. |
An update, on my system (Unraid docker 16gb ram) each task uses around 250mb of ram and never gets freed. |
Just to clarify, by task do you mean channel to index? There should be only one "task" running at once ever in terms of work tasks. |
OK, thanks. Does it stay stable at that amount of RAM usage? 40+ hours should be enough to have the tasks run a couple of times each. While high, if it's not increasing that's less of a concern initially. |
Yea, after watching it for a while each time a task ran it jumped around 250mb, doesn't increase past that just never releases it i guess. Topped out at 1.8gb with 7 sources. I'm guessing they run fine on your system, after a task runs the ram usage goes back to just nginx + other stuff? |
Might be something, i was looking to see whats downloaded an noticed it hasn't been deleting files. Maybe its getting hung up there? |
The not-deleting-files thing is probably #67 - it shouldn't use any more RAM, it just does a "if this filename exists delete it" check so it won't block anything. As for the tasks I can see they use a set amount of RAM then flatline at that usage even after re-running the indexing tasks again later via the scheduler. This would imply the data structures get re-used or deleted and re-allocated, but persist once the task is complete. There's no clear obvious point this occurs as there's no massive objects passed back from the tasks once they complete or anything, but I'll try stuffing in a bunch of |
Ah right just a coincidence then. After watching it for longer i agree it's not a leak. Memory in use just not freed when it could be. Like you say just not ideal. |
I've seen a huge uptake on memory use for my container on unRAID. I've got 4 channels with currently a total of 811 media items, and TubeSync is using 24.6GB memory. I'm using an external mariaDB database as the sqlite3 database didn't work for me. The memory usage ballooning happened a few TubeSync updates ago; it went from about 300MB to the currently absurd 24.6GB. It is stable at this point, but with the low number of channels and media items, I'm wondering what the cause of this could be. I'm happy to provide logs if required. |
Wow that does indeed sound like a pretty severe leak. Are the 4 channels very large? And does the memory usage return to normal when you restart the container? |
I'm not sure what constitutes a very large channel, one has about 450 videos, another has around 700, one with 16 and one with 27. Nothing extravagant, I feel. Over the Christmas weekend, memory usage has doubled to 49GB. On restart (or complete stop, then start), memory usage gets to about 241-243MB and stays there for a little while, then starts to slowly rise. |
Here's a log snippet from the latest startup just now:
|
Interaction with the WebUI sees memory usage shoot from 241MB to 3.5GB pretty much instantly, and it remains there. I went from the source list to the dashboard to the media list and back to the dashboard. Editing a source rises the memory usage further to 8.7GB. As a side note; editing sources pretty consistently crashes the nginx server resulting in a 502 Bad Gateway error message. Log snippet from the crash:
I'm not at all sure what happened there, but I saw the same behaviour when using the sqlite3 backend, only a lot more frequently and not just when editing sources. |
Editing (saving) a source is currently extremely intensive, it checks every media item linked to the source to see if any state has changed (such as, you changed the date range you want to keep media for and it may need to trigger more downloads for older media). This is being replaced with a watchdog sort of process shortly. Other interactions with the dashboard outside of saving a source shouldn't incur extreme use of memory though as they're just straight Django views with read-only content. |
Yeah, I can understand that, but surely nearly 50GB memory usage seems a bit excessive? I've got 144GB in my server, but still, it'd be nice to have some for other processes too... |
Oh absolutely, 50GB is totally insane and something is very wrong there. I was explaining the memory usage spike for saving a source in the UI. How are you measuring the memory usage? |
I'm just using unRAID's WebUI to look at memory usage: The container was started about an hour ago and I haven't really done anything with it, it's just sitting there doing its thing. I guess it has run some background synchronizing tasks to keep the channels up to date, but I haven't interacted with it. |
Hm, it would be interesting to see what's actually running in the container. I'd be amazed if the worker was actually allocating anything close to that. Can you drop into a shell in the tubesync container? If so, something like this (typed in the container shell) would be helpful: $ apt update
$ apt install procps
$ ps auxwww Just a process list with VSZ / RSS usage of each process in the container. |
Here's the ps output:
It bears to mention that there was an update available last night, which I installed. That update seems to have stabilized the memory usage at reasonable levels again, it now idles at around 450MB memory and shot up to 1.345GB when I added a new channel with 890 videos. It's currently synchronizing the channel and downloading happily and everything looks good so far. I'll keep an eye on it and report back later. |
Thanks for the details. There's nothing too out of the ordinary in that process list, the most intensive task is the worker as expected but that's sitting around 0.93gb of RSS so I assume it's indexing a large channel when you ran If it goes insane again (multiple tens of gigabytes of reported usage) paste the process list in this issue. If there's a mismatch between what the processes are actually using and what Unraid is reporting as being used it may just be the container being allocated RAM as it was being quite active and not released again because you have so much RAM in your server and there's no requirement to free it. I don't use Unraid myself, but general Linux systems allocate most or all of the RAM over time for various caches depending on which statistic is being tracked for the Unraid RAM usage graph (the typical |
I've been stress testing the container today by adding and removing channels, syncing and resyncing, and it never went over 1.8GB memory usage. That kind of amount is completely acceptable. :) With regards to Unraid's memory allocation/deallocation routines, I can't really say. It's based on Slackware (sort of -current, but kind of a snapshot at the same time) with a proprietary web ui on top, so I would think they leverage stock Linux/Docker functions for the actual docker administration. Anyway, I'll keep an eye on the resource use and report back if it starts getting out of control again. |
Good news! Thanks for the feedback. I'll close this issue for now, but if you have any future problems relating to memory usage comment again here and I'll open it back up again. |
Aaand it's back to 26GB memory usage. I also spun up a second instance for audio-only downloads, and that sits around 13GB with two sources and about 1900 items total. ps output for each of the containers: Video-only:
Audio-only:
|
OK, thanks that's helpful. That does seem a lot. Even the gunicorn workers are allocating gigabytes and they're straight normal Django with nothing fancy. Can you try editing the container config in Unraid and limiting its ability to allocate memory and see what happens? I believe you can just edit in additional custom flags and add in |
Done. The change shows up in the unRAID webui: The screenshot was taken less than a minute after restarting the containers, hence the low memory usage. I'll stress test (i.e. use) them for a bit and see what happens, but yesterday it seemed like they behaved normally while being used, but when I left them alone after a bit of use (navigation, enabling disabled items, etc), the memory use rose dramatically. I'll do the same today and then leave them alone for a few hours and see if I can replicate the problem. |
Thanks for testing. Hopefully it'll behave itself when given a (still very generous) sensible limit. |
Quick update: My TubeSync video-only container sits around 480MB memory usage and seems happy, despite being limited to 4GB total. My TubeSync audio-only container, however, hovers around 3.8GB ram while it's apparently doing nothing. This container is also limited to 4GB total RAM, so in any case it can't spin out of control. PS outputs: Video container:
Audio container:
|
Interesting that the audio-only instance worker seems to be the one causing the issues. That might imply an upstream issue, but I'll look into hacking in some force-object-deletion code in to see if I can encourage the GC to clear it up. Is your media downloading and does everything appear to generally be working with the 4G limits in place? |
They seem to be taking turns on chewing on the memory banks :) Previously it was the video container that was hogging the RAM, now it's the audio one. But yeah, they both seem to be working properly. They both download what they're supposed to and they are responsive when navigating. However, the video container consistently times out with a 502 Bad Gateway message when editing (actually saving) a specific source, resetting tasks or trying to delete said source. The audio container did this too when setting it up, as I duplicated the database from the video container and renamed it, and trying to clean that up for audio downloads only was a bit of a pain (ref #196) as everything I tried just timed out. Now that I'm aware of the command line black magic it's no longer a problem as such, and I also want to keep the source as it is. I'm curious to find the reason why it messes up TubeSync so badly, though, but I'm not sure how to approach the problem. |
The 502 nonsense will be fixed when I finally get time to replace the current worker / queue system and offload the work done in background signals to background tasks. There's not a great deal that can be done in the short term to fix it. |
It's been a few days, the containers have been doing their thing over the weekend and got a restart early Sunday morning when unRAID does its appdata (docker container) backup routine. Since then, they seem to have settled around 250MB at idle and use up to 3.6-3.8GB when doing resyncs of the sources, but they also seem to better release memory back to the docker daemon/unRAID to use for other processes. I think it's under control at the moment and as such this issue can be closed again. If new and weird things start happening I'll reopen the issue or create a new one. |
Cheers, thanks for the feedback. Given you've not experienced the containers allocating more RAM than their limits or being otherwise terminated for running out of RAM it's probably safe to assume it's just buffers using up your available RAM because you have loads of it. I'll close the issue again, but comment back if anything weird happens. |
v0.9.1 docker
Container stats off at 230mb.
After running for a couple of days memory usage of docker container has grown to 1.4gb.
/usr/bin/python3 /app/manage.py process_tasks is the process using all the ram.
The text was updated successfully, but these errors were encountered: