3

Principles of virtual memory:

The idea of virtual memory is to create a virtual address space that doesn't correspond to actual addresses in RAM. The system stores the official copy of memory on disk and caches only the most frequently used data in RAM. To make this workable, we break virtual memory into chunks called pages; a typical page size is four kilobytes. We also break RAM into page frames, each the same size as a page, ready to hold any page of virtual memory.

I'm running a Linux system and the swap area is empty, because there's enough space in main memory. That being said, is there still a virtual memory with pages and will processes continue to have virtual addresses instead of the physical addresses of their segments in the main memory?

What if there's no swap area in disk, is there a virtual memory in the system too?

In simple words, is virtual memory always available in a Linux system and will process always have virtual addresses?

1
  • 2
    After paging has been turned on in the CPU early in the kernel boot process, all running code uses virtual addresses, including user space code and kernel code. Jul 4, 2017 at 20:14

2 Answers 2

6

Virtual memory (paging) is enabled on CPU level. That means CPU uses MMU to convert virtual address (as it seen by application) to physical address. Memory is split to pages. Page could be either loaded to memory or loaded to disk. If page is on disk then accessing this page leads to page fault which is processed by OS (OS loads page from disk).

So if you have no swap you still have virtual memory, CPU still uses MMU and splits memory into pages but OS can't move those pages to disk.

http://wiki.osdev.org/Paging

4
  • Then the CPU also maintains the page table?
    – direprobs
    Jul 4, 2017 at 20:23
  • Yes, as always. Paging is not only used for swap. It also used for security (W^X/DEP etc). Simply disabling swap does not disable virtual memory.
    – user996142
    Jul 4, 2017 at 20:30
  • That's to say there would be no virtual pages and processes have only page frames in physical memory. Because there's nothing stored at disk. However, those processes are still given virtual addresses. I'm not sure if I got this right. So to some process they could have page frames without having pages?
    – direprobs
    Jul 4, 2017 at 21:02
  • 4
    The term virtual memory does not primarily refer to the fact that pages don't have mappings to physical pages, swapping to backing storage is just a secondary mechanism. The term virtual in this context is related to that each process is presented with its own address space starting from address zero, that is decoupled from the physical address space. This is why it is called virtual. The MMU handles the mapping between virtual and physical addresses, but the operating system maintains a separate mapping for each proecss, and switches the active mapping when scheduling processes. Jul 5, 2017 at 13:17
1

No.

As mentioned by user996142, there are two things called virtual address space. However, the definition you show in your question more specifically talks about the swap space. Either way, I think both statements are rather convoluted.

At the CPU level, you need to have an MMU to have what is called virtual memory addresses. As mentioned by the other user, the computer has physical memory which are given physical addresses. In most cases, physical memory starts at 0 and grows up to the total amount of memory you have. Some architectures like to have memory at the end as well (at addresses -1, -2, -3...) All CPUs do not have MMU chips, especially in embedded systems. Some such systems still use 8 bit CPUs which really can't be using an MMU (these are limited to 64Kb addressable memory anyway).

At the system level, the virtual memory is the swap as you mentioned. Again, this is not necessarily available. In most distribution of Linux, a swap is automatically created at installation time. However, there are systems that do not do it automatically (for example, DigitalOcean offers machines with Ubuntu which do not offer swap space by default...) In most cases, having some swap is a good thing to avoid having processes killed by the OS to restore RAM (and as a result, you have no clue what just happened).

The kernel is responsible for managing all of that for you.

The MMU part is a very low level. For most implementations, it is required to run processes under Linux. Without that feature, you either need processes that can run at any location (that was a fun thing on old Macs running on 68K processors, you'd create blocks of code that were around 64Kb that could be placed anywhere in memory), or have a way to relocate the process...

The swap is a much high layer which allows you to run processes that make use of way more RAM than you have on your computer by putting some of the data on disk. There is a huge penalty in term of speed, of course. In most cases, it's better to run one process, and once it is done, run the next process, etc. to avoid swapping to disk.

As an important aspect, the code of a process (the assembly code, for interpreted languages such as python and php, that would be the interpreter binary) is not changing and available on disk in read-only mode. What that means is: you can swap out part of the code of a process and reload it later as required without the huge penalty of having to swap data found in RAM. Data has to be written to disk and that's slow. Code only needs to be read. So if part of a process code is never executed, that page of code can be swapped out very quickly and reloaded in case it happens to be executed again later. That swap doesn't need the swap disk or swap file. So even if your swap space is 0Kb, the kernel can still swap out the code of running processes to save some RAM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.