For novice Linux system administrators who don’t have extensive experience running their site from a Linux box, one of the most commonly asked questions is: “Why is Linux using all my RAM?”.
Naturally, server admins want to get the biggest bang for their buck with their server, so they are concerned that its resources, particularly its memory, are being used efficiently. They find out about the “free” command, run it, and are upset to discover that they have almost no free RAM: it’s all being gobbled up by the system.
In fact, this is a confusion resulting from a difference in the way that operating systems report their available and used memory. Windows users are used to seeing plenty of free memory reported by that operating system, and they apply the same concepts to Linux. However, Linux reports its memory usage somewhat differently which can lead to unnecessary concern on the part of sysadmins that don’t have a grasp of Unix memory management.
Why Does “free” Show No Free RAM?
The first thing to understand is that truly free memory is a wasted resource. Why would you want RAM sitting there doing nothing most of the time if there is something useful it could be doing?
In this case, the “something useful” is disk caching. The Linux kernel loads and keeps data in memory that it thinks it’s going to need fast access to. It’s much quicker to have frequently used data already waiting in memory than to pull it off the disk repeatedly. Linux loads up the free memory with caches and buffers; doing so considerably speeds up the system.
The confusion arises because different operating systems define “free” differently. For Linux, free means: “not being used for applications or caching”, whereas in Microsoft Windows it means: “not being used for applications”.
But Doesn’t That Mean Less Memory Is Available For My Applications?
No. The memory being used for disk caching is always available immediately for use by applications. If applications need memory that is being used for disk caching, the kernel reallocates that memory to the application. There is no downside to using disk caching: it makes the system run more quickly and it doesn’t take memory away from applications.
If you want to check this out, you can use the “free” command again, but focus on the line that begins “+/- buffers/cache”, which is the equivalent of the Windows way of displaying free memory i.e. memory that is not being used for application but is being used for caching. This is the memory that’s actually free for your applications to use should they need it.
What other tips do you have for folks new to Linux server administration? Any resources you found particularly helpful?