In the Linux operating system it is important to have enough free physical memory (RAM) available for your processes which need to run. If this memory runs out, swap space will start to be used next, which will result in slower performance as I/O operations will need to be written to disk instead. If both main memory and swap become full your Linux system can completely freeze up.
By checking and monitoring the amount of memory in use and confirming there is enough free memory to service requests we can ensure that processes continue to run optimally.
Commands to check Linux memory usage
- The ‘free’ command.
- The ‘top’ command.
- The /proc/meminfo file
The ‘free’ command will provide the most accurate way of showing memory use, when run with the -m flag the output is easier to read as values will be shown in MB.
root@server1 [~]# free -m total used free shared buffers cached Mem: 3948 3248 700 0 245 2036 -/+ buffers/cache: 966 2982 Swap: 3999 675 3324
If you see that the amount of used Mem is high and free Mem is low, don’t panic (at least not straight away) as there is usually memory held for caching purposes to help speed things up. The best indication of unused memory is the free column for the buffers/cache row. This displays how much memory is completely unused and available, as the memory used by buffers/cache can quickly be used elsewhere should it be required.
Linux is smart enough to take some of your unused memory for disk caching to increase performance, however it can make it appear as if you have less freely available memory than you actually do. It’s technically in use by the operating system which is why it shows as in use, however should you actually require any of this memory elsewhere it will be ready to use straight away, so you may not necessarily be low on memory.
In the above example, the used Mem shows as 3248mb with 700mb free however this is not the best indication of free memory. The best would be the 2982mb listed free in buffers/cache.
The free command will also show your used and free swap usage, ideally 0 swap will be used, meaning that nothing has been swapped out from physical memory to hard disk. If this value is higher than 0, you should investigate memory usage as it may have filled up and spilled over to disk at some point. In our above example we have some minor swap usage which has not changed in some time and there is plenty of free memory, so currently it looks alright.
If swap fills up it is possible to assign more disk space to increase it, however first you would be better off confirming the additional memory usage is required, and then you would want to increase physical memory.
The top command is a well known way of getting a fast overview of resource usage in Linux dynamically in real time. Top shows us both the total memory available including how much of it is in use and how much is free. The top command also shows us how much swap space is available including how much is in use and free. These values include memory used by buffers and cache, similar to the first line shown in the ‘free’ command.
top - 22:39:56 up 7 days, 9:58, 1 user, load average: 0.03, 0.05, 0.00 Tasks: 124 total, 1 running, 123 sleeping, 0 stopped, 0 zombie Cpu(s): 0.2%us, 0.0%sy, 0.0%ni, 99.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 4043720k total, 3325664k used, 718056k free, 251452k buffers Swap: 4095992k total, 691520k used, 3404472k free, 2085212k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 5034 mysql 15 0 1443m 207m 6552 S 0.0 5.3 20:07.09 mysqld 8320 username 16 0 149m 46m 6848 S 0.0 1.2 0:11.94 php 8631 username 16 0 148m 45m 6832 S 0.0 1.2 0:21.39 php 8269 username 16 0 146m 43m 7252 S 0.0 1.1 0:14.85 php 8694 username 15 0 145m 43m 7272 S 0.0 1.1 0:12.74 php 8695 username 16 0 145m 42m 7276 S 0.0 1.1 0:12.60 php 9110 username 16 0 142m 40m 7248 S 0.0 1.0 0:03.30 php 8604 username 16 0 140m 37m 7192 S 0.0 1.0 0:03.86 php 8603 username 15 0 138m 35m 7204 S 0.0 0.9 0:05.54 php 8289 username 16 0 184m 29m 6648 S 0.0 0.7 0:06.77 php 2158 root 11 -10 28704 22m 1744 S 0.0 0.6 0:00.01 iscsiuio 8768 username 16 0 179m 20m 6680 S 0.0 0.5 0:01.44 php 8290 username 16 0 175m 17m 6336 S 0.0 0.5 0:14.69 php 8106 nobody 20 0 416m 12m 7316 S 0.0 0.3 0:00.97 httpd 8113 nobody 19 0 351m 10m 6832 S 0.0 0.3 0:00.08 httpd
We can then go a step further and sort the processes in top by amount of memory in use, allowing us to rank the processes from highest memory use to lowest. This will help us see if there are any memory heavy processes that may need to be investigated further. To sort the top output by memory, press the “shift + m” keys.
The output in the above example has been sorted this way, we can see that the mysqld process is using the most amount of memory, which makes sense as it has been configured to cache databases.
If you’re after an even more detailed break down on your memory, take a look at the /proc/meminfo file.
root@server1 [~]# cat /proc/meminfo MemTotal: 4043720 kB MemFree: 715944 kB Buffers: 251940 kB Cached: 2085324 kB SwapCached: 206052 kB Active: 1862372 kB Inactive: 1141632 kB HighTotal: 0 kB HighFree: 0 kB LowTotal: 4043720 kB LowFree: 715944 kB SwapTotal: 4095992 kB SwapFree: 3404472 kB Dirty: 740 kB Writeback: 0 kB AnonPages: 626324 kB Mapped: 34684 kB Slab: 284540 kB PageTables: 15068 kB NFS_Unstable: 0 kB Bounce: 0 kB CommitLimit: 6117852 kB Committed_AS: 3057500 kB VmallocTotal: 34359738367 kB VmallocUsed: 265844 kB VmallocChunk: 34359471351 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 Hugepagesize: 2048 kB
Summary
We have covered a few tools you can use to see how much physical memory (RAM) is in use in Linux. The free command will show memory in use and free memory for the whole system including memory used by buffers and cache, while the top command will show real time usage information of memory broken down by specific processes allowing you to see how much memory each process is using.
0 Comments.