You are on page 1of 9

C U S T O M E R (https://access.redhat.

com/)
P O R TA L

What is cache in "free -m" output and why is memory utilization high for
cache?
$ SOLUTION VERIFIED - Updated March 15 2018 at 1:34 PM - English ()

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?
C U S T O M E R (https://access.redhat.com/)
Kernel read and write operations 
P O R TA L 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 C U S T O M E R (https://access.redhat.com/)
P O R TA L
# 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 C U S T O M E R (https://access.redhat.com/)
total P O Rused
TA L free shared buffers cached

Mem: 3753 3455 298 0 144 664
-/+ buffers/cache: 2645 1107 <<-- Actual Free RAM
Swap: 4095 75 4020

In the above example, the total memory is 3753 MB, out of which 3455 MB is in use and 298 MB is free.
cache and buffers is considered as used memory, but they can be dropped in favour of applications.
Out of 3455 MB of used RAM, some amount of RAM is used for cache and buffers. As discussed earlier memory from cache and
buffers can be reclaimed whenever there is no free RAM available for new processes. So cache and buffer can be considered as
free RAM. Therefore the actual size of free RAM can be calculated by the below formula:

Actual Free RAM = "free" + "buffers" + "cached"


= 298 + 144 + 664
= 1106 which is approximately equal to the 1107 in second row under "free" column in "free -m" output.

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
C U S T O M E R (https://access.redhat.com/)
from disk can speed
P O up
R TAI/O,

L 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.

Product(s) Red Hat JBoss Enterprise Application Platform (/taxonomy/products/red-hat-jboss-enterprise-application-platform)


Red Hat Enterprise Linux (/taxonomy/products/red-hat-enterprise-linux)

Component kernel (/components/kernel) Category Learn more (/category/learn-more)

Tags cache (/tags/cache) eap (/tags/eap) jboss (/tags/jboss) kernel (/tags/kernel) memory (/tags/memory) performance (/tags/performance)

rhel (/tags/rhel)

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.

People who viewed this solution also viewed

When JON reports free and used memory on a Linux platform resource it does not take into account cached and buffered
memory

Solution - Apr 24, 2012


C U S T O M E R (https://access.redhat.com/)
Amount of cached memory in free -m differs from amount in /proc/meminfo 
P O R TA L

Solution - Jun 15, 2015

Why does my system utilize swap space instead of freeing up cache and buffer memory?

Solution - Nov 30, 2016

9 Comments
27 January 2012 11:10 AM (https://access.redhat.com/solutions/67610#comment-338123)

Amit Jadhav (/user/11733)


(/user/11733)
RED HAT echo 3 | sudo tee /proc/sys/vm/drop_caches

PRO

552 Above command is for local user. For root user below command can also be used.
Points

echo 3 > /proc/sys/vm/drop_caches

≤ Reply (/Ajax_comments/Reply/67610/338123)

13 September 2016 7:30 AM (https://access.redhat.com/solutions/67610#comment-1097261)
C U S T O M E R (https://access.redhat.com/)
UT unix team (/user/5212783)
P O R TA L

(/user/5212783)
Not releasing memory from cache as per deman from application. And application is facing memory issue
NEWBIE

7 Points ≤ Reply (/Ajax_comments/Reply/67610/1097261)

18 December 2017 10:26 AM (https://access.redhat.com/solutions/67610#comment-1251281)

Paolo Villaflores (/user/20889001)


(/user/20889001)
My system is running low on memory and cache is consuming 95% of it, and is causing user processes to fail
NEWBIE

12 Points ≤ Reply (/Ajax_comments/Reply/67610/1251281)

30 May 2018 10:06 AM (https://access.redhat.com/solutions/67610#comment-1311411)


HS Habibullah Salimy (/user/10596133)
(/user/10596133)
Hi support, My production server is caching too much memory which caused system performance slow. cache is
NEWBIE consuming 90% of memory please provide any solution ?
11 Points
≤ Reply (/Ajax_comments/Reply/67610/1311411)

12 July 2018 7:17 AM (https://access.redhat.com/solutions/67610#comment-1326771)


KM Kumaran M (/user/8432573)
(/user/8432573)
Don't execute that command to clear cache memory on Prod servers. It will kill running processes on cache memory.
ACTIVE
CONTRIBUTOR
≤ Reply (/Ajax_comments/Reply/67610/1326771)
188 Points

22 October 2018 4:41 AM (https://access.redhat.com/solutions/67610#comment-1366311) 


Gour Ghosh (/user/17326441)
C U S T O M E R (https://access.redhat.com/)
GG My systemPisOrunning
R TA L

low on memory and cache is consuming 95% and we getting memory utilization alert.How to resolve
permanently fix this issue.
(/user/17326441)
NEWBIE

5 Points
≤ Reply (/Ajax_comments/Reply/67610/1366311)

27 November 2018 5:56 PM (https://access.redhat.com/solutions/67610#comment-1389331)


EC Eli Cantu (/user/12101133)
(/user/12101133)
This answer is consistently parroted. "Don't worry about your insane cache memory usage. Just let that OOM killer kill
NEWBIE everything off, and your application stack die. Nothing to worry about. Nothing here to see. Move along." I for one, have
11 Points had it, with this lousy answer parroted all over the internet. RHEL 6.x is garbage in this respect, and it's likely the kernels
are really to blame. Servers with 64GB of ram, and any time one checks with "free" command, cache is showing 40GB. Yet
this same server is OOMing all day long. If OOM Is consistently occurring, and SWAP is being hammered, then that tells
me one thing. Severe memory pressure. ...yet, there's linux allocating almost 70% of physical ram to ... you guessed it,
cache. This is nonsense.

≤ Reply (/Ajax_comments/Reply/67610/1389331)

25 September 2019 4:29 AM (https://access.redhat.com/solutions/67610#comment-1629221)


RV Rohit Verma (/user/27319911)
(/user/27319911)
yes there should be some tunebles to limit this.
COMMUNITY
MEMBER
≤ Reply (/Ajax_comments/Reply/67610/1629221)
22 Points

9 March 2019 4:59 PM (https://access.redhat.com/solutions/67610#comment-1468941)


PK Pankaj Kumar (/user/22388681)
(/user/22388681) 
NEWBIE
You can also clear cache using script.....
C U S T O M E R (https://access.redhat.com/)
5 Points https://curiousviral.com/how-to-clear-cache/
P O R TA L

≤ Reply (/Ajax_comments/Reply/67610/1468941)

All systems operational (https://status.redhat.com)

Privacy Statement (http://www.redhat.com/en/about/privacy-policy) Customer Portal Terms of Use (https://access.redhat.com/help/terms/)


All Policies and Guidelines (http://www.redhat.com/en/about/all-policies-guidelines)
Copyright © 2019 Red Hat, Inc.

You might also like