Memory is one of the primary price-determining factors when buying server hardware or virtual server hosting. It is therefore important to monitor and understand memory usage.
To get an overview of system memory usage, use the following:
$ free -m
The m switch tells free to display the numbers in terms of megabytes. Other possible options include b for bytes, k for kilobytes (default), and g for gigabytes.
The first row lists total, used, and free physical memory. Physical memory refers to the memory that is provided by physical memory sticks on your machine, as opposed to virtual memory which is the space that your computer borrows from a long-term storage medium to extend memory.
In comparison to physical memory, virtual memory is slower but cheaper on a per-GB basis. Modern systems have enough physical memory that they rarely need virtual memory unless a specific purpose (like software compilation or video rendering) favors it.
The second row (-/+ buffers/cache) lists total, used, and free physical memory that isn’t part of the buffers or cache. Linux, and other operating systems, cache parts of the long-term storage medium (hard disk) in memory for quick access, because physical memory is much faster than a hard drive.
In reality, an operating system tries to fill memory that isn’t being used by programs with cache and buffer data from the hard disk. The more cache and buffer space it uses, the faster a system is.
The second row is the most important one when examining system memory usage.
The third row lists statistics for swap space, which is another term for virtual memory.
The other important memory-monitoring program is top. Try it out:
$ top
You can press q or Ctrl + C at any point to quit from top.
By default, it updates every 3 seconds with statistics about system processes. Here is a brief explanation of the data in the columns:
- PID is the process ID. Each process has a unique one. You can kill a process based on its process id with the kill command.
- USER is the name of the user who is running the process. Typically, it is either your user name or root.
- PR is the priority of the process according to the system. A value of RT means top priority.
- NI refers to the Nice value of the process. Positive values mean less priority. Negative values mean greater priority.
- VIRT refers to the total amount of virtual memory that is being used by the process, but it includes shared libraries and pages that have been mapped but haven’t been used. This is not the most important statistic.
- RES refers to the residential memory of a process. It includes only the programs personal memory and only includes physical memory.
- SHR refers to the memory used by the shared libraries of this process. Shared libraries are kept in memory but can be used by more than one process.
- S refers to the processes’s state. The most common are S for sleeping (idle), R for running, and Z for zombie (not responding).
- %CPU refers to CPU utilization.
- %MEM refers to the percentage of total physical memory that is devoted to the processes’s residential memory.
You should focus primarily on the amount of residential memory (RES) that is listed by a process. Shared memory is secondary.
If a process is taking up an abnormal amount of memory, check to see if there are performance-tweaking options in their configuration. For common server processes like apache and mysql, there are.
Additionally, top lists sizes for physical and virtual memory utilization including the buffers and the cache.