345

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

417

I recommend combining the previous answers

watch -n 5 free -m

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.

8
  • 1
    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?
    – Jonas
    Oct 26, 2010 at 17:48
  • 10
    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. Oct 26, 2010 at 18:49
  • 9
    The -h option is a lot nicer
    – frmdstryr
    Oct 19, 2017 at 20:11
  • 1
    @frmdstryr Good point! -h/--human did not exist when I wrote this, though. gitlab.com/procps-ng/procps/commit/… Oct 20, 2017 at 18:51
  • 4
    @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. May 16, 2018 at 22:11
273

I think htop is the best solution.

  • sudo apt-get install htop

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!

5
  • 3
    htop is nice because it's more "graphical" and possibly easier to read than free.
    – mjswensen
    Nov 26, 2013 at 22:32
  • 3
    i don't understand the output. Is RES the memory used by that application in MB? THen what is SHR?
    – faizal
    Aug 19, 2014 at 13:51
  • 2
    @faizal: virtual, reserved & shared memory.
    – WitchCraft
    Sep 19, 2015 at 7:47
  • 2
    how does htop show you the ' the memory usage over a period of time'? Aug 7, 2016 at 0:57
  • htop -s M_SHARE htop -s TIME
    – Tomachi
    Aug 21, 2018 at 5:08
51

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.

Example output:

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.

7
  • I wonder whether memory is shared between threads. It is shared between processes, isn't it? At least on Windows... Dec 27, 2014 at 12:51
  • 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.
    – Jason Mock
    Oct 1, 2015 at 23:37
  • 4
    Now available in PyPI (pip install ps_mem) and on GitHub. Jan 9, 2017 at 14:39
  • @ThomasWeller: Yes, threads always share memory, whereas processes may share some or all of it under certain conditions. Jan 9, 2017 at 14:44
  • for this I use gnome-system-monitor
    – user372194
    Dec 26, 2017 at 7:39
26

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:

-/+ buffers/cache:        947       1064
1
  • Thanks, looks great. So this 947 is memory usage minus the memory used for buffers and caches?
    – Jonas
    Oct 26, 2010 at 17:54
13

The watch command may be useful. Try watch -n 5 free to monitor memory usage with updates every five seconds.

1
  • 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.
    – Jonas
    Oct 26, 2010 at 17:44
13

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
2
  • 1
    adding watch to the front of this command makes it awesome! as in watch cat /proc/meminfo
    – Elder Geek
    Nov 9, 2016 at 3:58
  • The free command takes its information from /proc/meminfo and presents them in a compact way. Use free -h for human-readable output.
    – LoMaPh
    Feb 17, 2018 at 8:51
10

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

Screenshot

2
  • 3
    +1 for the amazing work on the graphical interface. BTW, the author now advises to use the Python version. Oct 26, 2020 at 20:20
  • 1
    Amazing work on the graphic and the responsiveness to terminal scaling. Jun 9, 2022 at 19:48
4

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.

Byobu in use

4

Single line solution and output:

free -m | grep "Mem:"

Here is an example of the expected output:

Mem:           3944         652         302          18        2990        2930
0
1

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.

0
1

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:

/etc/init.d/snmpd restart

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
1

Typing free in your command terminal provides the following result:

The data represents the used/available memory and the swap memory figures in kilobytes.

total - Total installed memory

used - Memory currently in use by running processes (used= total – free – buff/cache)

free - Unused memory (free= total – used – buff/cache)

shared - Memory shared by multiple processes

buffers - Memory reserved by the OS to allocate as buffers when process need them

cached - Recently used files stored in RAM

buff/cache - Buffers + Cache

available - Estimation of how much memory is available for starting new applications, without swapping.

0

glances is another fine option to try. Just pip install Glances or download from here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .