Check Memory Usage in Linux

1, Free command –> The most effective tool for finding the memory usage and other details. The m option displays all data in MBs.

free -m
total used free shared buffers cached
Mem: 22528 14872 7655 1048 0 13497
-/+ buffers/cache: 1374 21153
Swap: 4096 1461 2634

2,/proc/meminfo –> Another way is to read the /proc/meminfo file. The important values can be find from this. Please check sample result.

cat /proc/meminfo
MemTotal: 23068672 kB
MemFree: 7839488 kB
Cached: 13890932 kB
SwapTotal: 4194304 kB
SwapFree: 2697752 kB
Shmem: 1143948 kB

3,vmstat –> The vmstat command with the s option, lays out the memory usage statistics much like the proc command.

vmstat -s
23068672 total memory
15195280 used memory
7472596 active memory
7478680 inactive memory
7873392 free memory
0 buffer memory
13855964 swap cache
4194304 total swap
1496552 used swap
2697752 free swap

4, Top command and htop command will also give all the information. The result from top command is given below.

Mem: 23068672k total, 15279760k used, 7788912k free, 0k buffers
Swap: 4194304k total, 1496184k used, 2698120k free, 13857660k cached

5, Ram information –> To find out hardware information about the installed RAM, use the demidecode command. It reports lots of information about the installed RAM memory.

dmidecode -t 17

Advertisements

Better understanding of linux free memory

Usually we all have confusion regarding the amount of free memory available while running the command free -m. In the following example

free -m
total used free shared buffers cached
Mem: 72348 71972 375 0 6242 41377
-/+ buffers/cache: 24352 47995
Swap: 4095 1309 2786

In the above result, from the top line we can see that out of 72GB available 71GB is being used, but we don’t see any such high memory usage in the server. But you can see lot of memory in cached. The actual memory is being used is in the disk cache. Cached memory is essentially free, so that it can be quickly taken if a running (or newly starting) program needs the memory.

The reason Linux uses so much memory for disk cache is because the RAM is wasted if it isn’t used. Keeping the cache means that if something needs the same data again, there’s a good chance it will still be in the cache in memory. Fetching the information from there is around 1,000 times quicker than getting it from the hard disk. If it’s not found in the cache, the hard disk needs to be read anyway.

The -/+ buffers/cache line shows how much memory is used and free from the perspective of the application.

The difference between buffers and cache –> Buffers are associated with a specific block device, and cover caching of filesystem metadata as well as tracking in-flight pages. The cache only contains parked file data.
That is, the buffers remember what’s in directories, what file permissions are, and keep track of what memory is being written from or read to for a particular block device. The cache only contains the contents of the files themselves.

swapon: cannot find the device for LABEL=SWAP

Recently I have faced an issue in which the swap was not present.

[root@test] ~ >> free -m

total used free shared buffers cached
Mem: 4974 2584 1390 0 12 1757
-/+ buffers/cache: 814 7159
Swap: 0 0 0

You can see that the swap is zero. So I have tried to re-enable swap using swapon command

swapon -a
swapon: cannot find the device for LABEL=SWAP-sdb2

On checking the fsidk of the drive, I could see that swap partition was set up there

fdisk -l

Disk /dev/sdb: 898.9 GB, 598999040000 bytes
255 heads, 63 sectors/track, 72824 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 25 200781 83 Linux
/dev/sdb2 26 808 6289447+ 82 Linux swap / Solaris

As you can see in the fdisk, swap partition already enabled. “/dev/sdb2” was the partition

The only thing is that swap was missing the label. We can give label to swap using mkswap

mkswap -L SWAP-sdb2 /dev/sdb2

Setting up swapspace version 1, size = 6440386 kB
LABEL=SWAP-sdb2, no uuid

Now turn on swap using swapon

swapon -a

[root@test] ~ >> free -m

total used free shared buffers cached
Mem: 4974 2584 1390 0 12 1757
-/+ buffers/cache: 814 7159

Swap: 3142 0 3142