I'm currently struggling with a memory usage problem in Kivy.
When a popup is created and opened, memory usage goes up a little (which is normal), but when the user closes it (with the dismiss() method, which closes the popup and removes it from its parent), the memory isn't released.
So, if the user decides to open the popup a lot of times, the program will eventually use a lot of memory. Here is a part of my code that shows the problem by creating, opening and then dismissing a popup 500 times.
# py file
class OptionsView(Screen):
def popupLoop(self):
for x in range(0, 500):
popup = self.MyPopup()
popup.open()
popup.dismiss()
class MyPopup(Popup):
pass
# kv file
<OptionsView>:
BoxLayout:
orientation: "vertical"
Button:
text: "Popup Loop"
on_press: root.popupLoop()
<MyPopup>:
size_hint: (.6, .6)
title: "Confirmation"
BoxLayout:
Button:
text: "Cancel"
on_press: root.dismiss()
Pressing the "Popup Loop" button in the OptionView screen causes the program to jump from 1.2% memory usage to 11.7% (according to top
). Resizing the window (which calls gc.collect()
) does bring this number down a little bit, but it remains really high.
How can I prevent this from happening? (Bear in mind that I'm far from being a Python/Kivy expert, so even if the solution is really obvious to you, please try to explain it to me!)