0% found this document useful (0 votes)
29 views

Memory Management

Uploaded by

recu12121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Memory Management

Uploaded by

recu12121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Memory Management:

Memory management is the functionality of an operating system which handles or manages


primary memory and moves processes back and forth between main memory and disk during
execution. Memory management keeps track of each and every memory location, regardless of
either it is allocated to some process or it is free. It checks how much memory is to be allocated to
processes. It decides which process will get memory at what time. It tracks whenever some
memory gets freed or unallocated and correspondingly it updates the status.

Static vs Dynamic Loading


The choice between Static or Dynamic Loading is to be made at the time of computer
program being developed. If you have to load your program statically, then at the time of
compilation, the complete programs will be compiled and linked without leaving any
external program or module dependency. The linker combines the object program with
other necessary object modules into an absolute program, which also includes logical
addresses.

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.

Static vs Dynamic Linking


As explained above, when static linking is used, the linker combines all other modules
needed by a program into a single executable program to avoid any runtime dependency.

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 −

 Low Memory − Operating system resides in this memory.


 High Memory − User processes are held in high memory.

Operating system uses the following memory allocation mechanism.

Memory Allocation & Description


1. Single-partition allocation

In this type of allocation, relocation-register scheme is used to protect user processes


from each other, and from changing operating-system code and data. Relocation
register contains value of smallest physical address whereas limit register contains
range of logical addresses. Each logical address must be less than the limit register.

2. Multiple-partition allocation

In this type of allocation, main memory is divided into a number of fixed-sized


partitions where each partition should contain only one process. When a partition is
free, a process is selected from the input queue and is loaded into the free partition.
When the process terminates, the partition becomes available for another process.

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

External fragmentation can be reduced by compaction or shuffle memory contents to place


all free memory together in one large block. To make compaction feasible, relocation
should be dynamic.
The internal fragmentation can be reduced by effectively assigning the smallest partition
but large enough for the process.

What is a Logical Address?


The logical address is a virtual address created by the CPU of the computer system. The
logical address of a program is generated when the program is running. A group of several
logical address is referred to a logical address space. The logical address is basically used
as a reference to access the physical memory locations.

In computer systems, a hardware device named memory management unit (MMU) is


used to map the logical address to its corresponding physical address. However, the logical
address of a program is visible to the computer user.

What is a Physical Address?


The physical address of a computer program is one that represents a location in the
memory unit of the computer. The physical address is not visible to the computer user. The
MMU of the system generates the physical address for the corresponding logical address.

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.

So, The physical address consists of two parts:

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:

In Operating Systems, Segmentation is a memory management technique in which the memory


is divided into the variable size parts. Each part is known as a segment which can be allocated
to a process.
The details about each segment are stored in a table called a segment table. Segment table is
stored in one (or many) of the segments.

Segment table contains mainly two information about segment:

1. Base: It is the base address of the segment


2. Limit: It is the length of the segment.

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.

You might also like