On Sunday 16 August 2009 20:56:59 Philip Webb wrote:
> 090816 Raul Gonzales wrote:
> > I have a 2G of physical RAM but even without any major activity
> > output of free,vmstat and top reports only  ~64M free.
> >
> > free -m
> >             total       used       free     shared    buffers     cached
> > Mem:          1953       1888         65          0        143       1557
> > -/+ buffers/cache:        187       1765
> > Swap:         2055          0       2055
>
> The crucial figure is '187', which is what is usable.
> You have a lot of something cached: any idea what it might be ?
> -- the kernel should delete that stuff, if it needs the space.

top, free and every other tool that examines /proc lies through it's teeth.

Well, not really, but it's necessary to know what exactly it is measuring and 
also how the kernel is written.

But first: Raul, there is nothing wrong with your box. It is working as 
designed.

The "used" column literally means memory that in use for something, somewhere.
The "free" column literally means memory that is not in use anywhere. It's 
normally around 2 or 3 % of total physical RAM.

In Raul's case, the kernel has decided to allocate 1557M of RAM to cache. A 
particular block might have cached data in it, or it might not. It might have 
been used then marked as up for not in use anymore, or it might never have 
been written to since last reboot. It doesn't matter. All that does matter is 
that the kernel has decided that those blocks of memory *may* be used for 
caching.

The trick is that if more memory is needed by apps than what is listed as 
immediately available and free, the kernel will search out chunks that it can 
de-allocate, then it will do so.

Why does the kernel do this? It's a programming technique called lazy-
deallocation (similar to lazy-initialization) and it's used because it is 
efficient. It means the kernel only has to do work when it needs to do work. 
De-allocating memory as soon as it is not in use is expensive and usually a 
waste of time - it's very likely that that very chunk of memory will almost 
immediately be used once again. If so, why bother de-allocating it at all?

Bottom line: You, the user, do not really know what the kernel is doing with 
memory. It's also changing about 100 million times a second and your eyes 
don't work that fast. The kernel is designed to do these things in an 
autonomous fashion because it knows what to do, and you the user do not. The 
most you can do about this is provide hints as to how the kernel should 
operate and then let it get on with it.

top, free, et all, simply show snapshots of memory state at a point in time. 
You should ignore the absolute values they display and concentrate more on how 
the numbers are changing over time. A value that gradually increases while no 
real processes are running would indicate a problem for example.

This is all very correct and follows good engineering principles.

-- 
alan dot mckinnon at gmail dot com

Reply via email to