Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Virtual memory really useful all the time?

Virtual memory is a good concept currently used by modern operating systems. But I was stuck answering a question and was not sure enough about it. Here is the question:

Suppose there are only a few applications running on a machine, such that the physical memory of system is more than the memory required by all the applications. To support virtual memory, the OS needs to do a lot work. So if the running applications all fit in the physical memory, is virtual memory really needed?

(Furthermore, the applications running together will always fit in RAM.)

like image 503
Abhinav Avatar asked Oct 10 '11 13:10

Abhinav


People also ask

Does virtual RAM really make a difference?

A phone with 8GB RAM will be more efficient and fast than a device with 6GB RAM + 4GB of virtual RAM. The primary purpose of having virtual RAM is to make memory management better. With apps and games increasing in size, having virtual RAM will help your phone to keep more apps open on the RAM.

Why is virtual memory not as important anymore?

Using virtual memory makes a computer run slower, as the processor has to wait while data is swapped between hard disk and RAM. As secondary storage devices have slower access times than RAM, the computer's processing performance can be severely impaired.

Do you need virtual memory if you have a lot of RAM?

Therefore, no matter how large the capacity of RAM is, it's still necessary for us to enable the virtual memory. Another thing about virtual memory is that Windows only uses paging files when it's necessary. In other words, Windows does not use paging files all the time.

What is the main disadvantage of virtual memory?

- Switching between apps will most likely take a longer time. - Virtual memory reduces the amount of hard disk space available for your usage. - It has a negative impact on system stability. - It enables bigger programs to operate on systems that do not have enough physical RAM to execute them on their own.


3 Answers

Even when the memory usage of all applications fits in physical memory, virtual memory is still useful. VM can provide these features:

  • Privileged memory isolation (every app can't touch the kernel or memory-mapped hardware devices)
  • Interprocess memory isolation (one app can't see another app's memory)
  • Static memory addresses (e.g. every app has main() at address 0x0800 0000)
  • Lazy memory (e.g. pages in the stack are allocated and set to zero when first accessed)
  • Redirected memory (e.g. memory-mapped files)
  • Shared program code (if more than one instance of a program or library is running, its code only needs to be stored in memory once)
like image 88
Nayuki Avatar answered Nov 15 '22 11:11

Nayuki


While not strictly needed in this scenario, virtual memory is about more than just providing "more" memory than is physically available (swapping). For example, it helps avoiding memory fragmentation (from an application point of view) and depending on how dynamic/shared libraries are implemented, it can help to avoid relocation (relocation is when the dynamic linker needs to adapt pointers in a library or executable that was just loaded).

like image 23
DarkDust Avatar answered Nov 15 '22 09:11

DarkDust


A few more points to consider:

  • Buggy apps that don't handle failures in the memory allocation code
  • Buggy apps that leak allocated memory

Virtual memory reduces severity of these bugs.

like image 39
Alexey Frunze Avatar answered Nov 15 '22 11:11

Alexey Frunze