简体   繁体   中英

exact total memory usage in linux that equals to system monitor

By getting Memtotal and Memfree values from "/proc/meminfo" and subtracting them we must get the Used Memory in Linux. Most of threads and web pages that I have visited have guided through this approach to compute the Total Memory Usage in Linux. But by implementing this method, I get different results with GNOME System Monitor! My result is greater than that (approximately double). So what is the method that GNOME System Monitor uses?

GNOME system monitor uses libgtop to retrieve memory information for various platforms. For Linux it uses sysdeps/linux/mem.c 2 where the routine is as follows:

Strings like "MemTotal" are headings in /proc/meminfo .

…    
    buf->total  = get_scaled(buffer, "MemTotal:");
    buf->free   = get_scaled(buffer, "MemFree:");
    buf->used   = buf->total - buf->free;
    buf->shared = 0;
    buf->buffer = get_scaled(buffer, "Buffers:");
    buf->cached = get_scaled(buffer, "Cached:");

    buf->user = buf->total - buf->free - buf->cached - buf->buffer;

The memory reported in the application is buf->user . More precisely in src/load-graph.cpp 1 by:

mempercent  = (float)mem.user  / (float)mem.total;
set_memory_label_and_picker(GTK_LABEL(graph->labels.memory),
                            GSM_COLOR_BUTTON(graph->mem_color_picker),
                            mem.user, mem.total, mempercent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM