How to Find Out the Total Physical Memory (Ram) of My Linux Box Suitable to Be Parsed by a Shell Script

How can I find out the total physical memory (RAM) of my linux box suitable to be parsed by a shell script?

If you're interested in the physical RAM, use the command dmidecode. It gives you a lot more information than just that, but depending on your use case, you might also want to know if the 8G in the system come from 2x4GB sticks or 4x2GB sticks.

how to get physical memory including reserved memory from linux kernel?

physpages = get_num_physpages();
pr_info("Memory: %luK/%luK available (%luK kernel code, %luK rwdata, %luK rodata, %luK init, %luK bss, %luK reserved, %luK cma-reserved"
#ifdef CONFIG_HIGHMEM
", %luK highmem"
#endif
"%s%s)\n",
nr_free_pages() << (PAGE_SHIFT - 10),
physpages << (PAGE_SHIFT - 10),
codesize >> 10, datasize >> 10, rosize >> 10,
(init_data_size + init_code_size) >> 10, bss_size >> 10,
(physpages - totalram_pages() - totalcma_pages) << (PAGE_SHIFT - 10),
totalcma_pages << (PAGE_SHIFT - 10),
#ifdef CONFIG_HIGHMEM
totalhigh_pages() << (PAGE_SHIFT - 10),
#endif
str ? ", " : "", str ? str : "");

So get_num_physpages << (PAGE_SHIFT - 10) will get the whole physical memory.

How can the physical RAM size be determined in Linux programatically?

#include <unistd.h>

long long physical_mem_bytes = (long long) sysconf (_SC_PHYS_PAGES) * sysconf (_SC_PAGESIZE);

Other than the command line ulimit, I don't know of a way of finding maximum memory for an individual process.

Calculating % memory used on Linux

Yes, this is essentially correct. The actual numbers might be (very) marginally lower, but for all intents and purposes, if you have x physical memory and y virtual memory (swap in linux), then you have x + y memory available to the operating system and any programs running underneath the OS.

exact total memory usage in linux that equals to system monitor

GNOME system monitor uses libgtop to retrieve memory information for various platforms. For Linux it uses sysdeps/linux/mem.c2 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.cpp1 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);


Related Topics



Leave a reply



Submit