How to find the memory consumption of a particular process in linux for every 5 seconds
you may use SNMP to get the memory and cpu usage of a process in a particular device in network :)
Requirements:
- the device running the process should have snmp installed and running
- snmp should be configured to accept requests from where you will run the script below(it may be configured in snmpd.conf)
- you should know the process id(pid) of the process you want to monitor
Notes:
HOST-RESOURCES-MIB::hrSWRunPerfCPU is the number of centi-seconds of the total system's CPU resources consumed by this process. Note that on a multi-processor system, this value may increment by more than one centi-second in one centi-second of real (wall clock) time.
HOST-RESOURCES-MIB::hrSWRunPerfMem is the total amount of real system memory allocated to this process.
**
Process monitoring script:
**
echo "IP: "
read ip
echo "specfiy pid: "
read pid
echo "interval in seconds:"
read interval
while [ 1 ]
do
date
snmpget -v2c -c public $ip HOST-RESOURCES-MIB::hrSWRunPerfCPU.$pid
snmpget -v2c -c public $ip HOST-RESOURCES-MIB::hrSWRunPerfMem.$pid
sleep $interval;
done
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.
Retrieve CPU usage and memory usage of a single process on Linux?
ps -p <pid> -o %cpu,%mem,cmd
(You can leave off "cmd" but that might be helpful in debugging).
Note that this gives average CPU usage of the process over the time it has been running.
Peak memory usage of a linux/unix process
Here's a one-liner that doesn't require any external scripts or utilities and doesn't require you to start the process via another program like Valgrind or time, so you can use it for any process that's already running:
grep ^VmPeak /proc/$PID/status
(replace $PID
with the PID of the process you're interested in)
How to see top processes sorted by actual memory usage?
First, repeat this mantra for a little while: "unused memory is wasted memory". The Linux kernel keeps around huge amounts of file metadata and files that were requested, until something that looks more important pushes that data out. It's why you can run:
find /home -type f -name '*.mp3'
find /home -type f -name '*.aac'
and have the second find
instance run at ridiculous speed.
Linux only leaves a little bit of memory 'free' to handle spikes in memory usage without too much effort.
Second, you want to find the processes that are eating all your memory; in top
use the M
command to sort by memory use. Feel free to ignore the VIRT
column, that just tells you how much virtual memory has been allocated, not how much memory the process is using. RES
reports how much memory is resident, or currently in ram (as opposed to swapped to disk or never actually allocated in the first place, despite being requested).
But, since RES
will count e.g. /lib/libc.so.6
memory once for nearly every process, it isn't exactly an awesome measure of how much memory a process is using. The SHR
column reports how much memory is shared with other processes, but there is no guarantee that another process is actually sharing -- it could be sharable, just no one else wants to share.
The smem
tool is designed to help users better gage just how much memory should really be blamed on each individual process. It does some clever work to figure out what is really unique, what is shared, and proportionally tallies the shared memory to the processes sharing it. smem
may help you understand where your memory is going better than top
will, but top
is an excellent first tool.
How to log the memory consumption on Linux?
A small script like
rm memory.log
while true; do free >> memory.log; sleep 1; done
In Linux, how to tell how much memory processes are using?
Getting right memory usage is trickier than one may think. The best way I could find is:
echo 0 $(awk '/TYPE/ {print "+", $2}' /proc/`pidof PROCESS`/smaps) | bc
Where "PROCESS" is the name of the process you want to inspect and "TYPE" is one of:
Rss
: resident memory usage, all memory the process uses, including all memory this process shares with other processes. It does not include swap;Shared
: memory that this process shares with other processes;Private
: private memory used by this process, you can look for memory leaks here;Swap
: swap memory used by the process;Pss
: Proportional Set Size, a good overall memory indicator. It is the Rss adjusted for sharing: if a process has 1MiB private and 20MiB shared between other 10 processes, Pss is 1 + 20/10 = 3MiB
Other valid values are Size
(i.e. virtual size, which is almost meaningless) and Referenced
(the amount of memory currently marked as referenced or accessed).
You can use watch
or some other bash-script-fu to keep an eye on those values for processes that you want to monitor.
For more informations about smaps
: http://www.kernel.org/doc/Documentation/filesystems/proc.txt.
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.
Linux - Displaying Memory Usage Live
You can use the combination of watch which repeats the specified program and displays its output and free which shows current memory usage
watch free -m
free --help
Usage:
free [options]
Options:
-b, --bytes show output in bytes
-k, --kilo show output in kilobytes
-m, --mega show output in megabytes
-g, --giga show output in gigabytes
--tera show output in terabytes
-h, --human show human-readable output
--si use powers of 1000 not 1024
-l, --lohi show detailed low and high memory statistics
-o, --old use old format (without -/+buffers/cache line)
-t, --total show total for RAM + swap
-s N, --seconds N repeat printing every N seconds
-c N, --count N repeat printing N times, then exit
--help display this help and exit
-V, --version output version information and exit
For more details see free(1).
watch --help
Usage:
watch [options] command
Options:
-b, --beep beep if command has a non-zero exit
-c, --color interpret ANSI color sequences
-d, --differences[=]
highlight changes between updates
-e, --errexit exit if command has a non-zero exit
-g, --chgexit exit when output from command changes
-n, --interval seconds to wait between updates
-p, --precise attempt run command in precise intervals
-t, --no-title turn off header
-x, --exec pass command to exec instead of "sh -c"
-h, --help display this help and exit
-v, --version output version information and exit
how to monitor a processes memory usage over time - ubuntu
I use the following:
$ ps -o rss $(pgrep executablename)
Put that in a while loop or use watch to monitor it over time coupled with tail. You can also tee it to a file and have gnuplot plot it for you and refresh it every few seconds.
Related Topics
Fork() and Stdout/Stderr to The Console from Child Processes
How to Convert Dynamically Linked Application to Statically One
How to Translate X11 Keycode Back to Scancode or Hid Usage Id Reliably
Calculate Total Disk I/O by a Single Process
Distro for Linux Kernel Development
Busybox in Embedded Linux Shows "Applet Not Found"
Does File ".Bash_History" Always Record Every Command I Ever Issue
Running a Script Just Before Installation of Debian Finishes Using Preseed
Gcloud - How to Automate Installation of Gcloud on a Server
Is There a Libc in Kernel Space
Vagrant Ssh -C and Keeping a Background Process Running After Connection Closed
Embedding an Application (In This Case a Terminal) Within a Qt Application
Why Use G++ Instead of Gcc to Compile *.Cc Files
How to Measure Mispredictions for a Single Branch on Linux
How to Remount The /Proc Filesystem in a Docker as a R/W System
In Linux, Physical Memory Pages Belong to The Kernel Data Segment Are Swappable or Not