Which man pages?
Originally Posted by forbin
According to the man pages, RES=CODE+DATA
It's a tough call whether that is just seriously misleading or flat out wrong.
That is only for a tool that defines "SWAP" as VIRT-RES even though VIRT-RES has no significant connection to use of swap space.
I think what will help you understand this topic, is a example/description of two common major parts of VIRT and one often minor part:
1) Your process is running some executable that depends on some .so files. At load time, large areas of the executable and .so files are mapped
into your virtual address space. They are just mapped in, they aren't read in. Those pages are still on disk. As they get used, pages from those mappings are read in and become part of your process RES. But all of those mappings are part of VIRT even if never read in for use and none of them use swap space even if kicked back out of RES.
2) The malloc (or similar) support software inside your process requests a big chunk of empty memory from the OS. The OS maps that as "demand zero" memory, that is part of VIRT. But no actual memory is allocated. No swap space is used and none of it goes into RES.
3) Your process writes to a small part of the memory requested in (2). The kernel allocates that small part. That is now "anonymous" memory that is counted in RES. If physical ram were so tight that some of that then got paged out, it would be the swap usage you expect.
All the "memory" I described in (1) and (2) are part of VIRT. Only some of it is part of RES. There are likely big parts of the mapped .so files containing only functions that this executable never calls. The malloc support code can't predict the program's memory use, so it over estimates, so most of the "demand zero" mappings never become real ram pages. If there is a shortage of physical ram, the kernel prefers to kick out pages of read only content of the executable, which then are waiting for next use exactly the way pages that were never used wait for first use. So VIRT may be enormously bigger than RES with no use of swap space.