Kodi addon classes left in memory again

  • This was an issue a couple of years ago, got fixed and seems to have crept back beginning with the June 6th nightly build. The issue is where with LibreElec (generic version on Intel), not with Kodi on Windows, that many addons, including the LibreElec configuration addon are spewing classes left in memory errors. This is likely due to a problem with the Python garbage collection process. A thread was started in the Kodi forum. I just wanted to raise it here, in case the developers were unaware. I had posted some logs there but this is very easy to reproduce.


    Thanks,

    Jeff

  • This was an issue a couple of years ago, got fixed and seems to have crept back beginning with the June 6th nightly build. The issue is where with LibreElec (generic version on Intel), not with Kodi on Windows, that many addons, including the LibreElec configuration addon are spewing classes left in memory errors. This is likely due to a problem with the Python garbage collection process. A thread was started in the Kodi forum. I just wanted to raise it here, in case the developers were unaware. I had posted some logs there but this is very easy to reproduce.


    Thanks,

    Jeff

    When a Kodi addon stops or restarts, Python must properly clean up all used Kodi API classes. If an addon developer forgets to close or disconnect specific objects, these objects remain permanently in the devices' RAM. Kodi then displays this exact "has left several classes in memory" warning. These are pure Python memory leaks within the Kodi application itself, having nothing to do with core LibreELEC or Kodi. These warnings are almost always caused by two specific programming errors:

    1. Unclosed Monitors/Players: The developer uses xbmc.Monitor or xbmc.Player but forgets to delete it when the addon stops.
    2. Circular References: A global reference to the xbmcaddon.Addon() object persists in a background thread that has not been closed correctly.
  • When a Kodi addon stops or restarts, Python must properly clean up all used Kodi API classes. If an addon developer forgets to close or disconnect specific objects, these objects remain permanently in the devices' RAM. Kodi then displays this exact "has left several classes in memory" warning. These are pure Python memory leaks within the Kodi application itself, having nothing to do with core LibreELEC or Kodi. These warnings are almost always caused by two specific programming errors:

    1. Unclosed Monitors/Players: The developer uses xbmc.Monitor or xbmc.Player but forgets to delete it when the addon stops.
    2. Circular References: A global reference to the xbmcaddon.Addon() object persists in a background thread that has not been closed correctly.


    It looks like there is a proposed Kodi PR for the fix. I presume it will just be a matter of time. Ironically the LibreElec addon exhibits the same behavior. The issue is pervasive across many addons.


    Thanks,

    Jeff

  • The proposed Kodi pull request (PR) will not fix the underlying issue or change the standard for good python coding; it merely changes how Kodi handles and reports the developer's errors. The PR modifies CPythonInvoker to forcefully run a garbage collection cycle and clear module dictionaries before tearing down the Python sub-interpreter. While this prevents Kodi from crashing (segmentation fault) or spamming logs during a profile switch, it is an application-level safety net. It does not absolve the addon developer from writing clean code.

    The PR cleans up the leaked memory only when the execution is completely finished or when switching profiles. If an addon leaks xbmc.Monitor or xbmc.Player objects during its normal runtime execution (e.g., repeatedly restarting a background service or script without destroying old instances), those objects will still consume RAM and leak resources until the entire interpreter environment is destroyed.

    In short, the PR fixes Kodi's stability when handling badly written addons, but good conduct Python coding still dictates that developers must explicitly close monitors, stop players, and avoid circular references in threads to prevent active runtime memory leaks.

    Salute.