Memory

Problem: Sometimes computers seem to be permanently low on memory in spite of few operations running on them. You may be looking for a computer with a large amount of free memory.

For example, top running on the computer which has 2GB of real memory and 5GB of swap shows:

Cpu(s): 48.7%us,  1.0%sy,  0.0%ni, 49.8%id,  0.0%wa,  0.2%hi,  0.3%si,  0.0%st
Mem:   2073976k total,  1993768k used,    80208k free,   204920k buffers
Swap:  5242872k total,       16k used,  5242856k free,   544852k cached

Summing all processed memory usage reveals that the computer does not have processes using anything like the 2GB of ram, so it seems that it must be something else is using the memory.

What is causing this discrepancy?

Answer: The term free isn't quite what is wanted. On most modern machines it literally means that the memory (or swap etc) isn't being used at all (for anything!).

The design of modern (unix) memory systems (and certainly all our Linux machines) means that typically very little memory will be shown as free since the system will use memory to cache files which have recently been accessed.

Since that memory would be available for programs if they need it it can really be added to the free space etc. Similarly there are other kinds of memory which are used by the system only because nothing else needs it, and will be given up if needed.

There is a command called free which displays the total amount of physical memory and swap space for the system.

Example output for the command free:

             total       used       free     shared    buffers     cached
Mem:       2073976    1995752      78224          0     204940     544852
-/+ buffers/cache:     435960    1638016
Swap:      5242872         16    5242856

the most interesting line here is probably the "-/+ buffers/cache" which shows the free+buffer-cache etc, which about 1.6GB of the 2GB that the machine has.

Note also that programs not running can be swapped (or paged) out of system memory so as long as the working-set of the total of running programs is under physical memory and the total usage of all programs is under physical-memory+swap there isn't a significant problem.

To display the memory amounts in MB you can use the -m option e.g.:

  free -m