How to calculate system memory usage from /proc/meminfo (like htop)
htop author here. These are the calculations I make to get the numbers for the green, blue and yellow bars in the memory meter:
- Total used memory =
MemTotal
-MemFree
- Non cache/buffer memory (green) = Total used memory - (Buffers + Cached memory)
- Buffers (blue) =
Buffers
- Cached memory (yellow) =
Cached
+SReclaimable
-Shmem
- Swap =
SwapTotal
-SwapFree
In the htop source code: linux/LinuxProcessList.c and linux/Platform.c.
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);
Why do 'htop' and 'top' show different memory usage?
Top counts the cache aswell.
So if you do:
X = Used
Y = Buffers
Z = Cached
X - Y - Z = HTOPMEM
Take care my Linux Memory Usage Summary in console
As an alternative look at the contents of: /proc/meminfo
For example:
grep MemAvailable /proc/meminfo
and:
cat /proc/meminfo
Notice that MemAvailable is only available in modern Linux kernels (not RHEL/CentOS 6 unless you run it with a newer kernel, Like Oracle Unbreakable Linux does)
For fun and education look also at: https://www.linuxatemyram.com/
For a more convenient info on your systems resource usage you may be interested in something like atop: https://haydenjames.io/use-atop-linux-server-performance-analysis/ or one of the other top tools like these: https://haydenjames.io/alternatives-top-htop/
I'm just no big fan of free so I avoid it like the plague ;-)
How is the Linux calculating MemFree
MemFree
in /proc/meminfo
is a count of how many pages are free in the buddy allocator. This buddy allocator is the fundamental unit of physical memory allocation in the kernel; however there are a lot of ways pages can be returned to the buddy allocator in time of need - for example, freeing empty SLABs, discarding cache/buffer RAM (even if this means invalidating PTEs in a running process), or as a last resort, swapping things out.
In fact, MemFree
is generally controlled to be only 5-10% of total physical RAM, with any extra free RAM being co-opted into cache as time goes on. As such, MemFree
alone is a very incomplete view of the overall memory situation.
As for the virtual memory (VSIZE) of a given process, this refers to the sum total of the sizes of all mapped memory segments in the process's address space. However, not all of these will be physically present - some may be paged in upon first access and as such will not register as memory in use until actually used. The resident size (RSIZE) is a more accurate view, as it only registers pages that are mapped in right now - although this may also not be accurate if a given page is mapped in multiple virtual addresses (which is very common when you consider multiple processes - shared libraries have the same physical RAM mapped to all processes that are using that library)
How to get current CPU and RAM usage in Python?
The psutil library gives you information about CPU, RAM, etc., on a variety of platforms:
psutil is a module providing an interface for retrieving information on running processes and system utilization (CPU, memory) in a portable way by using Python, implementing many functionalities offered by tools like ps, top and Windows task manager.
It currently supports Linux, Windows, OSX, Sun Solaris, FreeBSD, OpenBSD and NetBSD, both 32-bit and 64-bit architectures, with Python versions from 2.6 to 3.5 (users of Python 2.4 and 2.5 may use 2.1.3 version).
Some examples:
#!/usr/bin/env python
import psutil
# gives a single float value
psutil.cpu_percent()
# gives an object with many fields
psutil.virtual_memory()
# you can convert that object to a dictionary
dict(psutil.virtual_memory()._asdict())
# you can have the percentage of used RAM
psutil.virtual_memory().percent
79.2
# you can calculate percentage of available memory
psutil.virtual_memory().available * 100 / psutil.virtual_memory().total
20.8
Here's other documentation that provides more concepts and interest concepts:
- https://psutil.readthedocs.io/en/latest/
Related Topics
Python Module Import Error for One User But Not the Other
How to Write Multiple Line String Using Bash with Variables
Calculate Size of Files in Shell
Reading Data from PDF Files into R
How to Convert an Ssl Certificate in Linux
How to Diff a Directory for Only Files of a Specific Type
How to Skip Saturday and Sunday in a Cron Expression
What Is .D File After Building with Make
Scp Command Syntax for Copying a Folder from Local MAChine to a Remote Server
In a Linux Shell How to Process Each Line of a Multiline String
Grabbing Specific Sections of Text from a String
Force Unmount of Nfs-Mounted Directory
Implementing an Update/Upgrade System for Embedded Linux Devices
Installing Node.Js on Debian 6.0
How to Make Vim Play Typewriter Sound When I Write a Letter
Difference Between Checkout and Export in Svn