jaredks / rumps

Ridiculously Uncomplicated macOS Python Statusbar apps
BSD 3-Clause "New" or "Revised" License
3.09k stars 179 forks source link

How to properly update the menu without leaking memory? #216

Open israelsucks opened 1 month ago

israelsucks commented 1 month ago

Hello,

I've spotted my app which updates its menu regularly reaches multiple GB's of memory over time.

Here is an example code similar to my code's logic, which uses exaggerated values for quick demonstration purposes:

#!/usr/bin/env python3

import rumps
import time

testinterval=0.1

class AwesomeStatusBarApp(rumps.App):

    def __init__(self):
        super(AwesomeStatusBarApp, self).__init__("myapp")
        self.menu = [f"hello"]

    @rumps.timer(testinterval)
    def sayhi(self, _):
        mylist = []
        for i in range(1000):
            mylist += ['time', str(time.time())*10, None]

        # reset menu
        self.menu.clear()

        self.menu = mylist # or self.menu.update(mylist), doesn't matter

if __name__ == "__main__":
    AwesomeStatusBarApp().run()

If you look at Activity Monitor, you'll see its memory grows indefinitely. I've also tried iterating over menu keys() and deleting them but didn't fix the leak.

Am I doing something wrong or is it the rumps which leaks memory under the hood?

SKaplanOfficial commented 1 month ago

Not sure exactly where the leak occurs (whether in rumps or in the underlying pyobjc bridge), but to get around it you can modify menu items in-place rather than remake the whole menu each time, like so:

import rumps
import time

testinterval=0.1

class AwesomeStatusBarApp(rumps.App):

    def __init__(self):
        super(AwesomeStatusBarApp, self).__init__("myapp")
        self.menu = [x for x in range(1000)]

    @rumps.timer(testinterval)
    def sayhi(self, _):
        for (_, item) in self.menu.items():
          item.title = f"time {time.time()}"

if __name__ == "__main__":
    AwesomeStatusBarApp().run()

This hovers around ~35.0 MB on my machine.