1
votes

On running the "free" command, I see that memory used is:
total:3854884
used:3752304
free:102580
shared:352
buffers:9252
cached:150908

-/+ buffers/cache: used: 3592144
free: 262740

Swap: 0 0 0

But on running htop, I see there are many processes using 4507M amount of memory under the VIRT column (virtual memory). RES column (physical RAM the process is using) shows 209M. SHR (shared memory) is 5352M.

-Xmx for the process is configured as 2048m.

How can virtual memory be used if the swap space is zero?

1
Would memory-mapped files be included in VIRT?Thilo

1 Answers

4
votes

The virtual memory these programs (htop and the like) are counting is just the size of the address space the processes have requested. You have physical memory, actual RAM, and a virtual address space which maps addresses as user space programs see them to physical memory. They are separate. A 0x0ff84560 pointer probably doesn't actually reference that part of RAM. The OS gets to set up a mapping that decides where in RAM you're actually referencing. Even further, it can set up the mapping before it has RAM to back it up. It's an event driven process. The OS will set up the mapping on request with no real backing, no physical memory allocated, and only actually map it to real RAM when you try to use the virtual memory.

The size of virtual memory is the size of this mapping. But not all of it has to be backed by physical RAM, so it can be larger than RAM even when there's no swap. But this causes problems programs try to actually use more memory than there is RAM. It's no problem at all if they only request it, only if they use it.

Additionally, as Thilo mentioned, memory mapped files can add to this. You can map an entire 100TB file into your virtual address space no problem. The OS handles the logistics in the background: bringing in the parts you need (the parts you try to access) and reaping the parts it has to to clear physical memory.