Widget memory leak when removed
See original GitHub issueAfter removing widgets, memory does not appear to be reclaimed (tested on iOS)
from kivy.app import App
from kivy.lang import Builder
class TestApp(App):
def on_pause(self, *l):
return True
def build(self):
return Builder.load_string('''
#:import Button kivy.uix.button.Button
BoxLayout:
orientation: 'vertical'
FloatLayout:
id: container
BoxLayout:
size_hint_y: None
height: 100
orientation: 'horizontal'
Button:
text: 'add'
on_press: container.add_widget(Button())
Button:
text: 'remove'
on_press: container.clear_widgets()
''')
TestApp().run()
Click add several times (memory consumption will increase). Click remove and wait, no memory seems to be reclaimed.
Issue Analytics
- State:
- Created 9 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Memory leak when displaying and removing multiple image ...
I think the images should be released when the widgets are cleared, but they don't appear to be doing that. Memory usage seems...
Read more >memory seems to keep deleted widgets - Google Groups
Hi, I've got a problem with deleted widgets. It seemes, that they are kept in the memory. I've got a GridLayout in a...
Read more >Removing tkinter frame without memory leak - Stack Overflow
The best practice is to not worry about it. The counter takes up only a few bytes per widget. You can avoid the...
Read more >Does removing the widget from a parent means it is destroyed ...
Yes, providing it is not being reference by anything else. You can store the reference to a widget in a variable of its...
Read more >Comments/Forum - Qt Plotting Widget QCustomPlot
QCustomPlot is a Qt C++ widget for plotting. ... The system memory stat shows however this causes massive memory leak somewhere.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Because of the way python GC works just because you have removed the Widget does not mean it is time for it to be garbage collected. The default thresholds for Garbage Collection are (700, 10, 10), this means a gc.collect() will only be run once the number of allocations - number of deallocations (throughout the whole codebase) is greater than 700. In addition once an object survives one round of garbage collection it will be moved into generation 2, in which case it will take 10 separate triggerings of the generation 1 garbage collection to trigger the first collection of this object. Meaning that you must move through 700*10 allocations and deallocations at a bare minimum (although in practice it will probably be much larger as many allocations will not get deallocated) to trigger this collect. Likewise if the object survives its first collection in this round it won’t be up for collection again until 10 triggerings of the generation 2 gc and thus 100 triggerings of the 700 alloc difference. This type of examples run into a problem I think because they seem big enough, but they fall in-between collection cycles resulting in a large amount of garbage. For such a use case, matham’s suggestion of manually calling gc.collect() is probably good to keep in mind.
I believe we had a discord discussion about this?
The way to test if memory is not reclaimed is creating a weakref to the widget and seeing if it’s not released when the widgets are cleared. Anything beyond that is related to how the system/python handles memory and is not something kivy can control.