Memory Management
Memory Management
If you are writing a Dynamically loaded program, then your compiler will compile the
program and for all the modules which you want to include dynamically, only references
will be provided and rest of the work will be done at the time of execution.
At the time of loading, with static loading, the absolute program (and data) is loaded into
memory in order for execution to start.
If you are using dynamic loading, dynamic routines of the library are stored on a disk in
relocatable form and are loaded into memory only when they are needed by the program.
When dynamic linking is used, it is not required to link the actual module or library with
the program, rather a reference to the dynamic module is provided at the time of
compilation and linking. Dynamic Link Libraries (DLL) in Windows and Shared Objects in
Unix are good examples of dynamic libraries.
Swapping
Swapping is a mechanism in which a process can be swapped temporarily out of main
memory (or move) to secondary storage (disk) and make that memory available to other
processes. At some later time, the system swaps back the process from the secondary
storage to main memory.
Though performance is usually affected by swapping process but it helps in running
multiple and big processes in parallel and that's the reason Swapping is also known as a
technique for memory compaction.
The total time taken by swapping process includes the time it takes to move the entire
process to a secondary disk and then to copy the process back to memory, as well as the
time the process takes to regain main memory.
Let us assume that the user process is of size 2048KB and on a standard hard disk where
swapping will take place has a data transfer rate around 1 MB per second. The actual
transfer of the 1000K process to or from memory will take
2048KB / 1024KB per second
= 2 seconds
= 2000 milliseconds
Now considering in and out time, it will take complete 4000 milliseconds plus other
overhead where the process competes to regain main memory.
Memory Allocation
Main memory usually has two partitions −
2. Multiple-partition allocation
Fragmentation
As processes are loaded and removed from memory, the free memory space is broken into
little pieces. It happens after sometimes that processes cannot be allocated to memory
blocks considering their small size and memory blocks remains unused. This problem is
known as Fragmentation.
Fragmentation is of two types −
1. External fragmentation
Total memory space is enough to satisfy a request or to reside a process in it, but it is not
contiguous, so it cannot be used.
2. Internal fragmentation
Memory block assigned to process is bigger. Some portion of memory is left unused, as it
cannot be used by another process.
The following diagram shows how fragmentation can cause waste of memory and a
compaction technique can be used to create more free memory out of fragmented memory
−
The physical address is accessed through the corresponding logical address because a user
cannot directly access the physical address. For running a computer program, it requires a
physical memory space. Therefore, the logical address has to be mapped with the physical
address before the execution of the program.
Paging:
Paging is a memory management scheme that eliminates the need for a contiguous allocation
of physical memory. The process of retrieving processes in the form of pages from the
secondary storage into the main memory is known as paging. The basic purpose of paging is to
separate each procedure into pages. Additionally, frames will be used to split the main
memory. This scheme permits the physical address space of a process to be non – contiguous.
In paging, the physical memory is divided into fixed-size blocks called page frames, which are
the same size as the pages used by the process. The process’s logical address space is also
divided into fixed-size blocks called pages, which are the same size as the page frames. When a
process requests memory, the operating system allocates one or more page frames to the
process and maps the process’s logical pages to the physical page frames.
The mapping between logical pages and physical page frames is maintained by the page table,
which is used by the memory management unit to translate logical addresses into physical
addresses. The page table maps each logical page number to a physical page frame number.
Page Number is used to specify the specific page of the process from which the
CPU wants to read the data. and it is also used as an index to the page table.
and * Page offset is mainly used to specify the specific word on the page that the CPU wants
to read.
Page Table:
The Page table mainly contains the base address of each page in the Physical memory. The
base address is then combined with the page offset in order to define the physical memory
address which is then sent to the memory unit.
Thus page table mainly provides the corresponding frame number (base address of the frame)
where that page is stored in the main memory.
As we have told you above that the frame number is combined with the page offset and forms
the required physical address.
1. Page offset(d)
2. Frame Number(f)
where,
The Frame number is used to indicate the specific frame where the required page is stored.
and Page Offset indicates the specific word that has to be read from that page.
The Page size (like the frame size) is defined with the help of hardware. It is important to note
here that the size of the page is typically the power of 2 that varies between 512 bytes and 16
MB per page and it mainly depends on the architecture of the computer.
If the size of logical address space is 2 raised to the power m and page size is 2 raised to the
power n addressing units then the high order m-n bits of logical address designates the page
number and the n low-order bits designate the page offset.
The logical address is as follows:
where p indicates the index into the page table, and d indicates the displacement within the
page.
Disadvantages of paging:
1. Internal fragmentation is caused on older systems.
2. The memory lookup time is more in paging compared to segmentation.
3. Additional memory is consumed by the page tables.
4. Multi-level paging can cause memory reference overhead.
Segmentation:
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compared to the page table in paging.
Disadvantages
1. It can have external fragmentation.
2. it is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.