I created a simple application that only creates and displays empty 50x50 window, but it already consumes 20MB of memory. I am targeting low-memory devices, so each megabyte really counts. What causes GTK to consume all that memory? Is it possible to reduce memory usage?
Here's the complete source code for the program:
#include <gtk/gtk.h>
int main(int argc, char* argv[]) {
gtk_init(&argc, &argv);
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DOCK);
gtk_window_set_default_size(GTK_WINDOW(window), 50, 50);
gtk_window_move(GTK_WINDOW(window), 50, 50);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Here's what I compile it with:
gcc -std=gnu99 -Wall -o example main.c $(pkg-config --cflags --libs gtk+-3.0)
And here's the resulting memory usage:
$ ps -FC example
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
platon 4214 11052 7 84812 20996 1 16:13 pts/5 00:00:00 ./example
(ps measures memory usage in KB, so that's 20996KB or ~21MB)
I'm using gtk3 version 3.22.16, on linux 4.11.6, x86_64.
Problem context: target system is relatively low-memory PC (200-400 MB of memory). Application is kiosk-like interface on that PC, with relatively complex GUI structure (many pages and possible interactions). And I would have preferred to avoid re-implementing all the GUI logic manually (on top of lower-level libraries), so I was looking for something higher-level - and it seems there is only GTK and Qt in that space (Qt is usable only from C++, which is a pain).
Before claiming that your process eats much RAM you should answer the following questions:
Also, since you target a system with less than 1 gig of ram, investigate the possibility to run in 32 bit mode and the memory usage in that environment. This will reduce the size of each pointer with a factor of 2. This mainly affects applications that are pointer-heavy.
I ran your program (optimized with -O1) in the background, than used pmap(1) to observe its (virtual) memory map. It consumes 335752K (i.e. 335Mbytes) in 349 memory segments, on Debian/Sid/x86-64. 77 different shared libraries are loaded. BTW, as soon as you use texts and fonts, they also go into the virtual address space. And ps -FC gives about 84Mb for SZ and 21Mb for RSS (try also, as commented, with pmap -x, to get RSS details).
You should use pmap on your own (target) system and get your own conclusions about what is consuming memory. If pmap is not available and the process has pid 1234, see /proc/1234/maps (giving the same information as pmap and parsed by pmap), read proc(5)
BTW I am not surprised by such a high consumption. Think a bit more about all the implied resources used by GUIs (and you'll guess that by studying the output of pmap, or using strace(1) ...). See also this answer and follow the links I gave there. My opinion is that you need in practice a gigabyte (or at least half of it) of RAM (like on most RaspberryPIs) to run modern GUI toolkits like Qt or GTK. Notice that today a gigabyte of RAM is really cheap, so your company may need to sell millions of devices to pay up the years of additional development work needed to fit into only a few hundred megabytes.
(Perhaps you'll better use lower level things like libsdl, or libX11 but see this)
so each megabyte really counts
Then you don't want a full-fledged GUI toolkit (or you should afford a more powerful hardware with a gigabyte of RAM). You could target just a raw Wayland (or X11) display server (at the expense of many years of development efforts, or by accepting a much less sophisticated GUI). BTW QT (and perhaps also GTK) have specialized embedded -or framebuffer- variants (which could be slightly less resource consuming).
PS. I guess your OS is some Linux variant; I recommend reading some textbook like Operating Systems : Three Easy Pieces (freely and legally downloadable) to get a better picture about how an OS works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With