Our site remains ad-free thanks to Linode. See if Linode works for you with $100 in credit. Accelerate innovation with simple, affordable, and accessible Linux cloud solutions and services. See why developers around the world trust Linode.
The post is not sponsored by the above affiliate and the content here is not representative of their company.
In the sections below you will learn how to identify the amount of memory that is installed on your server, check how much of the RAM is being used, and see a list of the applications using the most memory. Other than physical memory that is installed on your server, servers may also have burst memory, VSwap, and Swap memory.
Before the main tutorial, some basic knowledge about the types of memory may come in handy. First we have physical memory which is the memory that is physically installed onto the motherboard. Swap memory is used when the physical memory has approached its limit; your operating system will start storing memory onto your hard drive, also known as Swap memory. VSwap is similar to Swap memory, but VSwap is used for VPS servers that use OpenVZ. VSwap allocates unused physical memory from the server and provides it as Swap Memory, but it does artificially slow down the reading and writing to the memory. Finally burst memory is something that mostly belongs in the past. Providers would provide additional memory to your VPS when you were starting to run low on memory if any additional memory on the server was available.
Check Amount of Memory
Checking the amount of physical memory on your Linux server is very simple. You want to use the command
free to display the amount of memory that is currently used, free, and available. By default, the
free command will use kilobytes for each column. I prefer using the option for
free --human to display the values in a unit that better represents the value.
The command returns multiple columns: total, used, free, shared, buffers, cache, buff/cache, and available. The column “total” displays the total installed memory. Next the “used” columns displays the amount of memory that is currently being used. The column “free” displays the amount of memory that is unused. Shared shows the memory used by tmpfs. Buffers displays the memory used by kernel buffers. Cache shows the memory used by the page cache and slabs. Buff/cache shows the sum of the “buffers” and “cache” columns. Finally the “available” columns is an estimate on how much memory is available for starting new applications without swapping.
$ free --human total used free shared buff/cache available Mem: 7.3G 837M 5.0G 219M 1.5G 6.0G Swap: 7.4G 0B 7.4G
Looking at the results above, the device has a total of 7.3 gigabytes of physical memory with 837 megabytes currently being used. The device also has 7.4 gigabytes of Swap memory with zero bytes being used.
Check Amount of Used Memory
Checking the amount of used memory is identical to checking the amount of available memory that we described above. You will use the command
free and look under the column “used” to view how much memory is currently allocated. Below is an example of how to check how much memory is currently free in kilobytes.
$ free total used free shared buff/cache available Mem: 7608168 973068 5082048 224184 1553052 6140968 Swap: 7807996 0 7807996
As you can see, under the “used” column in align with the “Mem” row, we are using 973068 kilobytes of memory out of the available 7608168 kilobytes that is available. As for “Swap,” we are using zero kilobytes of Swap memory. It’s possible that the amount of used memory will not align with the amount of memory being used by active processes on your system. If a process doesn’t properly free the allocated memory by the time it exits, the memory is still used even though it won’t appear in process lists.
List Programs using the Most Memory
Depending on your preferences, there are multiple programs that can list the active processes on your system along with the system resources each process is using. This tutorial will using the command
ps to list processes and order the list by the memory usage. We are sorting by “RSS” (Resident Set Size) instead of “VSZ” (Virtual Memory Size) to get the amount of memory that is actually being stored in physical memory and isn’t swapped out. The “RSS” column will also include the amount of memory shared resources have allocated. For additional information on RSS and VSZ, read this StackOverflow answer explaining the difference between VSZ and RSS.
$ ps aux --sort -rss USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 5793 1.6 38.8 199492 195976 tty1 S+ 02:39 0:00 ./memory_hog root 699 0.0 1.3 97360 6996 ? Ss 02:28 0:00 sshd: user [priv] root 1 0.2 1.3 56964 6752 ? Ss 02:27 0:01 /sbin/init user 701 0.0 1.2 64868 6352 ? Ss 02:29 0:00 /lib/systemd/systemd --user root 733 0.0 1.1 56420 5936 ? Ss 02:29 0:00 /lib/systemd/systemd --user root 351 0.0 1.1 69940 5604 ? Ss 02:27 0:00 /usr/sbin/sshd -D root 736 0.0 1.0 21112 5116 tty1 S 02:29 0:00 -bash
As you can see from the snippet above from the
ps command, the program
memory_hog is using 195976 kilobytes of memory and is the process that is currently using the most physical memory on the system. Sorting by RSS can give you a false view of what processes are using the majority of memory though. If an application allocates a small amount of memory but is using a shared library, the application may appear higher in the list even though it’s not using any additional memory as long as more than one application is using the shared library.