- How can I monitor the memory usage?
- 13 Answers 13
- 10 ‘free’ Commands to Check Memory Usage in Linux
- 1. Display System Memory
- 2. Display Memory in Bytes
- 3. Display Memory in Kilo Bytes
- 4. Display Memory in Megabytes
- 5. Display Memory in Gigabytes
- 6. Display Total Line
- 7. Disable Display of Buffer Adjusted Line
- 8. Display Memory Status for Regular Intervals
- 9. Show Low and High Memory Statistics
- 10. Check Free Version
How can I monitor the memory usage?
I have used top to see the memory usage at the moment. But I would like to monitor the memory usage over a period of time. E.g start monitoring and then execute a few commands, and final stop the monitoring and see how much memory that have been used during the period. How can I do this on Ubuntu Server? I guess I could start a cronjob every 5th second or so, and invoke a command that log the current memory usage in a textfile. But what command should I use to get the current memory usage in a format that is easy to log to a text file?
13 Answers 13
I recommend combining the previous answers
Note that Linux likes to use any extra memory to cache hard drive blocks. So you don’t want to look at just the free Mem . You want to look at the free column of the -/+ buffers/cache: row. This shows how much memory is available to applications. So I just ran free -m and got this:
total used free shared buffers cached Mem: 3699 2896 802 0 247 1120 -/+ buffers/cache: 1528 2170 Swap: 1905 62 1843
I know that I’m using 1528 MB and have 2170 MB free.
Note: To stop this watch cycle you can just press Ctrl + C .
Thanks, that’s informative. But on the used column you first have 2896 and then 1528 for buffers, doesn’t this mean that you are using 2896 + 1528 ?
Mem: used is your total used memory. -/+ buffers/cache: used is your total used memory minus buffers and cache. I know the output looks funny, but no arithmetic is required here. You’re just looking for used/free in the -/+ buffers/cache row.
@frmdstryr Good point! -h/—human did not exist when I wrote this, though. gitlab.com/procps-ng/procps/commit/…
@cbradsh1 You can just do free -h , e.g. watch -n 5 free -h to get «human readable» output, e.g. 2.1G instead of 2170 in the output.
I think htop is the best solution.
This way you will notice what programs is using most RAM. and you can easily terminate one if you want to. Here’s a screenshot!
If you looking for a nice breakdown of the memory used by each running process, then I might recommend checking out ps_mem.py (found here at pixelbeat.org).
I know in the comments above, you mentioned wanting a one-line snapshot from free, but I figured others might find this useful.
user@system:~$ sudo ps_mem.py [sudo] password for user: Private + Shared = RAM used Program 4.0 KiB + 7.5 KiB = 11.5 KiB logger 4.0 KiB + 8.0 KiB = 12.0 KiB mysqld_safe 4.0 KiB + 10.0 KiB = 14.0 KiB getty 4.0 KiB + 42.0 KiB = 46.0 KiB saslauthd (5) 48.0 KiB + 13.0 KiB = 61.0 KiB init 56.0 KiB + 27.5 KiB = 83.5 KiB memcached 84.0 KiB + 26.5 KiB = 110.5 KiB cron 120.0 KiB + 50.0 KiB = 170.0 KiB master 204.0 KiB + 107.5 KiB = 311.5 KiB qmgr 396.0 KiB + 94.0 KiB = 490.0 KiB tlsmgr 460.0 KiB + 65.0 KiB = 525.0 KiB rsyslogd 384.0 KiB + 171.0 KiB = 555.0 KiB sudo 476.0 KiB + 83.0 KiB = 559.0 KiB monit 568.0 KiB + 60.0 KiB = 628.0 KiB freshclam 552.0 KiB + 259.5 KiB = 811.5 KiB pickup 1.1 MiB + 80.0 KiB = 1.2 MiB bash 1.4 MiB + 308.5 KiB = 1.7 MiB fail2ban-server 888.0 KiB + 1.0 MiB = 1.9 MiB sshd (3) 1.9 MiB + 32.5 KiB = 1.9 MiB munin-node 13.1 MiB + 86.0 KiB = 13.2 MiB mysqld 147.4 MiB + 36.5 MiB = 183.9 MiB apache2 (7) --------------------------------- 208.1 MiB ================================= Private + Shared = RAM used Program
The only part I don’t like is the fact that the script claims to require root privileges. I haven’t had an opportunity yet to see exactly why this is the case.
I wonder whether memory is shared between threads. It is shared between processes, isn’t it? At least on Windows.
So this case, the shared memory refers to pages that are mapped by multiple processes in the form of shared libraries. Additionally within the context of a multi-threaded applications the entire process memory space is accessible by all threads in that process.
@ThomasWeller: Yes, threads always share memory, whereas processes may share some or all of it under certain conditions.
Use the free command. For example, this is the ouput of free -m :
total used free shared buffers cached Mem: 2012 1666 345 0 101 616 -/+ buffers/cache: 947 1064 Swap: 7624 0 7624
free -m | grep /+ will return only the second line:
The watch command may be useful. Try watch -n 5 free to monitor memory usage with updates every five seconds.
Thanks, this was great! However, I would prefer to get the memory usage on a single line, so it’s easy to log to a text file.
You can do it using cat /proc/meminfo .
MemTotal: 4039160 kB MemFree: 309796 kB MemAvailable: 3001052 kB Buffers: 345636 kB Cached: 2341288 kB SwapCached: 8 kB Active: 1725160 kB Inactive: 1551652 kB Active(anon): 538404 kB Inactive(anon): 70076 kB Active(file): 1186756 kB Inactive(file): 1481576 kB Unevictable: 32 kB Mlocked: 32 kB SwapTotal: 4194300 kB SwapFree: 4194044 kB Dirty: 0 kB Writeback: 0 kB AnonPages: 589988 kB Mapped: 255972 kB Shmem: 18596 kB Slab: 374888 kB SReclaimable: 310496 kB SUnreclaim: 64392 kB KernelStack: 6976 kB PageTables: 26452 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 6213880 kB Committed_AS: 3589736 kB VmallocTotal: 34359738367 kB VmallocUsed: 0 kB VmallocChunk: 0 kB HardwareCorrupted: 0 kB AnonHugePages: 0 kB ShmemHugePages: 0 kB ShmemPmdMapped: 0 kB CmaTotal: 0 kB CmaFree: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB DirectMap4k: 147392 kB DirectMap2M: 4046848 kB
The free command takes its information from /proc/meminfo and presents them in a compact way. Use free -h for human-readable output.
I just found this solution and it works very well, creating a graphical interface through the CLI:
# fixed link git clone https://github.com/aristocratos/bashtop cd bashtop bash bashtop
+1 for the amazing work on the graphical interface. BTW, the author now advises to use the Python version.
For visual monitoring of overall RAM usage, if you use Byobu, it will keep your memory usage in the lower right-hand corner of the terminal and will run while you are in any terminal session.
As you can see from screenshot, my virtual machine has a 1h3m uptime, 0.00 load, has 2.8GHz (virtual) processor and 994MB (21%) of the RAM available on the system.
Single line solution and output:
Here is an example of the expected output:
Mem: 3944 652 302 18 2990 2930
I would use Cacti. This will graph your memory usage etc over a period of time, and you will be able to check on usage using your web browser.
Monitoring Memory Usage
I’m more in line with one of the preceding posts that mentioned Cacti as a great way to monitor memory usage. However, since it appears cacti is no longer popular in the mainstream, there is an alternative graphing application called Graphite.
Graphite is relatively easy to install on a ubuntu server and to install it, you can check out this link for the easy to follow installation procedures.
After graphite has been installed, now, you can send memory metrics to it, at whichever interval you wish; every 5 seconds, every minute, every hour. etc.
To graph memory metrics, as already suggested in previous posts, you can write your own script using system tools to gather the necessary memory information. Or, you can use a prewritten snmp plugin that’ll do all the work for you.
If you wish to write your own memory script, it’ll be wise to ensure you account for buffered and cached memory when calculating used memory, otherwise, you’ll end up gathering false data.
If you wish to instead utilize an snmp plugin that already does all the necessary calculations for you, here’s a link to one that works pretty well: checkMemoryviaSNMP.
Pros of SNMP:
I have snmp installed on all the remote nodes I monitor. This allows me to monitor all my systems from one central server(s), without having to copy or put a plugin on the remote nodes.
Cons of SNMP:
You’d have to ensure the snmp agent is installed on each of the remote nodes you wish to monitor memory on. However, this installation will be a one time deal. If you’re using automation tools such as chef or puppet or similar tools in your environment, then this isn’t a problem at all.
Configuration of the SNMP agent on the remote node(s):
After the snmp agent has been installed, simply vi the /etc/snmpd/snmpd.conf file and add this line to it:
rocommunity (specify-a-community-string-aka-password-here)
Then restart the snmpd agent, with:
Then, on your central server, from which you instead to monitor all your other servers, you can run the following command:
$ time ./checkMemoryviaSNMP -v2 public gearman001.phs.blah.com 30 90 graphite,10.10.10.10,2003,typical WARNING: Used = [ 3.26154 GB ], Installed = [ 5.71509 GB ], PCT.Used = [ 57.069% ], Available.Memory = [ 2.00291 GB ]. Buffer = [ 137.594 MB ], Cached = [ 1.3849 GB ]. Thresholds: [ W=(30%) / C=(90%) ]. System Information = [ Linux gearman001.phs.blah.com 2.6.32-504.30.3.el6.x86_64 #1 SMP Thu Jul 9 15:20:47 EDT 2015 x86_64 ]. real 0m0.23s user 0m0.03s sys 0m0.02s
10 ‘free’ Commands to Check Memory Usage in Linux
Linux is one of the most popular open source operating system and comes with huge set of commands. The most important and single way of determining the total available space of the physical memory and swap memory is by using “free” command.
The Linux “free” command gives information about total used and available space of physical memory and swap memory with buffers used by kernel in Linux/Unix like operating systems.
This article provides some useful examples of “free” commands with options, that might be useful for you to better utilize memory that you have.
1. Display System Memory
Free command used to check the used and available space of physical memory and swap memory in KB. See the command in action below.
# free total used free shared buffers cached Mem: 1021628 912548 109080 0 120368 655548 -/+ buffers/cache: 136632 884996 Swap: 4194296 0 4194296
2. Display Memory in Bytes
Free command with option -b, display the size of memory in Bytes.
# free -b total used free shared buffers cached Mem: 1046147072 934420480 111726592 0 123256832 671281152 -/+ buffers/cache: 139882496 906264576 Swap: 4294959104 0 4294959104
3. Display Memory in Kilo Bytes
Free command with option -k, display the size of memory in (KB) Kilobytes.
# free -k total used free shared buffers cached Mem: 1021628 912520 109108 0 120368 655548 -/+ buffers/cache: 136604 885024 Swap: 4194296 0 4194296
4. Display Memory in Megabytes
To see the size of the memory in (MB) Megabytes use option as -m.
# free -m total used free shared buffers cached Mem: 997 891 106 0 117 640 -/+ buffers/cache: 133 864 Swap: 4095 0 4095
5. Display Memory in Gigabytes
Using -g option with free command, would display the size of the memory in GB(Gigabytes).
# free -g total used free shared buffers cached Mem: 0 0 0 0 0 0 -/+ buffers/cache: 0 0 Swap: 3 0 3
6. Display Total Line
Free command with -t option, will list the total line at the end.
# free -t total used free shared buffers cached Mem: 1021628 912520 109108 0 120368 655548 -/+ buffers/cache: 136604 885024 Swap: 4194296 0 4194296 Total: 5215924 912520 4303404
7. Disable Display of Buffer Adjusted Line
By default the free command display “buffer adjusted” line, to disable this line use option as -o.
# free -o total used free shared buffers cached Mem: 1021628 912520 109108 0 120368 655548 Swap: 4194296 0 4194296
8. Display Memory Status for Regular Intervals
The -s option with number, used to update free command at regular intervals. For example, the below command will update free command every 5 seconds.
# free -s 5 total used free shared buffers cached Mem: 1021628 912368 109260 0 120368 655548 -/+ buffers/cache: 136452 885176 Swap: 4194296 0 4194296
9. Show Low and High Memory Statistics
The -l switch displays detailed high and low memory size statistics.
# free -l total used free shared buffers cached Mem: 1021628 912368 109260 0 120368 655548 Low: 890036 789064 100972 High: 131592 123304 8288 -/+ buffers/cache: 136452 885176 Swap: 4194296 0 4194296
10. Check Free Version
The -V option, display free command version information.
# free -V procps version 3.2.8