10

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

18
  • 21
    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 1 at 7:33
  • 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 1 at 7:53
  • 13
    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. Jul 1 at 8:45
  • 7
    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 1 at 13:29
  • 4
    @LabhamJain as has been repeatedly explained, that is wrong
    – OrangeDog
    Jul 2 at 10:18
25

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.

7
  • 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 1 at 8: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 1 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 2 at 1: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 2 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 2 at 13:13
20

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.

15

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.

4
  • 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... Jul 2 at 10:23
  • 5
    @LabhamJain sounds like you used up all your RAM and swap. Disk cache is not your problem.
    – terdon
    Jul 2 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"... Jul 2 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 3 at 11:32
10

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.

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.

2
  • That's not i want i just want to limit my caching of memory... Jul 1 at 11:51
  • 1
    @LabhamJain No you don't.
    – Boann
    Jul 3 at 12:11
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.

0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.