quickmic 1749 Posted January 2, 2025 Posted January 2, 2025 (edited) I was chasing a memory leak on Kodi for almost 3 days and wanna share my results: As I started Kodi tests with my Raspberry Pi 5 (8GB) ram I was wondering why this device uses all the memory. I tracked it down to skin/widget refreshes means, by trigger a library update. More and more ram was consumed and never released. Also each playback start/stop triggers a skin refresh and memory usage was increasing even more till a point Kodi crashed. First thought was a bug in Kodi/images, so I tried libreelec and Xbian which are the only available images for RPI 5, but no difference. I uninstalled every plugin, every dependency, even build in addons and stripped down Kodi to bare minimum, still no difference. As I never experienced this issue on my other boxes (vero 5 and osmc), I started thinking what could be the difference. First clue was, osmc is an 32bit OS, so I tired libreelec in 32bit, but turns out this 32bit images still seems to uses the 64 bit kernel, but 32bit Kodi application. Digging deeper and deeper, I started reviewing the glibc parameters. Turns out, the MALLOC_ARENA_MAX seems the key to success (at least for now). https://www.gnu.org/software/libc/manual/html_node/Memory-Allocation-Tunables.html On 32bit OS the MALLOC_ARENA_MAX is by default 2 times the cores = 8 on a 4 core system. On 64bit OS it's 8 times cores = 32 As I started lower MALLOC_ARENA_MAX=1 via env parameters (as a test) I immediately realized the difference. Ram usage was down to a bare minimum and never grows again. However, MALLOC_ARENA_MAX=1 is not optimal for performance, currently I'm testing MALLOC_ARENA_MAX=16 on my RPI 5 with libreelec and seems to work quite good. Highest mem usage is currently 4.7GB. Anyway, if you experience same memory leaks, give it a try: In libreelec you can adjust the parameter here: /storage/.config/kodi.conf and add this line MALLOC_ARENA_MAX=16 also crashed MALLOC_ARENA_MAX=8 same as on 32bit os seems to work Edited January 20, 2025 by quickmic 2
quickmic 1749 Posted January 3, 2025 Author Posted January 3, 2025 Xbian: /etc/init/xbmc.conf replace "env MALLOC_MMAP_THRESHOLD_=65536" with "env MALLOC_MMAP_THRESHOLD_=8192" and add "env MALLOC_ARENA_MAX=8"
TeamB 2444 Posted January 19, 2025 Posted January 19, 2025 for reference https://thehftguy.com/tag/malloc_arena_max/ https://sourceware.org/glibc/wiki/Development_Todo/Enhancing_malloc 2
hoodafukisalice 1 Posted 1 hour ago Posted 1 hour ago @quickmic, many thanks for sharing this. I'm running LibreELEC on a MeLe mini PC (Intel Celeron N5105 with 4GB RAM) where it was frequently crashing on scraping content. I was checking Kodi's log, and then dmesg to frequently find out of memory kills of kodi.bin. Everywhere I searched, on Kodi forums, "experts" were suggesting primarily upgrading to a higher end media box with more memory! I stumbled on your post today, tried your fix. While it doesn't completely fix the problem with running out of memory, MALLOC_ARENA_MAX=8 certainly helped the scraping get far ahead from where and how frequently it used to crash earlier. So fewer crashes now. A simple modification like this just proves bugs like these are what the Kodi and/or *ELEC teams should be focusing on priority, instead of piling more "features"! Would you know if it's modifying /storage/.config/kodi.conf on CoreELEC as well? If not, could you suggest the right file? I created an account here and wanted to post this as a testimony that your research was fruitful. So, thanks again, and may your post help others who are suffering as well. Cheers. 1
quickmic 1749 Posted 1 hour ago Author Posted 1 hour ago Well, I assume it's the same for coreelec, but not tested. btw, you can even lower the value to MALLOC_ARENA_MAX=2. Actually, that's what I'm currently using. 1
hoodafukisalice 1 Posted 49 minutes ago Posted 49 minutes ago Thanks for the snappy response. I was checking because I have a Khadas VIM3L running CoreELEC, that's suffering from the same symptoms. I'm currently trying MALLOC_ARENA_MAX=4. Will try 2, if the crashes persist. Cheers.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now