This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Join our live AMA on the Future of Page Builders with Brizy's CEO! Register Now →

“Linux Is Eating My Ram” Myth Busted

Updated on January 28, 2017

4 Min Read

In my experience, as a system administrator, I have been asked a lot of questions about system eating up all the memory. In all fairness, it is not uncommon for an operating system to occupy the memory and release it when required.

linux-ate-my-ram-banner

There is a saying in Linux community: “Free memory is the waste of memory.”

What happens in Linux OS is that it borrows unused memory for disk caching. This makes memory to be apparently consumed by the operating system, but in real essence it is not “eating up RAM.”

But, the question stands: Why do we think Linux is mean when it devours RAM?

Linux is not mean. What it does is for the sake of speed. It makes caching much faster. It acts like a “bank” which keeps all the memory and a chunk of it is released when the need arises. Whenever an application needs more memory, a chunk is borrowed immediately and it is returned once the function is complete.

The common utilities in Linux to check free memory explain it a bit differently. Check out the screenshot below. In it, you can see that the amount of free memory is 168 MB whereas the cached processes are using 1261 MB of RAM.

You don’t really need to clear disk cache but if you insist you can do this by using the following code:

sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"

Once the cache is emptied, you can see the amount of free memory has increased.

memory_usage_2

Another question that I get (and I don’t know why I get it) is: How can we stop it?

My answer is simple: “WHY in the world you want to stop it?”

This free memory is usually invested in a technique that improves performance. Stopping this process will definitely decrease performance. Disk cache makes applications load faster and run smoother, but it NEVER EVER takes memory away from them! Therefore, there’s absolutely no reason to disable it!

Let’s try an experiment:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
int main(int argc, char** argv) {
   int max = -1;
   int mb = 0;
   char* buffer;
 
   if(argc > 1)
       max = atoi(argv[1]);
 
   while((buffer=malloc(1024*1024)) != NULL && mb != max) {
       memset(buffer, 0, 1024*1024);
       mb++;
       printf("Allocated %d MB\n", mb);
   }
   return 0;
}

Here, we have a small script that will keep on consuming memory. Let us see how it goes. First, we will check the free memory.

Here you can see the free memory shown is 156 MB. OOM Killer should just end this process and hopefully the rest will remain undisturbed. We need to disable swap for this.

$ sudo swapoff -a
$ gcc munch.c -o munch
$ ./munch

You will observe something like this.

Even though it showed 156 MB “free”, it didn’t stop the application from grabbing 1347 MB.

Afterwards, the cache is pretty empty, but it will gradually fill up again as files are read and written.

However, you do need to understand that you cannot run on the same amount of RAM as you grow. Each visitor to your website uses a minute amount of RAM when they are visiting your website. As your website becomes popular, you will get more visitors and there will come a time when you will need more RAM. On Cloudways, you can increase RAM by scaling your server size from the Vertical Scaling section inside the Server Management tab.

Vertical Scaling Cloudways

At Cloudways, we want to provide the easiest cloud hosting platform ever. If you are interested, start your free trial from the banner below.


Many thanks to LinuxAteMyRam for this study.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Maaz Shah

Maaz Shah works as System Engineer for Cloudways. His days are spent in tackling technical troubles.

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now