community
community copied to clipboard
When I manually resize an kivy app, the kivy app uses a lot of unnecessary RAM. Memory will not be re-released correctly
Software Versions
- Python: 3.10.9, 3.11.4, 3.11.6
- OS: Windows 10
- Kivy: 2.2.0, 2.2.1
- Kivy installation method: pip
When I manually resize an kivy app, the kivy app uses a lot of unnecessary RAM. The memory will not be released again. Please help me
Minimal Code Example:
from kivy.app import App
class Main(App):
def build(self):
return
if __name__ == "__main__":
Main().run()
Steps
- Run the provided code.
- Resize the Kivy window multiple times.
- Observe that the memory consumption increases, and the memory is not released after resizing.
https://github.com/kivy/kivy/assets/148625351/4b92c642-2b8b-477e-8953-8ef4564ba953
is that the same code u are running? how is there a window without any widgets? i tested with
if True:
import os
os.environ["SDL_VIDEO_X11_WMCLASS"] = "float-override"
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
class Main(App):
def build(self):
return FloatLayout()
if __name__ == "__main__":
Main().run()
and the memory used does not change at all
I run the code you provided but the problem is the same. As long as the app window is resized, the RAM will not increase. But if you resize the window, the ram usage will increase and the ram will not be released. Watch in this video.
https://github.com/kivy/kivy/assets/148625351/eb2c0472-bd84-437e-8ba4-a0428c147d26
Can you check with another version of python? ( maybe python 3.11? )
Can you check with another version of python? ( maybe python 3.11? )
I tried but same problem
this is super wierd :thinking: , should we do some profiling? Until then, can you install the main branch instead of the wheel?
This is a bug in kivy. if resize the kivy window, the ram usage will increase very much and the ram will not be released
When I try (Win 11, Py 3.11) the memory usage changes, but sometimes decreases after I short while. I suspect we are just looking at Python garbage collection.
The kivy app that I run does not reduce ram usage at all
I see the behavior that other users are reporting, I resize the window, the memory jumps up, after a few seconds, the memory size reduces. If I minimize the window I see the reduction quickly. Win11, Python 3.11.4, Kivy 2.2.1
If there is an issue here, it would most likely be in SDL2 (a kivy dependency) not in kivy directly. You may want to look for bug reports of memory leaks in SDL and see if anything matches your experience.
Are you having a problem or just observing an unusual behavior? I wonder if there is a system configuration that could be slowing the release of memory.
No matter which way the screen is resized, the ram usage increases a lot. The problem is that the ram usage does not go down at all.
Do you have a lot of ram in the computer? Perhaps there is simply no need to release memory now.
Because memory usage could decrease does not mean we can assume that it should decrease. The used memory does decrease when the app closes, right? So you know there is eventually no memory leak, right?
Just because the Python garbage collector does not work the way you expect does not mean there is a Kivy issue. This is simply speculation about how you think the garbage collector should work. If you want to show an error you need to be a lot more convincing than - this is not what I expect.
None of the above means there is not an issue, only that your current demonstration is not convincing to anybody else.
Do you have a lot of ram in the computer?
Doesn't look like the case
Just because the Python garbage collector does not work the way you expect does not mean there is a Kivy issue.
maybe we can do aggressive garbage collection? It sems like a weakref issues
Python: 3.10.9, 3.11.4, 3.11.6 OS: Windows 10 Kivy: 2.2.0, 2.2.1 Kivy installation method: pip
RAM: 4 GB
My problem is that when I resize the window of the kivy app a little, the ram usage rises very high. The biggest problem is that the increased ram usage is not released when the screen is not resized.
Screen not resized: 54.1 MB
After resizing: 430 MB ram usage is not released like 54.1 MB
@mpmp8i can u please run this script and share the output?
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.clock import Clock
import tracemalloc
import linecache
from random import randint
def display_top(snapshot, key_type='lineno', limit=10):
snapshot = snapshot.filter_traces((
tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
tracemalloc.Filter(False, "<unknown>"),
))
top_stats = snapshot.statistics(key_type)
print("Top %s lines" % limit)
for index, stat in enumerate(top_stats[:limit], 1):
frame = stat.traceback[0]
print("#%s: %s:%s: %.1f KiB"
% (index, frame.filename, frame.lineno, stat.size / 1024))
line = linecache.getline(frame.filename, frame.lineno).strip()
if line:
print(' %s' % line)
other = top_stats[limit:]
if other:
size = sum(stat.size for stat in other)
print("%s other: %.1f KiB" % (len(other), size / 1024))
total = sum(stat.size for stat in top_stats)
print("Total allocated size: %.1f KiB" % (total / 1024))
def set_random_windows_size(dt, time=1):
Window.size = (randint(50,600), randint(50,600))
if time < 20:
return Clock.schedule_once(lambda x: set_random_windows_size(x, time+1), 0.5)
App.get_running_app().stop()
class Main(App):
def build(self):
return FloatLayout()
def on_start(self):
Clock.schedule_once(set_random_windows_size)
if __name__ == "__main__":
tracemalloc.start(10)
Main().run()
snapshot = tracemalloc.take_snapshot()
display_top(snapshot)
make sure to open task manager to see if memory usage is increasing, in my testing the memory usage stayed the same
out.py:71: 1.7 KiB
fbind('size_hint', update)
183 other: 77.5 KiB
Total allocated size: 106.9 KiB
When running this, the ram usage does not increase. But if I resize the screen manually, that problem appears.
Interesting 🤔, that does give us a hint of where the issue might be 🤔
Thank you for helping me
@mpmp8i I wonder if this could be a graphics driver issue. Do you have the most up to date driver for your platform?
@mpmp8i I wonder if this could be a graphics driver issue. Do you have the most up to date driver for your platform?
Yes. but same problem.
Is there any new development on this particular issue? I seem to be experiencing the same problem, although the memory usage growth is not as big, it still never gets released. I've executed the "empty-app" test program and it exhibits those issues, and executing the auto-resizing test program does not cause the memory growth.
OS: Ubuntu 24.04 Python 3.12.3 Kivy 2.3.0
In addition to that, I've created an app with button triggering a bunch of labels be updated with text on a bunch of widgets stacked on a scroll-view, and this button-click-triggered update, causing the resize of labels, seems to be causing the same effect, which is increasing memory use, which is never released. This happens even though the window itself does not get resized. I've tried to do some digging on this app with tracemalloc and it points to the following as the growing factor in memory usage (paths deliberately trimmed):
.venv/lib/python3.12/site-packages/kivy/uix/boxlayout.py:311: size=34.9 MiB (+9595 KiB), count=305242 (+81875), average=120 B
.venv/lib/python3.12/site-packages/kivy/uix/boxlayout.py:312: size=11.6 MiB (+3198 KiB), count=101745 (+27291), average=120 B
.venv/lib/python3.12/site-packages/kivy/uix/boxlayout.py:315: size=11.6 MiB (+3198 KiB), count=101757 (+27285), average=120 B
.venv/lib/python3.12/site-packages/kivy/uix/boxlayout.py:314: size=7604 KiB (+2028 KiB), count=64952 (+17307), average=120 B
It does not add any new widgets, It does not remove and re-create anything on the scroll-view, It simply updates a bunch of labels on 6 widgets on a scroll view.
As an additional test, I've modified the code of this app so that any update of labels do not result in any label size change, or any widget size change, instead it would set each label to a random 4-digit number. In that particular case memory usage of the process stays at exact same level.
Tested with "empty-app" on:
OS: Ubuntu 22.04 Python 3.10.12 Kivy 2.3.0
and the problem seems to not be occurring there
Did some further testing on Ubuntu 24.04. I've used Deadsnakes PPA to install older versions of python and test it with latest version of kivy. Using python 3.10.14 and python 3.11.9, in both cases with kivy 2.3.0 installed via pip, the problem does not occur.
I've also used a deb packaged version of kivy version 2.2.1 and installed it via apt for python 3.12 and this problem does not occur with this combination either.
I've then built kivy 2.3.0 from source for python 3.12, but with this combination the problem is occurring.