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?


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.


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!