Tuesday, December 21, 2010
Memory Usage - DistroWatch.com
Questions and Answers (by Jesse Smith)
One of our readers asked about programs which display memory usage, what to use and what the different types of memory (shared, virtual, swapped, cached) mean. Furthermore, what fields are important to look at when comparing different systems?
First let's talk about programs to use when monitoring memory usage. When in doubt, I generally fall back to basics and use "top". It runs everywhere and shows you most of the information you should need, both in regards to memory usage and CPU usage. If you're just concerned with how much memory is being used by the system and uninterested in CPU usage then "free" is also a good command to run as it'll show you usage information for shared memory, cache and swap. When looking at GUI applications, I like KDE's System Monitor. The default layout looks a lot like top's, but it's easy to configure System Monitor with different views and with different filters. My favourite feature of System Monitor is its method of displaying CPU, memory and network usage in a graph so you can quickly tell if your resource usage is spiking or holding steady. For non-KDE people, there is a GNOME application, which also carries the name System Monitor, and it shows the same information in approximately the same manner.
Of course having access to all of this information isn't very useful without knowing what the data means, so let's begin with shared memory. As the name implies, shared memory is a section of your computer's memory which is can be accessed (or shared) by multiple processes. Sometimes programs will use a common pool of memory as a fast way to communicate or work on a common task. Shared memory can also refer to items in memory that are common across multiple processes. Let's say, as an example, that you have a text editor open. That text editor requires certain libraries be loaded into the system's memory. When you then open another program which also makes use of the same library there really isn't any reason to have two copies of the same library in memory. In those cases the operating system can keep just one copy in memory and let both programs access it. In short, when you see shared memory it means multiple processes are making use of the same section of memory.
Virtual memory is, in itself, a big topic and it can get a bit confusing. In the context of what you see when using the top command, the amount of virtual memory a process is using really means the total amount of memory that task is consuming. It includes the size of the program's executable, libraries being used and any data that task is storing in memory.
Swapped memory is, perhaps, a bit easier to understand. Modern operating systems often need access to more memory than they physically have. When Linux runs out of physical RAM it needs to store data somewhere else. That somewhere else is typically a swap partition. This is basically a closet in which data can be temporarily placed until it is needed. Swap space is useful because it means we can effectively use 2 GB of memory, even if the computer in front of us only has 1 GB of RAM. That extra GB is stored on the hard drive as swapped memory. The down side to using swap space is that accessing the hard drive is slower than accessing RAM and so making heavy use of swap space can make the computer less responsive...
... Which brings us to cached memory. Since it slows us down when we access the computer's hard drive, it is handy to keep as much data as possible in RAM. Cache refers to data in memory which is not currently being actively used, but is instead being held for quick access. You can see cache at work if you open a large program, like OpenOffice, and make note of how long it takes to load. Then close OpenOffice and immediately open it again. The second time OpenOffice loads it should come up faster because the OS left the program in our cache. Some distributions (such as SliTaz) make use of cache by loading the entire OS into memory at boot time, making loading applications almost instant.
When comparing different systems, I generally look at the amount of memory the system uses, minus cache. Since cache isn't being actively used, it's not really impacting what we can load into memory later. An easy way to get this information is to run "free" and look at the second line. The number under the "used" column of the second line will show you how much RAM is being used (in kilobytes), ignoring cache.