Calculating memory of a Process using Proc file system
You likely want resident
or size
. From kernel.org.
- size total program size
- This is the whole program, including stuff never swapped in
- resident resident set size
- Stuff in RAM at the current moment (this does not include pages swapped out)
Calculating used memory by a set of processes on Linux
You will want to iterate through each processes /proc/[pid]/smaps
It will contain an entry for each VM mapping of the likes:
7ffffffe7000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
Size: 100 kB
Rss: 20 kB
Pss: 20 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 20 kB
Referenced: 20 kB
Anonymous: 20 kB
AnonHugePages: 0 kB
Swap: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Private_Dirty
memory is what you are interested in.
If you have the Pss
field in your smaps
file then this is the amount of resident memory divided by the amount of processes that share the physical memory.
Private_Clean
could be copy-on-write mappings. Those are commonly used for shared libraries and are generally read/no-write/execute.
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.
Is there a way to calculate I/O and memory of current process in C?
You could read and parse the files in the /proc
filesystem. /proc/self
refers to the process accessing the /proc
filesystem.
/proc/self/statm
contains information about memory usage, measured in pages. Sample output:
% cat /proc/self/statm
1115 82 63 12 0 79 0
Fields are size resident share text lib data dt
; see the proc
manual page for some additional details.
/proc/self/io
contains the I/O for the current process. Sample output:
% cat /proc/self/io
rchar: 2012
wchar: 0
syscr: 6
syscw: 0
read_bytes: 0
write_bytes: 0
cancelled_write_bytes: 0
Unfortunately, io
isn't documented in the proc
manual page (at least on my Debian system). I had too check the iotop
source code to see how it obtained the per process I/O information.
How can I measure the actual memory usage of an application or process?
With ps
or similar tools you will only get the amount of memory pages allocated by that process. This number is correct, but:
does not reflect the actual amount of memory used by the application, only the amount of memory reserved for it
can be misleading if pages are shared, for example by several threads or by using dynamically linked libraries
If you really want to know what amount of memory your application actually uses, you need to run it within a profiler. For example, Valgrind can give you insights about the amount of memory used, and, more importantly, about possible memory leaks in your program. The heap profiler tool of Valgrind is called 'massif':
Massif is a heap profiler. It performs detailed heap profiling by taking regular snapshots of a program's heap. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. The graph is supplemented by a text or HTML file that includes more information for determining where the most memory is being allocated. Massif runs programs about 20x slower than normal.
As explained in the Valgrind documentation, you need to run the program through Valgrind:
valgrind --tool=massif <executable> <arguments>
Massif writes a dump of memory usage snapshots (e.g. massif.out.12345
). These provide, (1) a timeline of memory usage, (2) for each snapshot, a record of where in your program memory was allocated. A great graphical tool for analyzing these files is massif-visualizer. But I found ms_print
, a simple text-based tool shipped with Valgrind, to be of great help already.
To find memory leaks, use the (default) memcheck
tool of valgrind.
How do I get the total CPU usage of an application from /proc/pid/stat?
Preparation
To calculate CPU usage for a specific process you'll need the following:
/proc/uptime
#1
uptime of the system (seconds)
/proc/[PID]/stat
#14
utime
- CPU time spent in user code, measured in clock ticks#15
stime
- CPU time spent in kernel code, measured in clock ticks#16
cutime
- Waited-for children's CPU time spent in user code (in clock ticks)#17
cstime
- Waited-for children's CPU time spent in kernel code (in clock ticks)#22
starttime
- Time when the process started, measured in clock ticks
- Hertz (number of clock ticks per second) of your system.
- In most cases,
getconf CLK_TCK
can be used to return the number of clock ticks. - The
sysconf(_SC_CLK_TCK)
C function call may also be used to return the hertz value.
- In most cases,
Calculation
First we determine the total time spent for the process:
total_time = utime + stime
We also have to decide whether we want to include the time from children processes. If we do, then we add those values to total_time
:
total_time = total_time + cutime + cstime
Next we get the total elapsed time in seconds since the process started:
seconds = uptime - (starttime / Hertz)
Finally we calculate the CPU usage percentage:
cpu_usage = 100 * ((total_time / Hertz) / seconds)
See also
Top and ps not showing the same cpu result
How to get total cpu usage in Linux (c++)
Calculating CPU usage of a process in Linux
Location of proc filesystem
The Linux /proc File System is a virtual filesystem that exists in RAM (i.e., it is not stored on the hard drive). That means that it exists only when the computer is turned on and running.
Here are some resources where you can find more details about this:
The /proc filesystem
Exploring /proc File System in Linux
Discover the possibilities of the /proc directory
Hope that helps.
Related Topics
When Running Ls -L, Why Does the Filesize on a Directory Not Match the Output of Du
Why Ln -Sf Does Not Overwrite Existing Link to Directory
How to Not Emit Local Symbols in Nasm So That Gdb Disas Won't Stop at Them
Linux Equivalent of Windows Dll Forwarders or MACos Reexport_Library
Script to Check for Process & Restart Program If Not Found
Perf Tool Stat Output: Multiplex and Scaling of "Cycles"
Linking to Modules Folder Gives Undefined Reference
Grep for String and Open at the Corresponding Line
How to Make .Gitignore Configurable Based on Environment Variables
How Provide Nested Mount of Overlayfs
How to Extract Patterns Form a Text Files in Shell Bash
File Glob Patterns in Linux Terminal
Rust Linux Version Glibc Not Found - Compile for Different Glibc/Libc6 Version
Shell Script to Check If Process Is Running