One of the frustrations I deal with on nearly a day-to-day basis is people's misconceptions about how memory is allocated under Linux 2.4 and later.
Listing 1: Tools to report memory usage
* top
* free -m
* cat /proc/meminfo |
Let's take a typical example. The "free -m" output below is from a production system that has been running for 80 days.
Listing 2:
total used free shared buffers cached
Mem: 3956 3939 16 0 81 3504
-/+ buffers/cache: 353 3602
Swap: 16378 0 16378 |
This system has 4GB of physical memory and 16GB of swap. At first glance it looks like 99.57% of the memory is in use. Your first instinct would be to run "top", press capital "M" to sort by memory usage and find out what the hell is taking up so much memory. Doing so will yield no results, however--on this particular system top will show no process taking up more than around 100MB of memory. This can be confirmed with the "ps" command.
So where did all this memory go? The answer lies in the theory of memory management. In particular, the Linux memory management system believes in the following two principles:
1) No matter how much memory is in use, system performance will not be impacted until it is required to use swap space.
2) Unused physical memory is wasted.
Now we can tackle the first line of the "free -m" output.
Listing 3:
total used free shared buffers cached
Mem: 3956 3939 16 0 81 3504 |
The key lies in the last two columns: buffers and cached. The largest of these is cached. Whenever Linux accesses a file on a filesystem it stores the content of that file in memory. As long as the data doesn't change, subsequent requests for that file will be read from memory instead of physical disk. This vastly improves file access time.
Memory used in such a manner can be freed up almost instantly, thus should really be counted as free memory instead of used. The "-/+ buffers/cache" line of our "free -m" output reflects this.
Listing 4:
total used free shared buffers cached
Mem: 3956 3939 16 0 81 3504
-/+ buffers/cache: 353 3602 |
Actual used memory here is 353MB. Free memory is 3602MB (Don't add the original 16MB back to this--it's already accounted for). That means over 90% of this system's memory is free and available for use. Quite a bit different than only having 16MB of memory free.
The 3602MB value comes from adding the 3504MB of cache with the 81MB of buffers and 16MB of actual free memory. The 353MB of used memory comes from taking total memory and subtracting the "virtually free" 3602MB value. Note that due to rounding these values may be a bit off.
This most certainly comes in handy
Thanks. a lot..
Great article !!
Would be nice to see Direct X become more widely compatible with different operating systems, but i suspect its one of the keystones in the Microsoft foundations which keep people and game developers closely linked to the windows platform. I think the move pushing DX10 to be a vista-only solution was a bad move on Microsoft's part since vista seemed to be rushed out long before it was really ready for the market. Im really hoping to see some sort of XP integration in the future but thats an unlikely long shot.
Instead they make it Vista-only. Which, really, is the exact opposite of what they should have done.
Sadly, the Microsoft marketing genius we feared and respected in the late '90s is no more =/
RHCE