How Memory is Managed by the OS

Your memory must support both the operating system’s (kernel) processes and user (private) processes. These steps happen when a you start a program.

  1. The operating system creates an address space for the program to run in, which involves setting up appropriate page tables.
  2. The program’s text and data segments are mapped from the executable file on disk to the virtual address space; the text being mapped as read only, the data being mapped as read-write.
  3. The stack and heap are initialized. (Whatever it means..forget it for this moment)
  4. Control is transferred to the program.
  5. The processor tries to access the first instruction. Since it is not in memory, a page fault is generated.
    This causes the operating system to intervene and allocate one or more pages of physical memory and to copy the code from the file. There you get the name: “Demand Paged Virtual Memory.”
  6. The processor then returns control to the program, which continues executing until it encounters another page fault. At this point, step 5 is repeated.
  7. If there is no free physical memory, the operating system will have to
    1. either discard (if read only) or
    2. swap out (write data to swap file if read write) pages before they can be reused.
  8. Typically a least recently used (LRU) algorithm is used for deciding which pages get tossed.
  9. This pattern continues for the life of the program.

When you executes a program, the operating system creates an address space for it to run in. This address space will include the instructions for the program itself, as well as any data it requires.

In a system with memory protection, each process is restricted by the operating system to access only the memory in its own address space. However, the combined program memory requirements often exceed the system’s amount of physical memory installed on a computer. Therefore your operating system uses a portion of the hard disk called a swap file to extend the amount of available memory. This technique, called virtual memory, treats physical memory as a cache of the most recently used data.

In a virtual memory system, memory is divided into units called pages. A page is typically 4-8 KB in size. The set of addresses that identify locations in virtual memory is called the virtual address space (VAS). Each process is allocated a virtual address space. The virtual address space can range from zero to 4 GB on a 32-bit architecture.

A process’s address space contains the set of instructions and data that is mapped into virtual memory when a program is executed. Virtual memory addresses are translated into physical memory addresses through the use of a look-up table, called a page table. In addition to mapping the entire process into virtual memory, a subset of pages is also mapped into physical memory. As each instruction in the process executes, it is either found in physical memory or is not found (called a page fault).

When a page fault occurs, the page that is needed must be moved from the hard disk into physical memory before the instruction can be executed. To make room for the new page, the operating system may need to decide which page to move out of physical memory. This is called swapping. A page fault is time consuming because retrieving data from the hard disk is much slower than obtaining it directly from the physical memory. Operating systems attempt to minimize the number of page faults by swapping multiple pages at once. This becomes a trade-off between operating system overhead and the time saved by minimizing page faults, which is affected by the size of each process’s working set.

The operating system takes care of swapping and translating from virtual address space to physical address space. With a virtual memory scheme, the amount of memory available is seemingly limited only by the amount of hard drive space. Virtual memory has more memory available than physical memory because it removes the restriction that an entire process must be loaded into memory at one time.

Now read (if you did not already) the post about OOM Errors

Why Do I Get Out-of-Memory Errors

Virtual memory refers to the ability of your computer to use your hard drive as if it were the random access memory (RAM) of your computer. This ability is present on all operating systems. Thus, if you have a lot of hard drive space, it might appear that memory is almost unlimited. So why do you get still unpredictable out of memory (OOM) errors?

One cause of OOM errors is that your system has run out of space to hold all of your variables. This means that there is no unallocated virtual address space enough on your computer for programs used by e.g. FSX or P3D and therefore no new variables can be created.

Secondly OOM errors are caused by memory fragmentation. This means that there is memory available, but there is no contiguous piece of memory that is large enough to hold specified variables. When virtual memory is used and freed during normal operation of programs, memory becomes fragmented. This means that the amount of total free memory is greater than or equal to the amount of contiguous free memory. Since variables must be stored in a contiguous block of virtual memory, the largest space, which can be created at a any point in time, is limited by the amount of contiguous free virtual address space.

32-Bit Architecture
For PC’s with 32-bit architectures, the size of a processor instruction (a pointer) can be max. 32 bits long. This limitation implies that the memory addresses can be a maximum of 32 bits long, which results in a maximum of 2^32 possible memory addresses. This translates into 4 gigabytes (GB) of addressable memory. Therefore in a 32-bit architecture, the seemingly unlimited virtual memory space is actually limited to approximately 4 GB. The Windows 32-bit operating system reduces the theoretical limit down to 2 GB of virtual memory due to a design decision to reserve the upper 2 GB of address space. On some versions of Windows, this limit can be moved to allow for an extra 1 GB of memory using the /3GB switch.

64-Bit Architecture
If you are using a 64-bit operating system in conjunction with a 64-bit version of a program, (and FSX nor P3D is not) it is in theory possible to access about 2^64 bytes of memory. This means that the amount of memory that is available to programs, will be limited by the amount of memory available on the computer. As this is typically related to the amount of available virtual memory or page file size, you may want to read here how to manage the virtual memory size.

How to configure the pagefile.

For Windows 7/8

  • Right-click on Computer
  • Select Properties.
  • Select the Advanced System Settings
  • Click on the Advanced tab and then Settings under Performance.
  • In Performance Options click on the Advanced tab
  • and then click on Change under Virtual Memory.

Under Automatically Manage Paging File Size, click on Custom Size to enter the amount of swap space in MB and follow the recommendations in this screen.

Better is to set the checkbox @ Automatically manage paging file size for all drives