What is cache in "free -m" output and why is memory utilization high for cache?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux (All Releases)
  • JBoss Enterprise Application Platform (EAP) - All versions

Issue

  • What is cache in free -m output and why is memory utilization high for cache?
  • How to check memory capacity required for Heap to create new server instance of JBoss by analyzing the free command output in JBoss, if there is already one instance of JBoss running?
  • Why are large amounts of memory used by cache?
  • Physical memory fully utilized frequently in cache

Resolution

What is cache in free -m?
It shows the amount of RAM which is currently used by the page cache. The page cache is a copy in memory of data which was read from or written to disk. In simple terms we can say the page cache is a copy of disk data available in memory.

What is the advantage of having the cache mechanism?
Kernel read and write operations operate on main memory. Whenever any read or write operation is performed, the kernel first needs to copy the required data into memory:

Read operation:

  • go to disk and search for data
  • write the data from disk into memory
  • perform read operation

Write Operation:

  • go to disk and search for data
  • write the data from disk into memory
  • perform write operation
  • copy the modified data back to disk

Summary:

  • Accessing disks is very slow compared to accessing memory
  • By using cache, fetching and writing data to disk for every read and wrote operation can be avoided
  • Periodically, modified ("dirty") data in the page cache is written to disk
  • This tremendously increases processing speed and system performance

The example below demonstrates how cache improves overall performance :

1. Append some data onto a new file XYZ:

# cat > XYZ
  hi how r u?
  ^C

2. Now clear the page cache with following command NOTE: This command is used only for the sake of explanation. It is not recommended to use this command if the system performs high I/O operations. Do not use this command in a production environment.

# sync
# echo 3 > /proc/sys/vm/drop_caches

3. Now check the time required to read the newly created file for the first time after creation. It shows 115 milliseconds.

# time cat XYZ
  hi how r u?
    
  real     0m0.115s
  user     0m0.001s
   sys     0m0.002s

4. The "XYZ" file should now be present in cache after Step-3, Now check the time required to read file again. It is only 2 milliseconds.

# time cat XYZ
  hi how r u?
    
  real     0m0.002s
  user     0m0.000s
   sys     0m0.001s

5. The access time was reduced by a factor of more than 50. So whenever there is free memory, the kernel will always try to utilize it to save the required files as cache.

What happens if there is no free RAM left and a new process needs free RAM?
When a new process requires free pages on RAM, at that time the kernel will check if there are any pages in the cache and accordingly the kernel will reclaim free pages by pushing back (syncing) the files from cache to the local disk and free up memory for new processes.

Detailed explanation about the "free -m" command output :-

# free -m
              total        used        free      shared  buff/cache   available
Mem:          15537        5432        2404         821        7700        8733  /* available RAM is considered as free memory in laymen term */
Swap:          7871           0        7871
  • In the above example, the total memory is 15537 MiB, out of which 5432 MiB is used and 2404 MiB is free.
    The used value is derived by following formula: (calculated as total - free - buffers - cache)

  • cache and buffers are considered as used memory technically, but they can be reclaimed by kernel whenever any demanding process(es) need(s) free memory pages.

  • For actual calculation of memory which can be made available upon demand to other process(es), one should look at available field which is the exact representation of free pages + reclaimable pages.
    As per above example, 8733 MiB is considered as free memory in laymen term.

  • As per man free :
    available:
    Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use.

Why is so much of memory used by cache?

  • Per the Linux virtual memory manager, this behavior is normal. To understand why cache memory can become so high, and why this is not an issue, one must understand how I/O works on Linux. When a user process reads or writes a file, it is actually modifying a copy of that file in main memory. The kernel creates that copy from the disk and writes back changes to it when necessary. The memory taken up by these copies is called cached memory.

  • Cached memory will be consumed whenever a user process initiates a read or write. The kernel will look for a copy of the part of the file the user is acting on, and, if no such copy exists, it will allocate one new page of cache memory and fill it with the appropriate contents read out from the disk. If the user only reads the file, this page will be marked as a "clean" cache page. However, as soon as the user writes the file, the page will be marked "dirty." A kernel thread which appears in ps called pdflush will wake up periodically and copy all of the pages marked dirty back to the disk, then mark them as clean again. Note that the page is only re-marked as clean, the page is not freed when it is written back, but is kept around in case something wants to do further IO on the part of the file it caches.

  • Cache pages are only freed again when the kernel needs memory for something else. Since having the cache page already read from disk can speed up I/O, and since getting rid of a clean cache page is just as easy as allocating a free page, and since a free page is doing nothing to help the system perform and function, there is no reason to turn a cache page into a free page. If memory fills up with cache pages, the next time the kernel needs memory it will simply evict the least recently used clean cache pages and re-purpose them.

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.