The program absolutely runs in copy paste

Monitor the memory state of ec2 with watch and free command




Hello everyone, It’s candle.
In this time, I will show you how to monitor ec2 memory in real time.
EC2 server of t2.micro has only 1GB of memory, If you run composer, rails, mysql on it, memory may be barely.
It is useful if you can monitor the memory and tune the server.


EC2 server


free command to monitor memory

The command to display the current memory status is “free”.
Let’s run it.


Total memory 1GB, you can see that 300MB is used.
With the free command only, you can’t keep monitoring the memory status.

Combine with watch command

The watch command is generally used as follows.

watch -n “time span” “command to repeat”

For example, you want to run free command each one second.

watch -n 1 free

Watching for a while, the value of “used” and “free” will change.
If you stop the process, type “ctrl + c”.

The -d flag to color the changed place is also useful.

watch -n 1 -d free

making a function.

Open the .bash_profile and write next code.

function lf(){
  watch -n 1 -d free

Save it, reload .bash_profile.

source ~/.bash_profile

Run lf command, you can monitor memory.



It can be used in various situations of the server.

 - サーバ