Pages

Analyzing Memory Usage in linux



Lack of free memory can either be a symptom of a bigger problem or nothing to worry about at all. Different operating systems handle memory in different ways, so it is important to start with a basic understanding of how memory is handled by the Linux kernel.

Why don't I have more free memory?

The example shown below list the contents of /proc/meminfo


Lets see the important fields relevant to know the basics of memory usage. The Linux kernel attempts to optimize I/O performance by copying what is on the disk into memory for faster access. The amount of memory used by the cache is listed in /proc/meminfo (noted above). Cached memory can be freed quickly if memory is needed for other reasons. However, there are two types of cached pages, and the amount of cached memory that can be evicted depends on how much of the cache is considered dirty.

Dirty cached pages are those that contain changes that the system still needs to write to disk. Whenever the system needs to reclaim memory, it can evict clean cached pages, but dirty cached pages must first be copied back to disk.

Processing can become less efficient if you have a lot of write-heavy operations and your dirty cache is large. There are several tunables you can adjust to reduce the amount of data cached by the Linux kernel. The most effective in this case is dirty_background_ratio, which contains, as a percentage of total system memory, the number of pages at which the pdflush background writeback daemon will start writing out dirty data. The default value is 10 percent.


How much memory is the Kernel taking up ?

The memory used by the linux kernel can be found by adding three of the /proc/meminfo values:slab,dirty, and buffers.

The buffers value reflects the amount of data read off the disk that is not part of a file, which includes data structure information that points to actual files (for example, ext4 inode information).
Most of the memory used by the Linux kernel is listed under slab. When the kernel allocates memory out of the slab cache, it is labeled, and the purpose for which the kernel allocates the memory is recorded in /proc/slabinfo.

Why Is the Kernel Using Swap When There Are Cached Pages Available?

When the kernel swaps out a page, that page stays swapped out until it is used again. This means that if there is a sudden spike in memory usage and pages get swapped out, those pages will not be swapped back in as soon as memory becomes free. The pages will not return until the application to which the pages belong tries to access them.

It should also be noted that Linux tends to favor clearing out the pages used least frequently, regardless of whether they are cached pages that need to be cleared or normal pages that need to be swapped. It might make more sense according to the kernel's heuristics to swap out a page rather than to free the cache. There is some bias in which action the kernel will prefer, though, and how much is tunable by /proc/sys/vm/swappiness.

No comments:

Post a Comment