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.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);
Related Topics
Download Images from Google with Command Line
How to Control a User Systemd Using 'Systemctl --User' After Sudo Su - Myuser
How to Use Expect with Optional Prompts
Hook Functions for Linux Filesystem
Why Does Docker Container Prompt "Permission Denied"
Recursively List All Files in a Directory Including Files in Symlink Directories
Replace Whitespaces with Tabs in Linux
Managing a User Password for Linux in Puppet
How Can the Linux Kernel Be Forced to Enumerate the Pci-E Bus
Adding a Directory to Path in Ubuntu
Help with Understanding a Very Basic Main() Disassembly in Gdb
How to Correctly Install Wkhtmltopdf on Debian 64 Bit
How to Fix Permission Denied for .Git/ Directory When Performing Git Push
Is Timer Interrupt Independent of Whether System Is in Kernel Mode or User Mode
Understanding Bash Short-Circuiting
Ssh: Could Not Resolve Hostname [Hostname]: Nodename Nor Servname Provided, or Not Known
What Are Stalled-Cycles-Frontend and Stalled-Cycles-Backend in 'Perf Stat' Result