Embedded Programmer
Embedded Programmer

Reputation: 641

Size of virtual memory during run time

I created an application, whose size, when i am checking using size is displayed as

  admin@pc:~/Desktop$ size u
  text     data     bss     dec     hex  filename  
  1725     552       16    2293     8f5   u

when checking using ps -au, during run time

admin@pc:~/Desktop$ ps -au  
USER    PID    %CPU %MEM  VSZ    RSS TTY      STAT START   TIME COMMAND  
admin   16730  0.0  0.0   3876   448 pts/2    S+   15:48   0:00 ./u  
admin   16731  0.0  0.0   3876   252 pts/2    S+   15:48   0:00 ./u
  1. why is it showing different size of virtual memory as we saw using size command. .i.e why 2293 is different from VSZ (3876) as seen by ps -au?
  2. As size of virtual memory is very less, it can be accumulate in single page frame(RAM/RSS) of 4k byte. It means, whole process can be loaded into single frame as 2293 byte is less than 4096 byte. Then why is RSS 448 and 252 less than 2293 or 3876?

Upvotes: 1

Views: 817

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328594

I'm pretty sure that u loads some shared libraries. These add to the size. Use ldd ./u to see those.

As for VSZ, the value is in 1 KiB (1024 byte) units:

VSZ virtual memory size of the process in KiB (1024-byte units).

(source: Ubuntu man page for ps(1)).

The virtual size contains all memory which your process needs, that also includes pages which are reserved for your process but which it doesn't actually use.

RSS is the amount of memory which currently sitting in RAM. Both processes have reserved the same amount of memory virtually but the second process hasn't actually allocated as much memory (using malloc(3) or similar library or kernel calls) or it hasn't loaded all shared libraries, yet (they are partly loaded on demand).

Upvotes: 2

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136256

why is it showing different size of virtual memory as we saw using size command. .i.e why 2293 is different from VSZ (3876) as seen by ps -au?

Stack and heap are not stored in the binary, they are created in run-time only. This is why sizes of text, data and bss sections of a binary don't add up to VSZ.

As size of virtual memory is very less, it can be accumulate in single page frame(RAM/RSS) of 4k byte. It means, whole process can be loaded into single frame as 2293 byte is less than 4096 byte. Then why is RSS 448 and 252 less than 2293 or 3876?

VSZ is reported in 1024-byte units. In other words 3876 stands for 3969024 bytes.

Upvotes: 3

Related Questions