Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

6 Answers 6

up vote 57 down vote accepted

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.

share|improve this answer
    
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 '10 at 17:48
4  
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. –  sidewaysmilk Oct 26 '10 at 18:49

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!

share|improve this answer
    
htop is nice because it's more "graphical" and possibly easier to read than free. –  mjswensen Nov 26 '13 at 22:32
    
i don't understand the output. Is RES the memory used by that application in MB? THen what is SHR? –  faizal Aug 19 at 13: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). The script even consolidates memory shared across multiple threads of a process (see apache2 in the example below).

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.

share|improve this answer
    
very good script –  soField Aug 12 at 18:25

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

share|improve this answer
    
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 '10 at 17:44

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
share|improve this answer
    
Thanks, this informative! 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 '10 at 17:45
    
@Jonas Updated my answer. –  Alvin Row Oct 26 '10 at 17:53
    
Thanks, looks great. So this 947 is memory usage minus the memory used for buffers and caches? –  Jonas Oct 26 '10 at 17:54

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.

share|improve this answer
    
Do you have a link to it or instructions? - I would like to be have the opportunity to try it. –  starbeamrainbowlabs Jun 17 at 15:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.