Cached Memory Takes all the memory

I've switched to Ubuntu just a couple of months ago. Some days ago, I found out that the system is storing cache in memory which is occupying a lot of space and although I'm getting the benefit of cached memory, this is now making me in trouble.

ubuntu cached memory

As you can see in this picture, Ubuntu is taking more than 50% of my RAM. Additionally, I'm using 23.3% of 11.6 GiB which basically means that my system is occupying more than 75-80% of my memory...

Now, I've searched a lot of times how I can clear my cache and I've found that sync; echo 1 > /proc/sys/vm/drop_caches command with sudo privileges can clear it, but this command only worked in the beginning like when I used this command and saw my cached my it was heavily decreased. But now, when I see my cached memory after running this it remains same.

Can anyone help me out with this trouble?

P.S. I think if I try upgrading my RAM, that won't resolve this issue...


Solution 1:

if system is taking all the space in caching then i won't be able to use the ram

This is not true. Cached RAM does not determine RAM in use. You will be able to use the RAM, that's why it's called cached — it can be used and discarded as needed. In fact, it's just the opposite of your assumption. If you eliminate cached RAM, you won't be able to use what is cached.

Now, I've searched alot of times how i can clear my cache, i've found that sync; echo 1 > /proc/sys/vm/drop_caches command with sudo privileges can clear it, but this command only worked in the beginning like when i used this command and saw my cached my it was heavily decreased. But now, when i see my cached memory after running this it remains same.

You only need to manually clear the cache if you have swappiness turned off. Disabling swappiness is not recommended to increase performance as this actually reduces system performance.

Unlike Windows, Ubuntu doesn't crash under heavy RAM load because of the differences in memory management. Disabling these features will significantly reduce performance.

In summation, cached RAM is not RAM in use and does not max out your RAM or decrease system performance because cached RAM is automatically discarded as needed. Conversely, cached RAM increases performance as it is used when needed.

Solution 2:

Cached memory is in fact free memory.

It can (and will) be fed as free to any process that asks for memory, as soon as the other free memory is used up.

In the meantime, the kernel keeps track what information is cached in this otherwise free memory.

This information is not expected to be written somewhere by a time-consuming i/o. It is either already written or read and never changed in the first place. This information can be safely (and quickly) deleted from memory.

Solution 3:

Caching does not hurt you in any way!

The RAM is not being used by cache... it is being borrowed by cache! That means it will be returned for your use the instant you need it.

If your software requests a block of memory, the system goes "here you go" and gives the block. If there happened to be cache sitting there, the cached elements are dropped (i.e. they are no longer in cache and would have to be fetched from disk again if needed).

Smarter systems will make smart choices about which elements to drop out of cache to give you your memory.

Solution 4:

Open your terminal (Ctrl+Alt+T), and type the command: free -h. You will get the output like this:

              total        used        free      shared  buff/cache   available
Mem:           15Gi       2.0Gi        10Gi        33Mi       2.7Gi        13Gi
Swap:          31Gi          0B        31Gi

Here you have 15G of total RAM, 2G is used, 2,7G is cache, and 10G is shown as "free". But look at the last column: it shows 13G as "available". That means that you can use 13G of RAM (that is, sum of free+cache) despite only 10G being shown as "free". As the other answers have pointed out, the cache memory is temporary: it is assigned to applications if needed, exactly as the "free" memory; there's no difference between these two with regard to this.

Look at this this way: the system uses part of free memory on your computer to temporarily cache data read from disk to speed up disk operations. But regardless of being used as cache, this memory is still free to be used by applications, that is, it will be assigned to application (invalidating the cache) if the application needs it.