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

Answers 8

  • Well, I've figured out my problem with the sync; echo 1 > /proc/sys/vm/drop_caches command, I misspelled this command somewhere, and now if I use this command properly then it cleans my cached memory, hence my problem seems to be solved, though more answers for future references and for community will be appreciated.


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


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


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


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


  • Empty RAM used as cache for your ssd is by design to speed up your computer. If you want to clear the cache every x minutes, you can set a crontab entry to clear the cache.


  • Welcome to GNU/Linux, friend!

    In this world, our kernels don't waste ram, and we don't worry about ram being 'in use' unless it's applications fighting other applications for it.

    What you describe is normal.

    100% of ram will be in use ideally on a GNU/Linux system. What ram applications don't need will be used by disk caching. Wouldn't you rather have disk operations operate at RAM's speed than at the speed of your hard disks? Well, you get that now, for free. Enjoy, and don't sweat it when you see memory in use by caching.



Related Questions