You’ve probably heard the managers asking questions like; how much free memory does the system have? Why is the amount of free memory so low? Where did the entire RAM go?
This article will provide you the right answers that will make the managers and customers smile, and understand the Linux memory consumption concept.
The standard Linux utilities for memory consumption report are “free” and “top”. The “free” utility displays the amount of free and used RAM, and “top” utility also provides a per-process memory usage breakdown. These utilities may often report that the free amount of memory left in the system is rather lower than you would expect, especially after the system was up and running for a while. You may conclude that there are problems related to memory leaks, or that your available RAM is being used inefficiently or even that your system does not have enough RAM to run correctly. Whilst these issues could be real problems with your system, it doesn’t always have to be case.
Linux memory consumption concept
Linux memory consumption concept is all about efficiency. The system’s RAM is a resource that is meant to be used; 100% of it (if possible), all the time (if possible).
Linux utilizes unused RAM to cache data and filesystem meta-data from slower storage devices (Flash or disk) because fetching the information from the RAM is much quicker: There are no bottlenecks such as slow physical media, slow buses or device clocks, and not decompression is required.
Assuming there are no memory leaks, the reason that memory report tools report low amount of free memory is because the RAM is considered to be wasted if it isn’t used. This concept may require some time to digest, because conventional thinking may lead to the conclusion that an efficient system is a system with a lot of free memory. This is not entirely correct. In Linux case, the kernel tries to utilize the most of the RAM to improve the system performance. Keeping the cache means that if the kernel or a task needs the same data again, there’s a good chance it will still be in the fast cache in memory.
Getting the amount of free memory
As mentioned before, there are two standard utilities that report the amount of free memory. For RT Embedded projects, these utilities are provided by “Busybox”, and their output might be slightly different than standard distributions due to memory consumption restraints. Here’s an example for the output of “free” utility:
The numbers shown are in Kilo-Bytes, meaning that this system has total memory of about 120MB, 41MB is used and 77MB is free. The buffers column displays the amount of memory that was used for filesystem meta-data (such as the filesystem tree information, file location information, etc.). System with hard disks or flash drives may also have swap memory. This snapshot was taken from a system with a single flash without a disk, and therefore, the total swap memory is 0.
Here’s an example of the output from “top”, taken from Fedora distribution:
The output shows similar memory reports fields as the “free” utility as well as per-proces detailed information. For the purpose of this article, the following columns are relevant:
- VIRT: Virtual Image (KB). The total amount of virtual memory used by the task. It includes code, data and shared libraries as well as pages that have been swapped out.
- RES: Resident size (KB). The non-swapped physical memory a task has used.
- %MEM: Memory usage (RES). A task’s currently used share of available RAM.
The detailed output of “top” may interest you when you are debugging memory leaks. In this case, the leaky task Resident size (and %MEM) will rise over time. Otherwise, the output of “free” is sufficient.
Getting more information from the proc filesystem
The kernel provides a special proc file called meminfo, which displays the memory consumption figures in details. The meminfo file may differ between distributions, especially between Embedded Linux and Desktop Linux. Here’s a typical output of this proc file, from the same system that was used for the “free” snapshot:
The information fields which are available in meminfo proc file:
- MemTotal – Total amount of physical RAM, in kilobytes.
- MemFree – The amount of physical RAM, in kilobytes, left unused by the system.
- Buffers – The amount of physical RAM, in kilobytes, used for file buffers.
- Cached – The amount of physical RAM, in kilobytes, used as cache memory.
- Active – The total amount of buffer or page cache memory, in kilobytes, that is in active use. This is memory that has been recently used and is usually not reclaimed for other purposes.
- Inactive – The total amount of buffer or page cache memory, in kilobytes, that are free and available. This is memory that has not been recently used and can be reclaimed for other purposes.
- LowTotal and LowFree – The total and free amount of memory, in kilobytes, that is directly mapped into kernel space. The LowTotal value can vary based on the type of kernel used.
- Mapped – The total amount of memory, in kilobytes, which have been used to map devices, files, or libraries using the mmap command.
- Slab – The total amount of memory, in kilobytes, used by the kernel to cache data structures for its own use.
- Committed_AS – The total amount of memory, in kilobytes, estimated to complete the workload. This value represents the worst case scenario value, and also includes swap memory.
- PageTables – The total amount of memory, in kilobytes, dedicated to the lowest page table level.
- VMallocTotal – The total amount of memory, in kilobytes, of total allocated virtual address space.
- VMallocUsed – The total amount of memory, in kilobytes, of used virtual address space.
- VMallocChunk – The largest contiguous block of memory, in kilobytes, of available virtual address space.
Getting the absolute free memory number
Your manager wants to know how much free memory the system has. What he really wants to know actually is the minimum amount of memory that the system requires in order to function properly. Let’s call this number “The absolute free memory number”. Given this number and the amount of actual installed memory, the manager also wants to know how much RAM is left for the customers’ customizations, or whether a smaller RAM device can be used in order to reduce the hardware cost (or even both…).
The absolute free memory number is not only the value of MemFree, but the sum of the MemFree, Buffers and Cached fields. It means that the majority of the used memory by Buffers and Cached can be reclaimed in case a task will require it, while reducing the amount of buffers and cached. Using unused (clean) cache will not impact the system performance. However, degradation in the system performance will appear once starting to free used cache. In some point (usually in the last free 1MB, will be discussed soon) the degradation will be very noticeable and the system will become very slow. It happens because live tasks are swapped out of the RAM, and execution of commands requires flash/disk access and decompression. If even more RAM is required, the kernel will initiate the Out-Of-Memory Killer and start killing tasks that have the highest “bad” score (See the article about Linux processes for more details). In this stage, the system is not in a stable state and its functionality is not guaranteed. If your system reaches this state on a regular basis, you may either have a serious memory leak, or the actual physical RAM installed is insufficient.
Flush and invalidate system caches
It is possible to artificially increase the value of MemFree (and make your manager happy) by flushing and invalidating the clean (unused) cache and filesystem meta-data. This operation is non-destructive and it does not free the currently used caches (like running processes). Prior to this operation, the “sync” command needs to be run first in order to make sure all cached objects are synchronized.
- To free pagecache:
– echo 1 > /proc/sys/vm/drop_caches
- To free dentries and inodes:
– echo 2 > /proc/sys/vm/drop_caches
- To free pagecache, dentries and inodes (basically everything):
– echo 3 > /proc/sys/vm/drop_caches
Here is the output of the same meminfo file, before and after all the caches were freed:
|Memory status before dropping caches||Memory status after dropping caches|
We can see that we gained almost 8MB in of RAM in MemFree field (and also in the “free” report) just by dropping the caches, and that the amount of Buffers and Cached has been reduced.
Other kernel tunables regarding memory
The Kernel provides run-time tunables that can change its behavior in extreme cases:
- /proc/sys/vm/min_free_kbytes: Used to force the Linux VM to keep a minimum number of kilobytes free. This number’s value is around 1MB. The Kernel will start swapping out and killing processes in order to meet this requirement.
- /proc/sys/vm/overcommit_memory: Controls over-commit of system memory, possibly allowing processes to allocate (but not use) more memory than is actually available.
|Check out the ads, there could be something that may interest you there. The ads revenue helps me to pay for the domain and storage.|