Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual Memory

Most of the literature on Virtual Memory point out that the as a Application developer,understanding Virtual Memory can help me in harnessing its powerful capabilities. I have been involved in developing applications on Linux for sometime but but didn't care about Virtual Memory intricacies while I code. Am I missing something? If so, please shed some light on how I can leverage the workings of Virtual Memory. Else let me know if am I not making sense with the question!

like image 879
Prabhu. S Avatar asked Feb 04 '09 08:02

Prabhu. S


People also ask

What is virtual memory with example?

Example of virtual memoryA business owner uses their computer's virtual memory system when running multiple applications simultaneously. The user tries to load their email in their browser window while also running word processing software, shift scheduling software and a content management system.

What is the purpose of virtual memory?

Virtual memory enables data that is in RAM and not currently being used to be transferred to the hard disk. This frees up room in RAM for other programs and data. When the data on the hard disk is needed again, any other unused data is transferred to the hard disk before the original data is transferred back to RAM.

Is virtual memory faster than RAM?

The key difference between virtual memory and physical memory is that RAM is very much faster than virtual memory. So a system with 2 GB of physical RAM and 2 GB of virtual memory will not offer the same performance as a similar system with 4 GB of physical RAM.

How much virtual memory should 8gb RAM have?

To calculate the "general rule" recommended size of virtual memory in Windows 10 per the 8 GB your system has, here's the equation 1024 x 8 x 1.5 = 12288 MB. So it sounds as if the 12 GB configured in your system currently is correct so when or if Windows needs to utilize the virtual memory, the 12 GB should suffice.


2 Answers

Well, the concept is pretty simple actually. I won't repeat it here, but you should pick up any book on OS design and it will be explained there. I recommend the "Operating System Concepts" from Silberscahtz and Galvin - it's what I had to use in the University and it's good.

A couple of things that I can think of what Virtual Memory knowledge might give you are:

  • Learning to allocate memory on page boundaries to avoid waste (applies only to virtual memory, not the usual heap/stack memory);
  • Lock some pages in RAM so they don't get swapped to HDD;
  • Guardian pages;
  • Reserving some address range and committing actual memory later;
  • Perhaps using the NX (non-executable) bit to increase security, but im not sure on this one.
  • PAE for accessing >4GB on a 32-bit system.

Still, all of these things would have uses only in quite specific scenarios. Indeed, 99% of applications need not concern themselves about this.

Added: That said, it's definately good to know all these things, so that you can identify such scenarios when they arise. Just beware - with power comes responsibility.

like image 88
Vilx- Avatar answered Sep 28 '22 02:09

Vilx-


It's a bit of a vague question.

The way you can use virtual memory, is chiefly through the use of memory-mapped files. See the mmap() man page for more details.

Although, you are probably using it implicitly anyway, as any dynamic library is implemented as a mapped file, and many database libraries use them too.

The interface to use mapped files from higher level languages is often quite inconvenient, which makes them less useful.

The chief benefits of using mapped files are:

  • No system call overhead when accessing parts of the file (this actually might be a disadvantage, as a page fault probably has as much overhead anyway, if it happens)
  • No need to copy data from OS buffers to application buffers - this can improve performance
  • Ability to share memory between processes.

Some drawbacks are:

  • 32-bit machines can run out of address space easily
  • Tricky to handle file extending correctly
  • No easy way to see how many / which pages are currently resident (there may be some ways however)
  • Not good for real-time applications, as a page fault may cause an IO request, which blocks the thread (the file can be locked in memory however, but only if there is enough).
like image 41
MarkR Avatar answered Sep 28 '22 04:09

MarkR