Free vs. Available Memory in Linux

Originally published at: Free vs. Available Memory in Linux

At times, we will need to know precisely how our Linux systems use memory. This article will examine how to use the free command-line utility to view memory usage on a Linux system. In doing so, we will clearly define the difference between free vs. available memory on Linux systems. Table of Contents Free vs.…

For most monitoring tools, the free memory is what is being used without considering cache/buffers. For example a server configured with 64GB and having free memory to be 5GB and available to be 30GB will be flagged as being under memory pressure. How do you think this gap can be resolved ?

1 Like

Having 5GB not in use and a healthy 30GB of cache and buffers can be further improved by tweaking the kernels cache pressure.

I’m on mobile this morning, but if I didn’t include how to tweak cache pressure to hold more data rather than swap or be removed I will edit this reply with a link to another article which details how to do so.

Oh and welcome to the community!

Edit: To make full use of memory on a system with more than adequate RAM installed. See the this article:

Quote:

Consider adjusting your server’s cache pressure and tendency to swap (vm.swappiness) by following the guide below, which is from the previous article: Linux server needs a RAM upgrade? Check with top, free, vmstat, and sar:

vfs_cache_pressure – Controls the kernel’s tendency to reclaim the memory, which is used for caching of directory and inode objects. (default = 100, recommend value 50 to 200)

swappiness – This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase aggressiveness; lower values decrease the amount of swap. (default = 60, recommended values between 1 and 60) Remove your swap for 0 value, but usually not recommended in most cases.

Specific to your case:

On a healthy server with lots of available memory, use the following:

vm.swappiness=10
vm.vfs_cache_pressure=50

This will decrease the cache pressure [uses MORE memory for cache]. Since caching is good for performance, we want to keep cached data in memory longer. Since the cache will grow larger, we still want to reduce swapping to not cause increased swap I/O. [still promote opportunistic swapping]

To check current values using these commands use:

sudo cat /proc/sys/vm/swappiness
sudo cat /proc/sys/vm/vfs_cache_pressure

To enable these settings temporarily without rebooting, use the following commands:

sudo sysctl -w vm.swappiness=10
sudo sysctl -w vm.vfs_cache_pressure=50

To make these settings permanent:
Add or edit these lines in /etc/sysctl.conf file:

vm.swappiness=10 
vm.vfs_cache_pressure=50
1 Like

please will the recommended setting help in below scenario ?

1 Like

You cropped out if the command used was free or free -m.

It that’s 377G total, you don’t need to do anything. Looks healthy to me. Lots of “available memory”. Are you having issues?

It would not hurt to add, say 2G to 4Gb of swap and then monitor if it’s used.

Thanks Bro.
The command used is “free -h”. No issue but monitoring team complaining because they are checking the free column. swap was disabled as a requirement for deployment

They should be looking at the available column. Free = wasted memory. So there’s no issue with the server. Looks fine.

Free -h would return Gi or Mi at the end of figures. So it’s not free -h (human readable) more likely free -m

-h, --human

Show all output fields automatically scaled to
shortest three digit unit and display the units of
print out.

Following units are used.

            B = bytes
            Ki = kibibyte
            Mi = mebibyte
            Gi = gibibyte
            Ti = tebibyte
            Pi = pebibyte
1 Like

So sorry, i meant to type “free -g”.
Thanks for the feedback

1 Like

My pleasure. Thanks for the discussion!

New member here. Thanks for the post, I learned new things. I’ve been confused since I started using btop on my laptop, I don’t understand the difference between free and available.

btop on laptop

If I understood you correctly, you’re saying that the difference between Free and Available is that Free is memory that has not been used in the current session. Where as available is memory that is being used as cache and buffers but can be freed at will for new or existing processes to use. Is this correct?

Here’s a list of further questions I have:

  1. If 4.52 GB is available, doesn’t that mean it is free to use? So then why is there a different field name Free which is 1.14GB?
  2. If caches and buffers are using the available memory then how can it be available?
  3. If this available memory (4.52 GiB) is taken from the process that owns it without closing the owning process, and given to a new or existing process then wouldn’t that crash the process that was using it?
  4. If the memory is Free and unused then why is it wasted instead of being memory that can easily be used by a new or existing process since no other process is hoarding it?

Hi @randUser. Welcome to the community! :handshake:

Yes.

Also, great questions! Here’s a brief explanation:

  1. Free memory is what’s completely unused right now. Available memory includes both free memory and memory currently used by cache and buffers, which can be freed up instantly for new processes.
  2. Free shows unused memory, while Available gives a more accurate picture of the available memory your system can use if needed.
  3. Memory used for cache and buffers is low-priority. The system can quickly free it up for new processes without affecting running applications which are high-priority.
  4. Free memory is unused and just sits idle. Linux optimizes performance by using as much memory as possible for caching. If a large portion of your memory is always free, you might have more RAM than needed. Ideally, your system should use around 80% or more of available memory for all your tasks, rather than having a significant amount, like 16 GB out of 32 GB, sitting unused (Free).

Hope this helps!

Thanks for the warm welcome.

  1. Free memory is what’s completely unused right now. Available memory includes both free memory and memory currently used by cache and buffers, which can be freed up instantly for new processes.

I think I’ve seen btop show Free memory being greater than Available memory. How can this be when Available memory is the sum of free memory and memory used by caches and buffers?

Also when I hear buffers I think of buffers in vim which are used to open files in multiple vim tabs. If the system frees up buffers from vim, then why doesn’t an unsaved file close and loose its data?

  1. Memory used for cache and buffers is low-priority. The system can quickly free it up for new processes without affecting running applications which are high-priority.

When allocating or reallocating memory, new and old processes take precedence over caches and buffers, got it.

I noticed you are using the singular “cache”, I assume cache is simply temporary storage which is discarded when a process exits. Correct me if I’m wrong, shouldn’t we be talking about caches since there are many, one for each process I think?

You’re answers are very helpful. I’m glad I came across this question.