Linux eating up my memory!

Well, actually, probably it isn't. If you came here, probably it's because you saw something like the top statistics below.

And you thought: "What the hell is using 7 of my 12 GB of memory when I'm just surfing with Google Chrome?"

Understanding memory management is quite often a mess. The apparently simple concepts of "free memory" and "used memory" are not so straightforward in modern OSs.

The simple, most common answer to the title question is: you need to interpret in the right way the numbers linux gives to you.
So, let's execute free command. It's a little utility to control memory consumption.


This is my personal pc memory. My boss said: "It's an airplane!" ;) 
As you can see, there are 3 rows: Mem (memory), +/- buffers/cache and Swap.
Mem doesn't need an explanation i think.
Swap maybe needs some explanation, but I don't think that's the right post. (If you want to know more about swap, you can read this link: Swap Partition)
+/- buffers/cache is memory that linux reserved in total to all the programs running, but that are in fact available to be reclaimed in case it is needed.


In extreme synthesis and with a lot of simplification, cached memory is essentially free memory, while buffered memory is data waiting to be written on the hard drive.

To be a little more precise, linux occupy all memory, for if it isn't used, it's just wasted. So, every time an application requests some data, it is saved in the cache. In this way, if the same data is requested again, it can be retrieved in memory and not from disk (which is MUCH slower). However, as I said, data is saved in a portion of memory marked as cache. If the pc is running low on available memory (really free, not even cached), and you launch another program, it essentially claims a portion of cached memory. If you have no free memory available, and your program needs more memory (maybe you opened your 10th high quality image with GIMP?), it claims in the same way some of your cached memory.
Buffered memory is not claimed until it is written on the drive (that happens very often by a background worker, so it is freed very quickly). By the way, that's the reason not to brutally shut down you pc by removing the power plug: there can in memory be some data not yet transferred to the hard drive. Since RAM mantains data until it has electricity to mantain it, the powering off without proper shutdown means that data are simply lost.

So, to calculate the amount of "free memory", you should do something like this, considering only memory, which is looking the first line of free:

Mem free  + Mem buffers + Mem cached

Calculating on my PC:

5498776 + 367580 + 4170992 = 10037348

and well, what you find out? That is exactly the free buffers/cache memory!

Beware, however, that memory is a much more complex topic, and this is only to get the idea of what is happening to your pc. I hope to be able to write something more (and to study something more!) on this subject!

REFERENCES

Labels: ,