Richard Larkin is one DotModus' lead Python Developers and a regular contributor to Kivy. Kivy is an open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.
Here is a problem Richard and his team faced recently:
We recently encountered some rather nasty memory issues when testing our latest Kivy touchscreen deployment. Luckily, good tools came to the rescue!
The problem occurred when opening our customised file chooser dialog, which enhances the standard Kivy FileChooser. The enhancements include thumbnail image previews and EXIF (you know, the stuff that knows whether or not you took your photo sideways) image rotation support, courtesy of PIL (Python Image Library)
What to do? The first thing was to verify the problem, because we all know assumption is the mother of all... bad things. Enter the Kivy webdebugger. Two lines of code:
from kivy.config import Config Config.set(‘modules’, ‘webdebugger’, ‘’)
And we have live system stats served via an embedded flask server on http://127.0.0.1:5000/.
Yup, memory was doing a SpaceX on us, with object counts skyrocketing and the device responding with a slow and painful freeze. What to do?
The solution was two fold. The first step involved combing through our custom FileChooser code to ensure no object references prevented garbage collection. i.e. if you do:
self.obect = object
ensure you do:
when unloading. The second step involved manually invoking garbage collection. Although this is something you generally want to avoid and normally never really need to do in Python, it this case, the thumbnail generation just used too much memory to0 quickly.
import gc gc.collect()
Voila! Problem solved!
The Take Home
1. Having good tools to debug issues is invaluable.
2. Python is not invulnerable to memory issues.
3. Remember to cleanup after yourself.
Now, if only I could get my son to implement no. 3...