tags:

views:

233

answers:

2

How does free calculate used memory and why does it differ from what /proc reports?

# cat /proc/*/status | grep VmSize | awk '{sum += $2} END {print sum}'
281260

But free says:

# free
             total       used       free     shared    buffers     cached
Mem:        524288     326488     197800          0          0          0

Who is right? Is 281260kb memory used or 326488kb?

+3  A: 

The title asks: "How does free calculate used memory?"

Answer: It asks the OS, which has to keep track of that to do it's job.

More specifically, it asks the memory management subsystem. As sheepsimulator notes in the comments, the Linux kernel exposes all kinds OS maintained data in the /proc virtual filesystem, but every full service OS has to keep track of them kind of data, so it is a small matter to provide an API for free to use.

The question asks: "Why does this differ from adding up the VmSize reported for all processes?"

Answer: There are at least to thing going on here

  1. Linux will promise memory to a program without actually allocating it. When you do char *p=new(1024*1024*1024*sizeof(char)); the kernel doesn't go out at get you a gigabyte right away. If just says "OK", and figures it will grab it when you start using it. Thus the need for the infamous OOM killer.
  2. Dynamic libraries are are shared, and a single page of real memory can be mapped into the virtual address space of more than one process.

Further, your pass over the proc filesystem is not atomic.

The upshot is that the output of free more accurately reflects the use of physical memory on your machine at a given moment.

dmckee
I think you misunderstood the question. Yes, free reports the numbers the OS gives it. But where does those numbers come from and why the discrepancy between VmSize and free's number? It is even more confusing when the VmSize sum is greater than what free reports.
Björn Lindqvist
Yeah. I got there eventually...
dmckee
+1  A: 

The result from 'free' is more likely accurate than adding up the Virtual Memory size of each process (which is just virtual memory afterall, might even add up to more memory than is physically present!)

/proc/meminfo will give you some more of the details than 'free'.

Mike K