You need to use the free> command which, displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel. Another option is to either query /proc/meminfo file or use vmstat comamnd. The /proc/meminfo file also provide the same information.
Tutorial details | |
---|---|
Difficulty | Intermediate (rss) |
Root privileges | Yes |
Requirements | None |
Time | 5m |
Let us see how to use these commands quickly on Linux to get memory utilization information from the shell prompt.
free command examples
Type the free command at shell prompt to display amount of free and used memory in the system in human readable format from /proc/meminfo:
$ free
$ free -m
Output:
total used free shared buffers cached Mem: 2010 1965 45 0 152 776 -/+ buffers/cache: 1036 974 Swap: 2047 137 1910
The -b,-k,-m,-g option show output in bytes, KB, MB, or GB. As I said eariler you use cat command to display contents of /proc/meminfo:
$ cat /proc/meminfo
$ less /proc/meminfo
Sample outputs:
MemTotal: 12195116 kB MemFree: 157328 kB Buffers: 375424 kB Cached: 9298412 kB SwapCached: 22616 kB Active: 5714924 kB Inactive: 5473744 kB Active(anon): 870136 kB Inactive(anon): 644700 kB Active(file): 4844788 kB Inactive(file): 4829044 kB Unevictable: 0 kB Mlocked: 0 kB SwapTotal: 6291448 kB SwapFree: 6168080 kB Dirty: 35760 kB Writeback: 0 kB AnonPages: 1493084 kB Mapped: 567616 kB Shmem: 4 kB Slab: 667828 kB SReclaimable: 566912 kB SUnreclaim: 100916 kB KernelStack: 3240 kB PageTables: 54692 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 12389004 kB Committed_AS: 4022128 kB VmallocTotal: 34359738367 kB VmallocUsed: 52908 kB VmallocChunk: 34359680588 kB HardwareCorrupted: 0 kB AnonHugePages: 1212416 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB DirectMap4k: 7488 kB DirectMap2M: 12574720 kB
Update free command every [delay] seconds
To update every 5 seconds, enter:
# free -s 5
Pass the -c option to update [count] times i.e. update free command every 2 seconds 3 times, enter:
# free -s 2 -c 3
Sample outputs:
total used free shared buffers cached Mem: 12195116 11992716 202400 0 374820 9255356 -/+ buffers/cache: 2362540 9832576 Swap: 6291448 123368 6168080 total used free shared buffers cached Mem: 12195116 11992468 202648 0 374820 9255112 -/+ buffers/cache: 2362536 9832580 Swap: 6291448 123368 6168080 total used free shared buffers cached Mem: 12195116 11993700 201416 0 374824 9255140 -/+ buffers/cache: 2363736 9831380 Swap: 6291448 123368 6168080
Sai hello to top / atop / htop commands
The top command display Linux tasks including memory used by process. atop command or htop commands are top like tools with additional options:
# top
# atop
# htop
Sample outputs:
vmstat command examples
vmstat command provides more information :
$ vmstat
Output:
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 140480 43636 158196 797692 1 0 108 220 1 4 7 5 87 1 0
Understanding vmstat memory options
- swpd: the amount of virtual memory used.
- free: the amount of idle memory.
- buff: the amount of memory used as buffers.
- cache: the amount of memory used as cache.
- inact: the amount of inactive memory (see -a option).
- active: the amount of active memory (see -a option).
$ vmstat -a
Output:
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r b swpd free inact active si so bi bo in cs us sy id wa st
0 1 140480 37376 109516 1730040 1 0 108 220 1 4 7 5 87 1 0
The following command displays one new line of utilization data every second
$ vmstat 1
The following command displays one new line per 2 second, but only for the next 10 seconds:
$ vmstat 2 10
Sample outputs:
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ r b swpd free inact active si so bi bo in cs us sy id wa st 4 0 139216 23508 130644 1723680 1 0 108 220 1 5 7 5 87 1 0 2 0 139216 23252 130668 1723816 0 0 0 410 3242 11472 9 7 84 0 0 1 0 139216 23120 130656 1724012 0 0 0 750 3280 11592 3 6 90 1 0 0 0 139216 22996 130588 1724180 0 0 0 426 3272 11052 2 5 93 0 0 2 0 139216 20988 129932 1726980 0 0 6 1146 3353 12105 14 9 74 2 0 1 0 139216 20244 129900 1727216 0 0 0 392 3238 11752 8 7 85 0 0 1 0 139216 20120 129868 1727352 0 0 0 444 3197 11173 2 5 93 0 0 1 0 139216 25964 129852 1721044 0 0 0 268 3147 9269 1 4 95 0 0 3 0 139216 25964 129748 1721196 0 0 2 132 3199 10540 1 4 95 0 0 1 0 139216 25964 129676 1721332 0 0 0 456 3213 10608 2 4 93 1 0
Show memory statistics using vmstat
Pass the -s option to vmstat command displays a table of various event counters and memory statistics:
# vmstat -s
Sample outputs:
12195116 total memory 12020152 used memory 5697412 active memory 5475156 inactive memory 174964 free memory 375496 buffer memory 9299280 swap cache 6291448 total swap 123368 used swap 6168080 free swap 9174417 non-nice user cpu ticks 2353 nice user cpu ticks 3338415 system cpu ticks 831004606 idle cpu ticks 327853 IO-wait cpu ticks 9074 IRQ cpu ticks 1075734 softirq cpu ticks 0 stolen cpu ticks 5001944 pages paged in 1015656377 pages paged out 28385 pages swapped in 54024 pages swapped out 2347308881 interrupts 1694761409 CPU context switches 1387092323 boot time 246261 forks
Related media
This tutorial is also available in a quick video format:
Video 01: 5 Linux Commands: To See Amount Of Free and Used Memory
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 6 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Hi Vivek,
How can I know the memory usage for a particular process in Linux.
Thank you,
Vijay
Thank’s a lot yar…………thank’s
Hi Vivek
What is the difference between free memory and inactive memory
Thanks
sujith
Inactive memory is the pages that are used or allocated for some other processes earlier, but not in use now. Hence if it exhausts on free memory it will utilize form this set. The only advantage of inactive pages is, it saves time if the same page ( that is in inactive set ) is needed, it can be used without bringing in the page. If you want to reduce the inactive page, do as following:
dd if=/dev/urandom of=/tmp/test_file bs=20M
Now after writing for 1-2 GB of file, stop dd and delete it. You can see the respective amount of mem being added to free from inactive. Though this is not suggested as you might be losing the inactive pages, which can become valuable for other processes when they start.
wow.. thank you sreeharsha!. your trick solved my problem.
how to increase the file descriptor values in linux. . .