LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Why Are VIRT Size and RES Size Different When There is No Swapping? (https://www.linuxquestions.org/questions/linux-newbie-8/why-are-virt-size-and-res-size-different-when-there-is-no-swapping-815710/)

forbin 06-22-2010 12:49 PM

Why Are VIRT Size and RES Size Different When There is No Swapping?
 
Top shows a number of processes with VIRT size of approx. 300MB and RES size approximately half that. Swap shows 0k used, so there's no swapping going on.

According to the man pages, RES=CODE+DATA and VIRT=RES+SWAP. If SWAP is 0, then wouldn't RES and VIRT be the same?

johnsfine 06-22-2010 01:44 PM

Which man pages?

Quote:

Originally Posted by forbin (Post 4011656)
According to the man pages, RES=CODE+DATA

It's a tough call whether that is just seriously misleading or flat out wrong.

Quote:

VIRT=RES+SWAP.
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.


All times are GMT -5. The time now is 04:05 AM.