The `free` command looks simple, but there's actually quite a bit more to it than meets the eye. Today, let's sit down and discuss how to use this little tool to clearly see the memory usage of your lightweight cloud server. Let's start with the most basic operation. The simplest way to find out how much memory is being used is to type this line:
free -h
Adding the `-h` parameter will automatically convert the units to GB or MB, making it much easier to read. The result will look something like this:
total used free shared buff/cache available
Mem: 7.6G 5.2G 1.1G 123M 1.3G 1.9G
Swap: 2.0G 0.0B 2.0G
At first glance, you might think, "Oh no, 5.2G is used, only 1.1G is free, we need to expand!" Hold on, there's a common misunderstanding here.
Look at the middle column called `buff/cache`, it takes up 1.3G. What is this? These are some "tricks" the Linux system does for you. It caches read files and data, hoping to speed things up the next time you use them. Strictly speaking, this memory is occupied, but it can be released for regular programs at any time. So what you really need to care about is the last column, `available`, which tells you how much memory is immediately available for new applications. In the example above, `available` is 1.9GB, which is actually quite healthy.
Now, some people might ask, what if I want to see how much memory a program is actually using, not just the cached data? Older versions of `free` had a line called `-/+ buffers/cache`, which was used for this purpose. While newer versions have simplified the display, the principle is the same. Just remember, `available` is your "true friend," and the numbers in the `free` column are just for reference.
Speaking of which, we need to mention swap, the swap partition. Many people get nervous when they see a lot of swap usage, but there's no need to. If the system only occasionally moves some inactive data to swap to free up physical memory for more active applications, this is normal operation. However, if you notice that `used` swap space keeps increasing while `available` physical memory keeps decreasing, be careful. This might indicate that physical memory is actually insufficient, and the system is frequently swapping memory in and out, which will significantly impact performance.
How can you use `free` more conveniently? I usually use this combination:
free -hs 5
This command refreshes every 5 seconds, and the units are user-friendly. You can run a business application with it running to observe memory trends. For more detailed data, such as the total amount of memory and swap used, you can add the `-t` parameter.
Another small detail: If you're using a cloud lightweight server, especially a container environment, be mindful. Sometimes, `free` data within a container might not be accurate because it shares the host machine's kernel, and cgroup limitations may not be directly reflected in the `free` output. In this case, it's best to use it in conjunction with tools like `docker stats` or `kubectl top`.
When do you really need to worry about memory usage? Here are a few warning signs: First, available memory consistently falls below 10% of total memory; second, `vmstat` shows consistently large values in the `si` and `so` columns (representing the amount of data swapped in and out of swap, respectively); more seriously, check the system logs with the `dmesg` command. If you see "Out of Memory," then memory is completely exhausted, and the system has started killing processes.
If it gets to that point, don't panic. Add memory where needed and optimize applications as required. The `free` command is essentially there to help you detect problems early, so you don't wait until your website becomes inaccessible to realize the issue.
In short, when monitoring memory usage, don't just focus on `used` and `free`; pay close attention to changes in `available` and `swap`. Lightweight cloud servers, if you treat them carefully, won't cause you trouble.
EN
CN