Sometimes it can be a bit challenging to know when and if you should upgrade the RAM (Random Access Memory) on your Linux server. Even more so when deciding how much memory you should add, or, if you have enough memory, how do you make best use of it? This article will walk you through doing command line checking on a Linux server using some real-world examples. Also included is brief advice to help clarify the path forward if you are currently facing Linux server memory management options.
Based on the output of ‘free -m’ on a 1GB VPS and taking into account the amount of swap space in use, take a look at the first screenshot below (details further down), a RAM upgrade was required. The minimum required was 3GB so we upgrade to 6GB. This ensures that there is enough memory available for the buffers/cache. After the memory upgrade (around 9:30), here’s how the load average responded:
There are three additional before and after memory graphs of used versus freed memory:
Checking whether your Linux server needs a memory upgrade
Has your Linux server been running slower than usual lately? Or maybe you’ve seen higher CPU usage with similar traffic levels? Sometimes server performance can change overnight. Which can be due to many reasons like increase in web traffic, change in your database size/queries, your application or network, to name a few. To check the current status of your Linux server’s memory usage, you’ll want to use one or more of the following command line tools: free, Above, vmstat And search and rescue,
free command: check and swap server memory
free The command displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel. This is a more user-friendly way of reading the output of cat /proc/meminfo .
The free command, although simple, used to be one of the most misinterpreted Linux command line tools. However, as of kernel 3.14 (and emulation on kernel 2.6.27+) the Linux free command made some changes. Some of which helped administrators realize they had much less free or available memory than they used to. a large amount of blog posts, Q&A, Material And the server monitoring tools recommend that your Linux server’s free memory = free + buffers + cached = false!
Using: free – m (-m = Display the amount of memory in megabytes). The screenshot below is of the same 1GB memory Stacklinux VPS From above screenshot. Here’s an analysis of what’s being reported:
991 – Total: total installed memory (memtotal and swaptotal in /proc/meminfo)
628 – Used: Memory Used (Calculated as Total – Free – Buffers – Cache)
61 – free: unused memory (memfree and swapfree in /proc/meminfo)
180 – share: shared memory used by tmpfs (mostly) (Shmem in /proc/meminfo, available on kernel 2.6.32, displayed as zero if not available)
301 – buff/cache: Commemoration Used by kernel buffers (buffers in /proc/meminfo)/ Cash: Commemoration Used Pages by cache and slab (cached and slab in /proc/meminfo)
35 – available: Estimate of how much is the memory available To start a new application, without swap, Unlike data provided by cache or free fields, this field takes into account page cache and also that all reclaimable memory slabs will not be reclaimed due to objects in use (MemAvailable /proc/ meminfo, available on kernel 3.14, is emulated on kernels 2.6.27+, otherwise the same as free).
6015 – change: The total amount of swap memory.
1522 – swap used: The amount of swap memory in use.
4493 – free exchange: The amount of swap memory that is not in use.
Older versions of free looked something like this:
free -m total used free shared buffers cached Mem: x x x x x x -/+ buffers/cache: x x Swap: x x x
Note the extra buffers/cache line. to see it buffers/cache In the latest 3.14+ kernel versions of free, use the -w option. For example: free -mw, -hw, -w, etc. when you type cat / proc / meminfo The first 3 lines are: memtotal, memfree, and memavailable. On older Linux installs it appears that the third line (MemAvailable) is not listed.
Then how much free memory do I have?
you will always have something free Memory on Linux. You will see some free, buffered and cached memory even with constant swap I/O. The Linux kernel will use swap space (if swap is enabled) to maximize the amount of free or available memory as possible. As per the screenshot above 300MB is marked as ‘buff/cache’ and 61MB as ‘free’. But over 1.5GB of swap. If swap is not enabled on your server you will see poor performance, low ratio of buffers/cache, system freeze or at worst. On a server with swap space, the best indicator of low memory would be Swap I/O . high frequency of, In this case there is 1GB of RAM but 1.5GB of swap and kswap (the kernel service that manages swapping) is using most of the CPU time.
Bonus: Check Order: slabtop
top: check server memory and swap usage
In the video above I have used top to see memory + swap usage, notice kswap and ‘wa’ spike together. These spikes also clearly show that the performance of the web server is being directly hampered due to kswap swapping in disk swap. As mentioned, kswap is a process used by Linux to manage swap on the system, while ‘wa’ is the I/O wait or time the processor/processors are waiting during which the outstanding There are I/O requests. Let’s look at this more closely using vmstat and sar. Unlike Free and Top Command, the rest of the tools are constantly documented online.
vmstat: check server memory and swap usage
vmstat reports information about processes, memory, paging, block IO, traps, and CPU activity. use vmstat 1 As per above screenshot. Note the Si (Swap In), So (Swap Out) and Wa (Wait) columns. Swap I/O is not always bad, opportunists Swapping can really help with performance. However, a high frequency of swap I/O appears on almost every line (every second). Here’s what the same command looks like on a healthy server.
In the screenshot you can see that even with 1.3G of free RAM the server has 246M of used swap. It is from opportunistic swap. Or swaps performed during idle moments where it does not negatively affect server performance. Usually it’s 246M of data that hasn’t been used for a very long time. Continue reading to find out how you can increase the overall cache maintained by a Linux server without increasing the frequency and size of the swap cache.
sar: check server memory and swap usage
With Sir you can collect, report or save system activity information. Like vmstat, sar can be used for much more than just checking memory and swap. The screenshot above is also of the same 1GB VPS we started with.
Compare these results with Abstract-R Results On a healthy Linux server:
Kernel cache pressure and swapping
Another way to squeeze the most out of your server memory is to tune the system’s latency (propensity to swap) and cache pressure (propensity to reclaim cache).
exchange – This control is used to define how aggressively the kernel will swap memory pages. Higher values will increase aggression, lower values will reduce swap volume. (default = 60, recommended value between 1 and 60) Remove your swap for a 0 value, but this is generally not recommended in most cases.
vfs_cache_pressure Controls the tendency of the kernel to reclaim memory used for caching directory and inode objects. (default=100, recommend value 50 to 200) ,
You can add or change these lines to edit /etc/sysctl.conf file. For example, if you have low memory until you upgrade, you can try something like this:
This will increase cache pressure and may seem counterintuitive to some extent as caching is good for performance. However, swapping also significantly reduces the overall performance of your server. So not keeping much cache in memory will help to reduce swap/swap cache activity. Also, setting vm.swappiness to 10 or less than 1 will reduce disk swapping.
Use the following on a healthy server with lots of available memory:
This will reduce the cash pressure. Since caching is good for performance, we want to keep the cache in memory for a longer period of time. Since the cache will get bigger, we still want to minimize swapping so that it doesn’t cause increased swap I/O.
Use these commands to check the current values using:
sudo cat /proc/sys/vm/swappiness sudo cat /proc/sys/vm/vfs_cache_pressure
To temporarily enable these settings without rebooting, use the following command:
sudo sysctl -w vm.swappiness=10 sudo sysctl -w vm.vfs_cache_pressure=50
here is cat / proc / meminfo Simultaneous output of both servers:
Note the healthy server on the left, ‘SwapCached’ is low even with the vm.vfs_cache_pressure setting of 50.
update: Here are some screenshots of servers @ 6GB of RAM, then 1GB and then finally 6GB:
And finally the outputs of post-upgrade free and top:
Kernel buffer on VMs – Notice kernel buffer on VPS is zero. Usually this should not happen. VirtIO is being used as a storage bus driver/interface and much of the disk cache handling is delegated to the KVM hypervisor. Buffers are for disk operations. So we will see the number increase when the disk write cache needs to be written. You do I/O a . can see on Stacklinux VPS (See also: Your Web Host Doesn’t Want You to Read This: Benchmark Your VPS)
Originally Posted: July 25, 2017 | Last Updated: July 21, 2021