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
- 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.
- 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.