Tuesday, March 25, 2008

Finding bloat in KDE4

KDE 4.x is a terrific piece of code. Provided you speak C++, the code is easy to understand and clean.

However, as good as the code is, there are issues that cause kde 4 to eats up more memory that kde3. This blog is dedicated to finding some low hanging fruits in memory consumption.

My machine is an AMD Athlon(tm) 64 Processor 3400+, with 1GB ram. It is running Kubuntu 7.10, and I compiled kde from svn revision 795377 with debug enabled.

To start with, I ran kde4 and profiled system memory with exmap. Here is the result:

As you can see the memory consumption on my machine is about 169MB.

The single process that consumes more memory here is knotify4. Knotify is a kde daemon that sits there waiting for events to happen. Once an event happens, it receives a message via dbus, and may notify the user via a sound, a popup window or other mecanisms. It is the process that is responsible for playing a sound when you maximize, minimize windows, log in, log out, etc.

For such a simple task, it seems strange that it needs 30MB of memory, so I dug deeper.

Going to the details of knotify4:

Between the heap and anon pages (I am not sure what the difference is) it consumes about 25Mb.

So I ran knotify4 under a heap profiler, namely valgrind --tool=massif and the results are pretty self explanatory:

Playing sounds with xine seems to be eating up all the memory. I tested other xine based players, and I got the same results. It seems to me that xine library just sucks up a ton of memory for playing vorbis files.

I tried playing the same vorbis files with mplayer, and the result is very different, it uses 1MB for heap + anon.

To verify audio is the problem, I disabled sound notifications in the System Settings -> Notifications. This is what the memory consumption looks like with no audio notifications:

Now knotify4 uses 2MB effective memory. It is no longer a blip in the radar. And kde as a whole now uses about 22MB less memory.

It is worth noticing that phonon was designed so that back end can be replaced. Switching to a more efficient back end than xine can make a significant improvement in kde4 memory consumption, kudos to the phonon team to make such transition easy. Of course, the other way to fix the issue is fixing xine library itself. Being written in C and looking at the code, it is clear that is something I do not want to debug.

I will be posting more articules pinpointing low hanging fruits in kde memory consumption, so I will keep the score in a table as I find stuff:

ChangePotential memory savings (aprox)
Change phonon backend from xine to something else22 MB


Leo S said...

Xine allocates a bunch of memory for video playback, even if it doesnt do any video playback. This should be fixed in an upcoming release. For now, if you use gstreamer backend for phonon you don't see this problem.

Ivo Anjo said...

Excellent work! Please keep it up.

Anonymous said...

Phonon can be used with VLC as backend already, see
Additionally, with Qt 4.4 GStreamer will be supported as another backend afaik. Maybe you want to test the memory consumption of these?

Ajay said...

KDE 4.1 still seems to be a memory hog.Plasma is showing 124m, konqueror is showing 170m and knotify is showing 100m virtual mem usage in top.

I run virtualbox guest that consumes about 600M, but runs fine in non-KDE4 environs. With bare KDE 4.1, no kopete, no Juk etc, the machine began to crawl and jerk.

Memory is cheap these days, but still.. :-)