Differences and relations between VIRT and USED in output of top?

40,966

Solution 1

  • RES is the amount of RAM currently used by the process. This value can vary because memory pages might be swapped in or out. It might even be 0 for a process that has been sleeping for a long time, e.g. an unsolicited daemon.

  • VIRT is the full size of all memory the process is using, whether in RAM or on disk (shared objects, mmaped files, swap area) so VIRT is always larger than or equal to RES. A process is always dealing with (i.e. allocating / accessing / freeing) virtual memory. It is up to the operating system to map some or all of these pages to RAM.

  • USED is less than VIRT because it doesn't include the memory that is backed by something else than swap, for example code and libraries.

Solution 2

RES means the actual memory assigned currently to the running process in (physical memory) resident in RAM.

VIRT shows virtual memory (assigned on the hard disk and/or RAM) to the process (as explained in the man page).

USED = RES + that part of the executable which currently not resides in RAM. ps shows VIRT using vsz flag.

Share:
40,966

Related videos on Youtube

Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on September 18, 2022

Comments

  • Tim
    Tim almost 2 years

    From manpage of top

    VIRT  --  Virtual Memory Size (KiB)
               The total amount of virtual memory used by the task.  It includes
               all code, data and shared libraries plus pages that have been
               swapped out and pages that have been mapped but not used.
    
    USED  --  Memory in Use (KiB)
               This field represents the non-swapped physical memory a task has
               used (RES) plus the non-resident portion of its address space
               (SWAP).
    

    It seems to me that VIRT and USED mean the same, i.e. they are both the sum of what a process occupies in the physical memory and what in the swap. So what are their differences and relations?

    By the way, by default, top doesn't show USED. How can I make it visible?

  • heemayl
    heemayl about 8 years
    +1 but riddle me this..shared objects (and similar) does not have to reside in RAM or swap? They can be simply on disk and referred to the disk location?
  • jlliagre
    jlliagre about 8 years
    @heemayl They are either in RAM or in the file that originally contain them, no need to duplicate their storage in swap.
  • jlliagre
    jlliagre about 8 years
    Memory located in RAM (RES) might be swapped out in the future.
  • heemayl
    heemayl about 8 years
    Sorry for being a noob here, but before using those they should be brought to RAM, right?
  • jlliagre
    jlliagre about 8 years
    @heemayl Only the part of these files that is actually required (i.e. addressed) need to be brought to RAM.
  • tuzion
    tuzion about 8 years
    Right, fixed the answer!
  • jlliagre
    jlliagre about 8 years
    and VIRT is not necessarily assigned on the hard disk, might simply be in RAM and that's it.
  • hobbs
    hobbs about 8 years
    @heemayl they do need to be in RAM when they're actively used (at least, the pages that are in use do), but they can be evicted at any time and loaded from disk later. This is different from anonymous pages which can't be evicted from RAM without being written to swap first.
  • Tim
    Tim about 8 years
    thanks. (1) For USED, is "the memory that is backed by something else than swap, for example code and libraries" on the disk or in RAM? (2) Is USED equal to RES plus SWAP? Is VIRT - USED equal to the part of VIRT which is on the disk but not in the swap? (3) After reading the comments about shared objects, here is my question about them unix.stackexchange.com/questions/289663/…
  • Tim
    Tim about 8 years
    Thanks. Sorry that I am still confused. 1. In the manpage of top, field CODE is "the amount of physical memory devoted to executable code, also known as the Text Resident Set size or TRS." Is CODE the part of RAM where code is copied from disk? 2. Are code and libraries part of VIRT on disk? If yes, are they shared objects or mmaped files, since they are not in the swap area? "VIRT is the full size of all memory the process is using, whether in RAM or on disk (shared objects, mmaped files, swap area)".
  • Tim
    Tim about 8 years
    3. Is the used swap by a process included in USED of the process, and also in VIRT of the process? Since both VIRT and USED include RES, is VIRT-USED in disk? If both VIRT and USED include the used swap of the process, is VIRT-USED exactly the part of the virtual memory of the process on the disk but not in the swap?
  • jlliagre
    jlliagre about 8 years
    (1) The CODE column is documented to display the size of the code and libraries bits that are stored in RAM. (2) yes. They are both shared objects and mmaped files (have a look to /proc/<pid>/maps (3) yes.
  • jlliagre
    jlliagre about 8 years
    Beware when you compute values from a process statistics. RES size might look too small compared with other columns. One reason might be the fact these statistics are computed from different sources, and some of them do not take into account the fact a shared library or a memory mapped file shared by multiple processes doesn't use more space than when used with a single process. RES is measuring the actual usage, portions of CODE and DATA might be shared.