Score:11

Cached Memory Takes all the memory

id flag

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

in flag
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.
Labham Jain avatar
id flag
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.
guiverc avatar
cn flag
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)
Artur Meinild avatar
vn flag
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.
us flag
Not related to the question: since you have a lot of RAM, consider switching to ZRam instead of HDD based swap.
ru flag
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.
cn flag
@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.
gf flag
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.
cn flag
@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.
Labham Jain avatar
id flag
@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...
gf flag
@LabhamJain as has been repeatedly explained, that is wrong
fk flag
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
Ruslan avatar
bv flag
Now that you mentioned SIGSEGV, this looks like an [XY problem](https://xyproblem.info).
nulldev avatar
cn flag
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.](https://askubuntu.com/questions/762717/high-shmem-memory-usage)
eckes avatar
cn flag
Windows also has caches, it’s just a bit less aggressive about it. @matigo
fr flag
https://www.linuxatemyram.com - this is a classic ;)
eckes avatar
cn flag
@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.
marcelm avatar
cn flag
@eckes `malloc()` on Linux essentially [never returns NULL](https://stackoverflow.com/questions/16674370), so that's moot.
Maciej Krawczyk avatar
in flag
@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 Krawczyk avatar
in flag
I have just paid attention to it. Just hit RAM overflow, the cache still takes 2GB RAM and is forcing app memory to swap.
Score:26
bo flag

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 avatar
bo flag
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 avatar
bo flag
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.
in flag
@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)
Ruslan avatar
bv flag
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.
terdon avatar
cn flag
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).
cn flag
@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.
mchid avatar
bo flag
@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.
Score:21
tr flag

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.

Score:15
bd flag

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.

Labham Jain avatar
id flag
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...
terdon avatar
cn flag
@LabhamJain sounds like you used up all your RAM _and_ swap. Disk cache is not your problem.
Harper - Reinstate Monica avatar
bd flag
@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"...
mchid avatar
bo flag
@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.
mu flag
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.
Score:10
cn flag
raj

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.

Score:3
cn flag

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.

Labham Jain avatar
id flag
That's not i want i just want to limit my caching of memory...
Boann avatar
cn flag
@LabhamJain No you don't.
Score:3
eg flag

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.

Score:0
br flag

Please check this link https://www.linuxatemyram.com/, author explains that what you are seeing is not what you think is going on.

Score:0
id flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.