|Why does Linux use so much memory?|
|Written by Michael Shinn|
|Monday, 27 February 2012 13:08|
Note: This article is not about Atomic Secured Linux (ASL), it is about all modern Linux based systems. This characteristic of modern Linux based systems is universal to all modern Linux systems, not just systems running ASL.
We often get asked why a Linux based systemseems to be using so much memory. Even on huge systems with tons of memory, over time a Linux seems seems to use up all the memory available. People get worried that something is wrong, and that maybe that there is a bug in something. Most of the time, it turns out that a Linux system isn't really using as much memory as it may appear, and this article is targeted at explaining how memory works in Linux and what tools you can use to find out how much memory is actually being used.
Memory is almost infinitely faster than reading from a hard disk. This is true even for Solid State Disks, which are not as fast as RAM, plus the bus speed for RAM is several orders of magnitude big and faster for the systems memory, than for the systems storage devices. The bottom line: RAM is way way faster than hard drives. So modern high performance operating systems, such as Linux, take advantage of this awesome capability. When there is memory available, they will cache things into memory, such as information read from disk. Over time, you will see a Linux system (via some tools) report an almost 100% "memory utilization" regardless of how much memory is actually needed or in use by a process or how much memory is installed in the system. You add more memory, and it just gets "used up".
This can be a little strange to users that are new to Linux and come from operating systems that do not do this, or do not cache reads (such as older versions of Windows), however this is normal and its really good for the system as actually makes it much faster. Now before you think this meanes your memory is in fact being used up, this does not mean your processes are using up all the memory the system has, this is simply modern caching which all modern Linux kernels will do. How can this be? The memory is use right? No, its actually not, lets see why.
Why Linux does this
As said before, Hard drives are slow. Even the fastest hard drive is never even a tiny fraction as fast as RAM. Its like the difference between shooting a bullet, and throwing a bullet. Hard drives are slow. If hard drives were fast, we wouldnt need RAM, any, at all. So thats why we load programs into memory: hard drives are slow, memory is fast.
As memory has gotten cheaper, and performance demands have increased, operating system and application vendors have increased the use of RAM over reading from and writing to hard drives to improve performance. One way they do this is by caching "reads" from the hard drive (they cache other things too). In the case of caching reads, the operating system will temporarily store information it has been asked to read from the hard drive into memory, such as files, documents, graphics, even other programs. This makes it much faster the next time the operating system wants to "read" that information, it doesnt have to go back to the hard drive to get it, it can get it from memory. Which results in a massive performance increase. Applications can also cache things, for example, database servers (if configured to do so) will cache things they read from the database files they have stored on disk, again, because its wicked fast compared to reading from the hard drive. RAM is like magic, it makes things faster, so the goal with modern operating systems is to make maximum use of it. Nothing should be unused and because RAM is so fast if you arent using it youre wasting it.
On the surface this may seem bad, after the memory is in use right? No, its not in the traditional sense. The reason this isn't an issue is that caching is different from process utilization. Process utilization, or actual memory in use by processes, is very different from caching. Modern operating systems of course use memory for processes (actual use), and they also memory to temporarily "cache" things that they have accessed from disk. Most users are familiar with process utilization, this is what we typically think of memory in use and is what may cause them to think that a Linux based system has "used up all their memory". The reality is that the amount of memory in use by the processes is considerably less than the total amount of memory in use, because the bulk of it generally in use by the cache.
It is the later use of memory, caching, that typically "uses" up the memory on the system and creates this illusion that all memory is in use. This memory is actually not "in use", or prevented from being used by other processes on the system. Its really "free memory", just like users are used to. Because, the moment a process needs this memory the cached information is dropped and the memory is made available to the application. Its not paged out like an application, so its no big deal to drop it. So in reality, the system is using considerably less memory that it may appear to be using because its making temporary use of unused memory to cache things.
Remember, this is temporarily, its not in use like a normal program. Its really a very clever enhancement, and something all operating system vendors are implementing or have already implemented. As memory has continued to get cheaper, some products don't even have hard drives anymore, and just use RAM. Smart Phones for example, and even some modern tablets just use memory.
How to find out how much memory Linux is really using
So, to determine how much memory is actually being used by your processes as opposed to all memory being used by processes and caches, you will need to use a tool that can tell you how much memory is cached, and how much is actually being used by your programs. Once such tool is "free". The application "top" which is popular for looking at memory usage, is not a good tool for this as it will report the total memory is in use, and not the actually amount being used just by processes (it reports utilization plus cache).
Here is an example for how to use the "free" tool:
total used free shared buffers cached Mem: 12002 10199 1803 0 573 8185 -/+ buffers/cache: 1440 10562 Swap: 14015 0 14015
In this example the total amount of memory in use is 10GB, however 8GB of that is cached. So the system isn't using 10GB of memory. Of the 12GB of memory on the system, just slightly under 10GB is actually free (1.8 GB isnt used at all, and 8GB is cached).
This is very typical of a Linux based system, in that its really using much less memory that some tools report, because of this use ofcaching.
Remember that cached memory is always available to any program that needs it. So the memory is not "used", its just being temporarily taken advantage of because nothing else is using it to make the system faster. Linux will just make use of the memory available on the system to cache information until any program requests that memory space, at which time that cached data is dropped and the memory is made available to the application. Its a free lunch, the system gets faster, and there no adverse impact.
So enjoy your advanced OS and its clever use of memory!