Chapter 5. Memory Management

Table of Contents

The Buffer Cache
The Directory Cache
Paging and swapping
Introduction
Swap Space
Swapping
Paging
The working sets
Implementation of swapping and paging in different systems
Virtual memory

Figure 5.1. Kernel Memory, table and buffer cache allocations

Kernel Memory, table and buffer cache allocations

The Buffer Cache

All data accessed from files on the system that are performed through the use of read() and write() system calls, pass through the filesystem buffer cache.

This buffer cache greatly speeds up disk access, often the most critical performance bottleneck on the system, so that recently accessed files can be served quickly from this RAM based cache, instead of reading then from or writing them to the physical hard disk.

Traditionally, Unix up to and including SVR3 keeps the buffer cache in kernel memory. This means that on SVR3 based Unix systems like SCO Unix and SCO Open Server V changing the size of the buffer cache requires configuring the kernel to use a new size and then rebooting the system so that the kernel can allocate this and thereby change the size of kernel memory appropriately on initialisation.

Linux is renowned for its fast disk access and this is mainly because of its efficient implementation of the buffer cache. The buffer cache rose and shrinks dynamically in user memory as required. (Actually, kernel memory grows dynamically into user memory to allow this - the buffer cache is still considered part of kernel memory.)

As files are accessed on disk the buffer cache grows in user memory as much as it can. Remember that user processes live in user memory. The buffer cache never interferes with these and as the number and size of processes grow and shrink in memory, the buffer cache automatically expands and shrinks around them.

You can monitor memory usage with the free(1) command. The size of the file system buffer cache is displayed in the "cached" column, not the "buffers" column as you might expect. Other caches (like the directory cache) are reflected in the "buffers" column. It is important to understand this information. What you need to know to keep track of memory usage is how much user memory is being consumed by applications.

The amount of free memory indicated by the free command includes the current size of the buffer cache in its calculation. This is misleading, as the amount of free memory indicated will often be very low, as the buffer cache soon fills most of user memory. Don't' panic. Applications are probably not crowding your RAM; it is merely the buffer cache that is taking up all available space. The buffer cache counts as memory space available for application use (remembering that it will be shrunk as required), so subtract the size of the buffer cache to see the real amount of free memory available for application use. Therefore:

Available user memory = total memory - (used application memory + buffer cache)

riaan@linux:~> free
             total       used       free     shared    buffers     cached
Mem:        498572     493484       5088          0      50960     237436
-/+ buffers/cache:     205088     293484
Swap:       706852          8     706844
riaan@linux:~>
        
[Tip] Tip

The word root

The word root often confuses People, the word root can be used to mean 4 different things

  1. there is a user account on the system named "root" this is the default super user, or administrator account and has got full and unrestricted access to the system

  2. the /root directory is the root user accounts default HOME directory on Linux

  3. every filesystems top level directory is called that filesystems root directory (whether floppy CDROM or hard disk filesystem)

  4. the root directory of the system (superstructure root) , also denoted by a forward slash ("/")