Understanding Linux Memory Usage: Free, Cached, Buffered, and Hardware

Tue, Jan 21, 2014

Tweet this!

Photo Credit:Edmund Tse

Photo Credit: Edmund Tse

One of the key components of system resource monitoring is memory usage. Knowing how much RAM is utilized helps you judge whether you have too many programs open, or if you need an upgrade. Linux provides several ways of obtaining this information, but there are some details you should be aware of when you interpret the numbers.

Monitoring Memory Usage: Tools and Utilities

There are several ways to monitor memory usage in Linux. Utilities are available both via GUI and the command line; perhaps the most well-known GUI resources monitor is the one included with Gnome, appropriately named “gnome-system-monitor”. In Gnome, you can find this application under the “System” menu, in the “Administration” section, labeled “System Monitor”. This application will show you statistics on more than just memory usage; it gives you information on network, CPU, processes, and more.

If you take a look at the memory graph, you’ll see that there are two lines: Memory and Swap. The memory line is your physical RAM; RAM is incredibly fast, and whenever possible the system will almost always use RAM before it goes to swap, which is a part of your hard disk that is being utilized as memory. Hard disks (even solid state drives) are much, much slower than RAM, and therefore the system will avoid using swap unless it runs low on RAM. When someone says their machine is “swapping”, it means that they have run out of physical memory, and their computer has been forced to rely on swap, making it incredibly slow. This is one reason why adding memory to a computer can often provide a significant speed improvement.

Note: If you’re interested in learning more about swap, I’ve answered both “what is swap used for?” as well as how do I manage swap devices? in separate posts.

Showing memory statistics on the command line

On the command line, the free command is used to show the system’s current memory usage. Here is an example of the free command:

pi@raspberrypi ~ $ free
             total       used       free     shared    buffers     cached
Mem:        448768     181752     267016          0      13424      80036
-/+ buffers/cache:      88292     360476
Swap:       102396          0     102396

For a more readable version, free -m will show memory in value of megabytes (similarly, -k works for kilobytes and -g works for gigabytes):

pi@raspberrypi ~ $ free -m 
             total       used       free     shared    buffers     cached
Mem:           438        177        260          0         13         78
-/+ buffers/cache:         86        352
Swap:           99          0         99

You’ll notice that, like the Gnome System Monitor, we have information on both memory and swap; but, there are several numbers given for each item. Understanding these additional numbers is key to understanding how Linux uses memory. It’s also important if you’re considering adding memory to your computer; knowing what these numbers mean will help you gauge the performance improvement you can expect from adding RAM.

Shared, Buffered, and Cached Memory

Linux doesn’t just use RAM for running programs; it also uses RAM to speed up hard disk access. This is known as “caching,” and it greatly improves the performance of your computer. The more memory available and used for caching, the better your overall system performance. This is because hard disks are incredibly slow, and anything that can be stored in RAM instead will be accessed much faster. The free command tells us how much memory is being used by the system for caching in the “buffers” and “cached” fields.

Buffers is the amount of memory that’s being used to keep filesystem metadata in RAM. This data is frequently accessed, and so keeping it in memory results in huge performance benefits.

Cached is the amount of memory that’s being used to keep copies of actual files in RAM. When there are files that are constantly being written or read, having these in memory reduces the amount of times you need to operate on the physical hard disk; as a result, you can greatly improve performance by using memory for cache.

Both of these values are determined dynamically by the Linux kernel, which generally does a good job at figuring out how much RAM should be used for each. But it’s important to note that if a program requires additional memory, the kernel will free up the memory being used for caching file data (“cached” memory) in order to store the program’s data. This is why the free command shows a the -/+ buffers/cache line; it tells you how much memory can be freed up for programs, if needed. Therefore, if you want to know how much memory you actually have free for programs, you’ll want to use the value for “free” in the -/+ buffers/cache line.

Checking memory hardware without opening the case

Finally, sometimes you want to know how what size memory modules a machine has installed, or its upgrade capability, without physically opening the case. In Linux, this information is easily provided by the dmidecode command. dmidecode requires root, and has a rather lengthy output, so you’ll probably want to execute it as such:

sudo dmidecode | less

Scroll down or search for “Memory Device“, which is an entry for an installed DIMM. These entries look like this:

Handle 0x0029, DMI type 17, 27 bytes
Memory Device
Array Handle: 0x0025
Error Information Handle: Not Provided
Total Width: Unknown
Data Width: Unknown
Size: No Module Installed
Form Factor: DIMM
Set: None
Locator: DIMM1
Bank Locator: BANK1
Type Detail: None
Speed: Unknown
Manufacturer: Manufacturer1
Serial Number: SerNum1
Asset Tag: AssetTagNum1
Part Number: PartNum1

Here, you can see that the first DIMM in the first memory bank has no memory installed, where the following shows that DIMM zero in bank zero has a 1GB module installed (remember, computers tend to count from zero rather than one):

Handle 0x0027, DMI type 17, 27 bytes
Memory Device
Array Handle: 0x0025
Error Information Handle: Not Provided
Total Width: 64 bits
Data Width: 72 bits
Size: 1024 MB
Form Factor: DIMM
Set: None
Locator: DIMM0
Bank Locator: BANK0
Type: DDR
Type Detail: Synchronous
Speed: 333 MHz (3.0 ns)
Manufacturer: Manufacturer0
Serial Number: SerNum0
Asset Tag: AssetTagNum0
Part Number: PartNum0

Finally, the Physical Memory Array section will give you the maximum amount of memory that your motherboard can support, as well as the number of physical slots (populated or not) in the system:

Handle 0x0025, DMI type 16, 15 bytes
Physical Memory Array
Location: System Board Or Motherboard
Use: System Memory
Error Correction Type: None
Maximum Capacity: 8 GB
Error Information Handle: Not Provided
Number Of Devices: 2
Taking action on memory upgrades

By using and understanding the information provided by free and dmidecode, you can determine whether or not you need a memory upgrade, and (if you do need an upgrade) what kind of memory you need to purchase for your system.

In a later article, I’ll show you how to determine what programs are consuming the most RAM, and what you can do to improve the efficiency of the memory you already have installed.

I’d love to know whether or not memory is an issue on your system; on older machines, there’s usually not enough memory to go around, but newer boxes tend to have more than enough RAM for most applications. How much memory do you have on your system? Are you running low on RAM, or do you have more than enough? Leave a comment below and let everyone know!

Like this post?

5 Responses to “Understanding Linux Memory Usage: Free, Cached, Buffered, and Hardware”

  1. The challenge would be to create a program that could run in the background and advise the user of their memory usage and advise them should some remedial action be required.

  2. burningman404 says:

    I run gentoo with lxde dekstop and when using chromium run at 80 mb of memory.

  3. RoseHosting says:

    RAM Memory is cheap these days and buying extra RAM is usually worth it for the performance boost.

  4. Helena Bolan says:

    Well with Windows, it demands more RAM than what I have in my system just for a basic minimistic fresh install, it wants more than 1GB is is a piss take of MS to be honest.

    With Linux this is completely different you see… atm my system is only using 52MB of RAM. I am playing music and web browsing as well as using a VPN and Xorg.

    Altogether it just shows how ridiculous the IT industry can really be when it comes to trashy, heavy, hoggish software development. Does a computer really need 4-8GB of RAM? Well in most cases it ACTUALLY needs no where near! mainly 3D renderation and sometimes virtualization require masses of RAM, nowadays computers are shipped with RAM lots of RAM that isn’t really needed, it only is there because of how heavy Windows is.

    I am still using the i386 and i486 architectures for small tasks and I still find em to work flawlessly. I run the latest version of OpenBSD on both architectures.


  1. Links 22/1/2014: Games | Techrights - 22. Jan, 2014

    […] Understanding Linux Memory Usage: Free, Cached, Buffered, and Hardware […]

Leave a Reply

Weekly Poll

What's the best Linux distribution for desktops?

View Results

Loading ... Loading ...

Related Posts

Search TechThrob