Introduction
The OS provides memory management functionality, which allows multiple processes to run simultaneously.
It must be shared fairly, which presents some challenge especially if the physically available is not large enough
Even though it may appear that multiple programs are running simultaneously, in reality only one runs at once, which means the processor must switch between them quickly in order to give the illusion of multitasking.
There are two main types of memory management:
Paging
In modern operating systems, paging splits memory into fixed size physical divisions, usually 4KB in size . They are all equally sized.
Microsoft Windows' pagefile is stored at C:/pagefile.sys
When programs run, they are allocated pages to work in, and they do not have to be contiguous.
Performance
Performance is not affected by having non-contiguous (not next to each other) pages for your program. Any pages being used that are not active can be sent to virtual memory to free pages up for other tasks. Performance may be affected if some programs are split (for example when parts of a looping function are split across pages) Writing to virtual memory is much slower though, and can cause the system to lag
Segmentation
Older OSs use segmentation, which uses logical divisions, which mean that there is no minimum size When a program loads, it is given enough memory for the parts it is currently trying to load. The rest of the program is kept in storage until needed, but the logical section given to the program will need to be made bigger. Programs that are split across logical splits (non-contiguous) suffer greatly for not being together. Segmentation can also use virtual memory to store unused parts of programs in virtual memory to free space for something else.
Virtual Memory
Virtual memory is used when the computer has no more available random access memory (RAM). VM uses a section of the HDD/SSD to act as RAM when the main space is not sufficient.
This is slow though, and an overreliance can cause "disk thrashing" which will wear out the SSD or make it appear to "freeze", as more time is waiting for the IO operation to complete than the program running .