62

After the system starts, in a few minutes my memory cache fills up and it starts using the swap. Here's a screenshot of /proc/meminfo.

memory

However, if I can disable this process / clean the cache once after that, I think my system will speed up to some extent. Correct me if I am wrong here.

I have also tried Bleachbit memory cleaning, but it doesn't seem to clean the memory cache properly. Also, the feature is still in an experimental stage.

bleachbit memory

There was already a similar question posted here: How can I disable the prefetch cache?, but it was regarding Ubuntu Server, and also the answers involved manual settings etc.

So, I want to know if there's some software for disabling/enabling Memory Cache for Ubuntu 12.04.

  • 1
    Are you sure this will solve your problem? Memory doesn't fill up without a reason. I also had the problem that my memory was filling up, and traced it back to tracker-miner. After I uninstalled all tracker packages, my problem went away. I also have 4GB memory, and 2GB are used by chaches, but I'm nor experiencing any problems. – daniel kullmann Jun 25 '12 at 12:39
  • 1
    @danielkullmann: He's referring to cached memory, not memory used by apps... – ish Jun 25 '12 at 13:04
  • @izx Yes, I know. It could just be that he looks for the solution of the wrong problem. The kernel is smart enough not to fill up memory with caches when it is needed by programs. That's why I think that his memory problems come from a program that uses too much memory. – daniel kullmann Jun 26 '12 at 06:28
  • @danielkullmann: I am curious to know how you traced it back to one process that's gobbling up all that memory... Is there some tool/command for that? – Bharadwaj Srigiriraju Jun 26 '12 at 14:43
  • I just used the htop command, and sorted by memory usage. – daniel kullmann Jun 26 '12 at 15:56
  • @danielkullmann: Oh, yes the htop... How conveniently I forgot one of the best tools ever. Nevermind, I thought you used some alien software I didn't know... LOL – Bharadwaj Srigiriraju Jun 26 '12 at 20:23
  • You barely have anything in swap in that screenshot. I've seen small amounts of memory get swapped for no apparent reason, not sure. But anyway, I don't think the disk cache is at fault here. – sudo Sep 08 '17 at 02:17
  • Since I cannot comment I do answer. Disk cache fills RAM when there is a write action on disk. If you have a lot of writings the cache will grow to the point that the system will start swapping. I know it's true because I have seen this before and it sounds crazy to me but apparently is normal for the rest of the world. Read the logic: you use the disk intensively and what do you get? More disk usage because the system starts swapping because you dump stuff on disk. I mean... hello anybody at home? My suggestion is, use sync && echo 3 | sudo tee /proc/sys/vm/drop_caches as much as you can be – Viktor Joras Jan 13 '19 at 10:31

4 Answers4

73

Note: Linux is NOT "eating" your RAM! Please take a look at Geirha's excellent answer below to understand why...

After the above note, if you still feel "cleaning" the cache could help, you can certainly try: it's a one-liner from the terminal:

sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

There's no easy way to disable the cache, but you can achieve the same effect by cleaning it as often as every minute, if you want to:

  • Make it a cron-job

  • Press Alt-F2, type gksudo gedit /etc/crontab, and add this line near the bottom:

    */15 * * * * root sync && echo 3 > /proc/sys/vm/drop_caches

  • This cleans every 15 minutes. You can set to 1 or 5 minutes if you really want to by changing the first parameter to * or */5 instead of */15

One liner to know REAL free RAM, excepting cache

Geirha's answer explains the details, but in short, you get the number of free megabytes with:

free -m | sed -n -e '3p' | grep -Po "\d+$"

which on my 2GB command-line server returns an extremely health 1835.

ish
  • 139,926
  • 5
    Ergh! Knocking out the cache is bad enough, but making a cronjob for it? I mean, this does "solve" the question, so to speak, but that doesn't make it any less painful. – Reid Jun 25 '12 at 13:25
  • 2
  • @izx: hmm... so this is how one creates a cronjob. If I am going to drop the cache periodically, doesn't it mean that I am actually slowing down the system? If that were the case, I think your answer sounds more like a theoretical answer than geirha's answer. :p

    Good answer BTW...

    – Bharadwaj Srigiriraju Jun 25 '12 at 19:23
  • 2
    @ForbiddenOverseer: I only put that in because you wanted to "disable" the cache, and dropping it periodically is the only way to accomplish that. My note at the top (and geirha's link) clearly states that the cache actually speeds up your system most of the time, even if it looks like its "eating your RAM) --- so yes, IMO you would be slowing down your system, but you wanted to try it, so I just answered your question instead of simply nagging you and telling you it's a bad idea and so you shouldn't do it! I just gave you the power to experiment... :D – ish Jun 25 '12 at 19:49
  • 1
    I am using Ubuntu Server 12.04 LTS as a VDS. I am trying approach provided in this answer, but I am constantly getting tee: /proc/sys/vm/drop_caches: Permission denied. Any tips? – Eimantas Aug 03 '13 at 13:57
  • This cron job did the trick for me in centos 7 in vagrant * * * * * sync; echo 3 > /proc/sys/vm/drop_caches – Damodar Bashyal Mar 28 '17 at 00:38
  • 1
    Before starting to drop_caches on production, be absolutely sure that truly helps the overall picture. The changes are very high that your performance as a whole will suffer from dropping the caches. – Mikko Rantalainen Jan 04 '21 at 08:49
  • 1
    Note that if your workload prefers having e.g. 200 MB of free RAM (to allow up to 200 MB process to launch without needing to carve the RAM from cache) just set /proc/sys/vm/min_free_kbytes as needed. No need to drop caches for that! – Mikko Rantalainen Jan 04 '21 at 08:55
64

Help! Linux ate my RAM!

www.linuxatemyram.com explains this beautifully in FAQ form, with the essentials being:

What's going on?

Linux is borrowing unused memory for disk caching. This makes it looks like you are low on memory, but you are not! Everything is fine!

Why is it doing this?

Disk caching makes the system much faster! There are no downsides, except for confusing newbies. It does not take memory away from applications in any way, ever!

What if I want to run more applications?

If your applications want more memory, they just take back a chunk that the disk cache borrowed. Disk cache can always be given back to applications immediately! You are not low on ram!

How do I see how much free ram I really have?

To see how much ram your applications could use without swapping, run free -m and look at the "available" column:

$ free -m
              total        used        free      shared  buff/cache   available
Mem:           1504        1491          13           0         855      792
Swap:          2047           6        2041
This is your answer in mebibytes.

Source: as mentioned, the excellent www.linuxatemyram.com -- please visit for more information.

geirha
  • 46,101
  • 1
    @geirha: Thanks for the link... It was very informative and I liked the experiments given at the end!! – Bharadwaj Srigiriraju Jun 25 '12 at 19:20
  • @izx: tbh, I am having a hard time deciding accepted answer. Not to mention, this link was actually informative in every other way... ;) – Bharadwaj Srigiriraju Jun 25 '12 at 19:27
  • @ForbiddenOverseer: see my comment in my answer for a detailed reply -- I gave you the short version of why you shouldn't do it, but then answered your question anyway, giving you the power to experiment and try it out for yourself. If you don't think cleaning/dropping the cache helps, just don't do it anymore :) – ish Jun 25 '12 at 19:51
  • @izx, I found it a bit hard to do that for this case. However, you did write a useful text on the matter, so perhaps you could just add the url to your answer, and we delete my answer? – geirha Jun 27 '12 at 09:10
  • No need, your link in an excellent resource; I just incorporated some parts into the answer (with attribution) so that it's now a perfectly valid alternative (in fact the better answer as seen by the public!) Great job, upvoted - welcome to 5k! – ish Jun 27 '12 at 09:51
  • Oh... I can see that the answers have changed... Now, this one's sure a great answer!!

    @geirha: My apologies. May be I was too quick to judge an answer... I will think more when I vote next time. Regardless of what answer I have accepted for, I am sure the upvotes this answer got shows that this might be more helpful to people.

    – Bharadwaj Srigiriraju Jun 28 '12 at 15:12
  • "There are no downsides except confusing newbies" - Why'd ya have to call me out like that. – rocksNwaves Aug 18 '20 at 02:11
  • Also worth mentioning is that with modern kernels the Available is the only number you should look at. For example, if you use PostgreSQL with 10 GB shared_buffers setting it will show up as extra 10 GB in the buff/cache display and kernel cannot drop those caches on need. – Mikko Rantalainen Jan 04 '21 at 08:51
  • I have a machine with 0.5Tb of RAM and even though there is a lot of memory marked available, it will swap once in a while... Any idea why that would happen? See https://superuser.com/questions/1517266 – Alexis Wilke Mar 11 '22 at 15:02
  • There are no downsides point is arguable since your device may suddenly lose power and any changes to data flying in cache and not written to disk yet will be lost. So it should truly say that the downside is performance gain at the expense of reliability. – igor Sep 24 '23 at 22:00
  • @igor that's quite an assumption, that linux would casually wipe out data waiting to be written to disk in order to give it to an application that needs more memory. It's data cached from disk reads that can be safely wiped at any time. – geirha Feb 20 '24 at 08:14
10

To Check your Current Memory Usage

 watch -n 1 free -m

or

 watch -n 1 cat /proc/meminfo

To Free Up Space

 sudo sysctl -w vm.drop_caches=3

NOTE: this action won't make your system faster nor it will affect its stability and performance, it will just clean up memory used by the Linux Kernel on caches.

or

 sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

NOTE: You can use cron jobs to schedule the commands above to run at specific time intervals.

SilleBille
  • 453
  • 5
  • 18
0

As we can see the cashing not a problem just if it cause swapping, what you can avoid by managing the vm.swappiness value. Maybe that's what you looking for: why-is-swap-being-used-even-though-i-have-plenty-of-free-ram

Tamas
  • 9