| description | 05-30-2023 |
|---|
This is a memory management technique that was especially popular when RAM was very expensive.
Secondary memory can be used as if it were part of the main memory.
It utilizes both hardware and software to enable a computer to compensate for low physical memory.
It works by transferring data from RAM to disk storage.
Mapping chunks of memory to disk files enables a computer to treat secondary memory as if it were main memory.
Sometimes computers would only have 4-8 GB of RAM, and some would argue that is insignificant for a computer nowadays due to the size of programs. This is where virtual memory shines. If the computer was to not have enough RAM to handle the load of the programs, it would initiate and begin sending data to the disks to handle the operations.
Note: They will all increment by one.
Scenario:
Consider this: you, the user, opens Microsoft Word and it loads what it needs (10MB).
Result:
Array[0]->Array[10]
Scenario:
You then load Excel and it loads all the memory it needs (5MB).
Result:
Array[11]->Array[16]
Scenario:
While you are working with Word, you decide to open up a 100MB document.
Result:
Array[17]->Array[117]
How cool is that????
This is done so that way you do not ever get the wrong data in an application. Everything stays where it should be.
Why is this being mentioned here?
- Well, if you try to write or access outside of an array, you will get a segfault
All of our global memory is mixed up and we will need to translate addresses. We will need to also know what parts of our array belong with our Word and Excel process. Otherwise, we will get the wrong data from these processes.
Therefore, what happens if we access outside of that memory?
SEGFAULT!
These errors can occur when a program attempts to access memory outside of the pre-allocated space/memory for the program. This will always result in a segfault.
Also, if a program attempts to use a pointer that is pointing to NULL, it will segfault as well since 0 is an invalid region of memory and address 0 does not exist.
When an application is in use, data from that program is stored in a physical address using RAM. A memory management unit (MMU) will then map the address to RAM and automatically translate addresses. The MMU can for example, map a logical address space to a corresponding physical address.
If/when the RAM space is needed for something more urgent, data can be swapped out of RAM and into virtual memory. The computer's memory manager is in charge of keeping track of the shifts between physical and virtual memory.
If that data is ever needed again, the computer's MMU will use a context switch to resume execution. A context switch is the process of storing the state of the process or thread so that it can be restored and resumed execution at a later point once it is needed again.
A context switch allows multiple processes to be able to share a single CPU.
While copying the virtual memory into physical memory, the OS divides memory with a fixed number of addresses into pagefiles or swap files. Each page is then stored on disk, and when the page is needed, the OS copies it from the disk to main memory and translates the virtual addresses to physical ones using the context switch as mentioned before!
However, this is a lengthy and computationally slow process to swapping from virutal to physical is inefficient. This means that computers with more RAM will always generally be faster.
NOTE: Virtual memory is still volatile even if it is temporarily stored on disk during this process. So if you lost power or crashed, you would lose this data. However, it could be possible to recover from forensics unless it was overwritten.