16

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

terdon
  • 100,812
  • 24
    This isn’t trouble. This is by design. Why have RAM if you’re not using it? When the system needs the memory for other purposes, such as applications and whatnot, the cache will be shuffled to swap or dropped from memory to accommodate the immediate requirements. Otherwise the system will do what it needs to be quick and responsive. This is one of the main benefits of Linux over another OS, like Windows. – matigo Jul 01 '21 at 07:33
  • Well, I don't have any problem storing cached memory, i just want that it should be in limit... Because if system is taking all the space in caching then i won't be able to use the ram... now because of the cached memory issue my chrome is showing me SIGSEV error. and AVD always sucks. – Labham Jain Jul 01 '21 at 07:35
  • 3
    If your programs make malloc() (memory allocation) calls and no free RAM is available then the cached ram is allocated, that part of the cache is just lost... I don't see how restrictions will help your issue, you're probably focused on the wrong issue.. (opinion, I don't know the code & my reading of linux kernel workings books was long ago) – guiverc Jul 01 '21 at 07:53
  • 14
    This is not a problem, you have it wrong. The system IS NOT using any memory for cache that would otherwise be used for applications. You THINK this is a problem, when in reality it's not. – Artur Meinild Jul 01 '21 at 08:45
  • Not related to the question: since you have a lot of RAM, consider switching to ZRam instead of HDD based swap. – Archisman Panigrahi Jul 01 '21 at 09:03
  • 8
    This is normal. As an example, my 32GB RAM daily driver system's cached ram is 7.6GiB out of ~31GiB usable. 7GB of RAM is actively in use by the system. Withj the cache, you'd think that I'm using 14.6GB of RAM, when however I'm only actually using 7GB of RAM. The 7.6GB of RAM is released if it's needed, otherwise it's kept only to 'speed up' things on the disks, caches, etc. Cached RAM is held until it's needed, and is immediately released when needed - it's not actually affecting your memory usage in any way. – Thomas Ward Jul 01 '21 at 13:29
  • 1
    @LabhamJain Listen to yourself. The system is using the memory. You want it to not use the memory. And your reason is that if the system uses the memory it "won't be able to use the ram". That makes no sense. You want the system to be able to use the RAM, it is using the RAM, proving that it is able to use it. – David Schwartz Jul 01 '21 at 17:52
  • 3
    SIGSEV is a result of programmer error. It has nothing to do with how much RAM is available or not. AVD always sucks when it's emulating arm instructions on an x86 CPU. – OrangeDog Jul 01 '21 at 22:00
  • @matigo: Windows also uses "unused" memory to cache things, but most of it's UIs hide this from the user so they dont' get confused and panic. – Mooing Duck Jul 01 '21 at 22:51
  • 1
    @OrangeDog It's actually a problem which happens when chrome gets out of memory access... so what i meant by cached memory is that my system occupies majority ram in cached so technically i'll be only able to use free memory e.g. 11 gb total - 6 gb in cached so i can only use 5 gigs max... – Labham Jain Jul 02 '21 at 10:17
  • 4
    @LabhamJain as has been repeatedly explained, that is wrong – OrangeDog Jul 02 '21 at 10:18
  • 3
    https://www.linuxatemyram.com/ Unused ram is wasted ram, If something needs that memory, Linux will drop what ever its using it for now. So its no real issue – exussum Jul 02 '21 at 11:42
  • 2
    Now that you mentioned SIGSEGV, this looks like an XY problem. – Ruslan Jul 02 '21 at 12:26
  • Note that cached memory does not always mean memory that can be discarded when more RAM is needed. For example, data in tmpfs will show as "cached memory". However, System Monitor will show this kind of memory as both cached and also being used (so it will be in the colored portion of the pie chart). This has bitten me before. – nulldev Jul 03 '21 at 18:09
  • 1
    Windows also has caches, it’s just a bit less aggressive about it. @matigo – eckes Jul 03 '21 at 22:52
  • https://www.linuxatemyram.com - this is a classic ;) – kiler129 Jul 04 '21 at 07:31
  • @OrangeDog yes but - since a failing malloc returns null it is very common for a program to crash with SEGV if it runs out of virtual memory and does no proper return checking, so there is a strong correlation there, For example the ZIp code in the Java runtime had that problem for years. – eckes Jul 04 '21 at 11:47
  • @eckes malloc() on Linux essentially never returns NULL, so that's moot. – marcelm Jul 04 '21 at 15:50
  • 1
    @matigo Why have RAM when not using it? So it can be allocated for programs when needed instead of having the whole system hang as 2GB is suddenly put on the swap, killing the poor SSD. This is crap design. I have 8GB and I hate how freely the OS dares to allocate 3GB for cache while I know I will run out of memory soon and experience mini-hang. – Maciej Kravchyk Oct 20 '22 at 07:06
  • I have just paid attention to it. Just hit RAM overflow, the cache still takes 2GB RAM and is forcing app memory to swap. – Maciej Kravchyk Oct 20 '22 at 09:17
  • Please check this link https://www.linuxatemyram.com/, author explains that what you are seeing is not what you think is going on. – Ilay Jul 10 '21 at 05:04

7 Answers7

28

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.

mchid
  • 43,546
  • 8
  • 97
  • 150
  • If you want to increase system performance, the best thing you can do is switch to a solid state drive instead of a hard disk drive. – mchid Jul 01 '21 at 08:58
  • Personally, I switched to a SSD and increased swappiness to 70 to get better performance on 5GB of RAM. Since you have 11GB, swappiness will have little effect on system performance unless the system gets bogged down. In this case, swappiness should increase performance, not decrease it. – mchid Jul 01 '21 at 13:15
  • 1
    @MooingDuck: Why do you think Linux has swap disabled by default? I think Ubuntu creates a swap partition during the default install - of course, you can insist on not having swap if you want. Also, "crash" is when the system becomes unresponsive or reboots - this is very different to what Linux does in an out-of-memory situation (deterministically stopping processes to free up ram) – Sergey Jul 02 '21 at 01:51
  • 2
    It's not quite fair to say Ubuntu doesn't crash on RAM overload. OOM-killing your X server is just as bad for desktop experience as locking up or kernel panicking. True, nowadays it's rare because of OOM score adjustments done by modern distros to important processes, but I did experience it often about 5 years ago. – Ruslan Jul 02 '21 at 12:20
  • 1
    Of course Linux (including Ubuntu) can crash on RAM overload. It's just harder to get to that point because of the way it manages memory, but it isn't impossible. If you actually use all of your RAM, the system will start swapping and if you fill that up as well, it will become unresponsive and eventually crash (presumably, we usually force reboot when this happens). – terdon Jul 02 '21 at 13:13
  • @terdon I mean, it won't crash, there's OOM killer for that sort of situation. Depends on what you mean under "crash", of course, but kernel will be just fine. Other processes - it depends. – val - disappointed in SE Jul 03 '21 at 17:35
  • @terdon Yeah, I've had the system frequently lock up on a really old computer (only 1.5GB of RAM) but that was because my swap partition was too small and the HDD was super slow. After increasing the size of the swap partition (and swappiness from 10 back up to 60), the system was slow but not unresponsive. – mchid Jul 04 '21 at 04:00
23

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.

fraxinus
  • 401
16

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.

  • This doesn't seems to be true, i tried running android emulator but when my ram got full with cache of 4gb it hanged my system instead of deleting that cache... – Labham Jain Jul 02 '21 at 10:23
  • 6
    @LabhamJain sounds like you used up all your RAM and swap. Disk cache is not your problem. – terdon Jul 02 '21 at 13:14
  • 3
    @LabhamJain cache problems don't hang your system, so I think "cache" is a red herring, and you have a completely different problem... this happens all the time here, it's called an "XY problem"... – Harper - Reinstate Monica Jul 02 '21 at 15:18
  • @LabhamJain I used to experience disk thrashing on an old computer and it turned out I needed to increase the size of my swap partition—the system was trying to swap out to swapspace that didn't exist. Also, I've had temporary hangs because of a slow hard drive. The solution there was to switch to SSD. – mchid Jul 03 '21 at 11:32
  • In my experience, all the answers that say that caching is not a problem are wrong. In some cases, memory is not made available fast enough to a program such as firefox and this is when the hell starts since the computer starts to swap, this causes cpu to flare up to full usage due to interrupts, rendering the computer unusable. You can try to manually envoke the kernel killer of highest ram eating programs but it will not work since your keyboard does not work since cpu is flooded by swap caused interrupts. Caching is the biggest bug in Linux kernel and as often is, it is called a feature. – atapaka Feb 01 '23 at 20:13
  • my linux kernel crashes a few times a year and its always this. today it was steam verifying a very large game. I have 64 gigs of memory in my laptop. i basically have to do

    watch -n 30 'echo 3 > /proc/sys/vm/drop_caches'

    – Eric Twilegar Dec 28 '23 at 17:01
11

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.

raj
  • 10,353
3

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.

Irsu85
  • 551
3

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.

2

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.