question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Widget memory leak when removed

See original GitHub issue

After 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:closed
  • Created 9 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Kovakcommented, Jun 10, 2014

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.

0reactions
mathamcommented, Mar 14, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found