Embedded Programmer
Embedded Programmer

Reputation: 641

Where does virtual memory exist in linux?

As program is stored on flash/disk. For it execution, program is loaded into virtual memory and is mapped to RAM by virtual manager. During its execution process is in RAM. Then where does virtual memory exist (where it has all .text, .data, .stack, .heap)?

Upvotes: 0

Views: 2613

Answers (3)

glglgl
glglgl

Reputation: 91017

Virtual memory means memory you can access with "normal" momory access methods, although it isn't clear where the data is actually stored.

It may be

  • actually in RAM
  • in a swap area
  • in another file (memory mapped file)

and access to it will be handled appropriately.

It is a layer of, well, virtualization so that you as a programmer don't have to worry about where the data is actually put.

The original purpose was mainly to be able to provide more memory to processes than we actually have and to extend it with means of swap space, but there are even more:

  • The OS is free to use the RAM for whatever it seems necessary, e. g. caching. Under some circumstances, it may be more effective to use RAM for cache than for holding parts of a program which hasn't been used for a long time.
  • Provide additional memory to a program when it requests it: if you call malloc(), the program's library may request the OS to provide a part of memory which can be attached seamlessly into the address space.
  • Avoid stack overflow: if the stack grows larger and larger, the respective memory section may be extended as well transparently so that the program won't have to worry about it.
  • A system can even do "overcommitment" of memory: if a process requests a large amount of memory, the OS may say "yes, ok", i. e. provide the memory to the program. That means in the first place "allow the program to access a certain address space area", but this address space is not immediately backed by memory. Only as soon as the program accesses this memory the mapping will be done, and if this cannot be fulfilled, the program is crashed by the Out of emory killer (at least, under Linux).

All this works by page-wise (1 page = 4 kiB) assignment of physical memory to a program, viewed via the program's address space, and this in the amount and frequency as it is needed.

Upvotes: 0

Ingo
Ingo

Reputation: 36329

The virtual memory is a view of the RAM plus maybe some swap space provided by a virtual memory manager. Modern OSs have virtual memory managers and provide virtual memory to processes so that the executing program can behave as if it had a contiguous address space whose size is not limited by the actual RAM. The pages or blocks making up the virtual memory can be mapped anywhere in the RAM, so that contiguos virtual pages need to be stored in contiguos RAM areas. Or they can be swapped out to page space or swap space, waiting there until needed, whereupon they're read by the OS and mapped to some RAM page.

When you say

During its execution process is in RAM.

This is not entirely correct. Some or all memory pages that belong to the process may be swapped out, as explained.

One more word concerning the answers and comments that say that "virtual" means it doesn't exist. This makes no sense. On the contrary, according to Webster:

being such in essence or effect ...

Hence virtual memory is something (therefore, it exists!) that behaves as if it were memory.

Upvotes: 3

user2560622
user2560622

Reputation:

Virtual memory is just like an illusion of RAM. It uses paging to acquire additional RAM that could be used by the processes in operating system.

Upvotes: 0

Related Questions