Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this simple GTK3 application consumes so much memory?

Tags:

c

gtk

gtk3

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).

like image 856
Rogach Avatar asked Jan 20 '26 02:01

Rogach


2 Answers

Before claiming that your process eats much RAM you should answer the following questions:

  1. How much would the amount of RAM increase when adding more widgets? It is likely that the memory cost is something like 20 MB + 0.1 MB*k, where k is the number of widgets.
  2. How many processes do you have besides your GUI. If the cost per widget is less than 0.1 MB (I believe it is as long as you do not load huge images), you can have 1000 widgets without any problems, as long as you do not run anything else that takes a lot of RAM.

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.

like image 192
user877329 Avatar answered Jan 22 '26 17:01

user877329


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.

like image 21
Basile Starynkevitch Avatar answered Jan 22 '26 19:01

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!