Segmented, Paged and Virtual Memory

Greg's Study Journal
24 followers
Nov 8, 2022
Segmented, Paged and Virtual Memory
This diagram illustrates the essence of a segmented memory management system.
On the left are some of the programs that have been installed on the computer's hard drive.
These are just files on disk until they've been launched.
In the middle is the computer's physical memory, the RAM.
This contains any programs and data that are currently in use.
On the right is a swap file on the computer's hard drive.
Each program that's currently running might be a procedure or a function.
A running instance of a program is called a process.
Needless to say, the operating system itself has a number of
processes in memory when the computer is working.
Suppose the user starts launching applications.
Sooner or later, the memory is going to fill up.
Notice how different processes occupy different amounts of memory.
These portions of memory are known as segments.
They're based simply on the sizes of the programs.
For each process, the operating system knows its starting location and how big it is,
so it knows where everything is.
When a request is made to launch some more software, perhaps a word processing application,
something has to move out of memory to make room for it.
If one of the processors belonging to the presentation software is currently doing nothing,
the operating system might decide to move it to disk,
temporarily at least, so the word processing programs can begin.
When another request is made, perhaps to launch a web browser this time,
another idle process of at least equal size has to be moved out of memory to make way.
In order for the spreadsheet process in the swap file to start running again,
something else of at least equal size has to be swapped out.
Although there is enough free space to allow for this, the free space is fragmented.
In a segmented memory management system, each process is atomic.
It can't be split up. It's all or nothing.
So the spreadsheet process is going to have to wait.
The chances of the large presentation process getting back into memory are now particularly low
because a number of adjacent processors will have to become idle before this can happen.
In summary, in a segmented memory management system,
segments are swapped between disk and main memory as needed.
Program segments correspond to blocks of program code such as procedures or functions.
Data segments correspond to data structures such as stacks, queues or graphs.
Segments vary in size.
The operating system knows the start and size of each segment in physical memory.
Each segment is atomic.
Either the whole segment is in RAM or none of the segment is in RAM.
A segment in memory can only be replaced by a segment of the same size or smaller.
Segmentation can result in memory fragmentation.
A lot of small segments with gaps in between.
Large segments may not be allowed into the memory very often at all.
Segments can be pushed together to limit the amount of fragmentation
and allow large segments to be loaded more often.
With paged memory, as far as any one program is concerned,
it has the whole memory to itself, which of course is not true.
But the operating system allows the program to behave as if this is the case.
A program's view of the memory is called logical memory.
In reality, each program is broken up into a number of equal-sized pages.
Each page is only four kilobytes in size, so a single page might contain a single program,
more than one program, or just part of a program.
These pages could be anywhere in physical memory that the operating system decides to put them.
Indeed, some pages may be on secondary storage.
To make this possible, the operating system maintains a page table
with information about which logical locations map to which physical locations.
As far as any one program is concerned, the memory is a quiet place.
Here's the word processor's view of the memory, if it is also running.
And here's what the spreadsheet application thinks is going on.
The reality of physical memory is, of course, somewhat more complicated.
When yet another program, or indeed data, wants to access the memory,
it may well be loaded in a fragmented state.
If there isn't enough space, some of the other pages will be moved to disk.
This is called paging out.
When a program on disk is required again, it will be paged back in.
Supplementing the RAM with secondary storage like this is called virtual memory.
In a busy system, when the memory is almost full,
there can be so much paging activity that the computer becomes unusably slow.
This is called disk threshing, or disk thrashing.
In summary, with paged memory, the memory is split up into small,
equal-sized sections called pages, or page frames.
A single application might occupy multiple pages, which aren't necessarily contiguous.
Each application program has its own view of the memory.
This is called logical memory.
A page table records where the different pages of a program are located in physical memory.
Unused pages may be paged out to a swap file on disk to make room for others,
and these pages are paged back in when they're needed again.
Supplementing physical memory with secondary storage is known as virtual memory.
When memory is low, excessive swapping can lead to disk threshing,
and this can degrade performance.
So what's better, segmentation or paged memory?
Segmented memory makes an entire block of code available to the processor,
which allows for fast access.
Segmentation, on the other hand, can lead to fragmentation of free space.
With segmented memory, large processors might not get access to the memory very often.
Paged memory, on the other hand, can lead to fragmented processors, which will run more slowly.
However, paged memory makes better use of free space.
Windows uses paged memory.
Segmented memory is not as common.
Some processors support a hybrid of paged and segmented memory management,
in which each segment consists of several fixed-size blocks.
The best of both worlds.
