Memory Management

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Memory Management

Memory Management is an essential function of the Operating System


three different variants of memory to be used inside a computer.

1. Cache Memory: A small size of memory that is incredibly fast, expensive,


and volatile.
2. Random Access Memory: A medium-sized memory of a few gigabytes,
medium-priced, medium-fast, and volatile.
3. HDD or SSD: A large-sized memory of up to a terabyte of storage, cheap,
slow, and nonvolatile.

Memory Manager​ :

Efficiently Manage the Memory by

● keeping track of which parts of memory is in use.


● Allocate memory to processes.
● Free up the memory after the processes have used it.

Need of memory management in OS?


Relocation
Protection
Sharing
Logical Organization
Physical Organization
Logical and Physical Address in Operating System
Logical Address

generated by CPU while a program is running.

The logical address is a virtual address​ as it does not exist physically, therefore, it is also known

as Virtual Address.

This address is used as a reference​ to access the physical memory location by CPU.

The term ​Logical Address Space ​is used for the set of all logical addresses generated by a
program’s perspective.

The hardware device called ​Memory-Management Unit ​is used for mapping logical addresses to
its corresponding physical address.

Physical Address

identifies a physical location of required data in a memory.

The user never directly deals with the physical address but can access by its corresponding logical

address.

The user program generates the logical address and thinks that the program is running in this
logical address but the program needs physical memory for its execution, therefore, the logical

address must be mapped to the physical address by MMU before they are used.

The term Physical Address Space is used for all physical addresses corresponding to the logical

addresses in a Logical address space.


Comparison Chart:

PARAMETER LOGICAL ADDRESS PHYSICAL ADDRESS

Basic generated by CPU location in a memory unit

Logical Address Space is set of Physical Address is set of all

Address all logical addresses generated physical addresses mapped to

Space by CPU in reference to a the corresponding logical

program. addresses.
User can view the logical User can never view physical
Visibility
address of a program. address of program.

Generation generated by the CPU Computed by MMU

The user can use the logical The user can indirectly access

Access address to access the physical physical addresses but not

address. directly.
A page, memory page, or virtual page

● is a fixed-length contiguous block of virtual memory​, described by a single


entry in the page table.
● It is the ​smallest unit of data​ for memory management in a virtual memory
operating system.
● Similarly, a ​page frame​ is the smallest fixed-length contiguous block of physical
memory into which ​memory pages are mapped​ by the operating system.
Types of Memory Allocation
1. Static Memory Allocation
Static memory allocation is performed when the compiler compiles the program and generate
object files, linker merges all these object files and creates a single executable file, and loader loads
this single executable file in main memory, for execution​.

In static memory allocation, the size of the data required by the process must be known
before​ the execution of the process initiates.

Data -- Size Unknown -- Guess -- Larger/Smaller -- Wastage/inconsistent

Memory Allocation -- Before Execution -- Hence faster execution

Static memory allocation provides more ​efficiency​ when compared by the dynamic
memory allocation.
2. Dynamic Memory Allocation
Dynamic memory allocation is performed while the program is in execution. Here, the
memory is allocated to the entities of the program when they are to be used for the ​first
time​ while the program is running.

The actual size, of the data required, is known at the run time so, it allocates the ​exact
memory space to the program thereby, reducing the memory wastage.

Dynamic memory allocation provides ​flexibility​ to the execution of the program. As it


can decide what amount of memory space will be required by the program. If the
program is large enough then a dynamic memory allocation is performed on the
different parts of the program, which is to be used currently. This reduces memory
wastage and improves the performance of the system.

Allocating memory dynamically creates an overhead over the system. Some allocation
operations are performed repeatedly during the program execution creating more
overheads, leading in ​slow​ execution of the program.

Dynamic memory allocation does not require special support from the operating
system.

It is the responsibility of the programmer to design the program in a way to take


advantage of dynamic memory allocation method.

Thus the dynamic memory allocation is flexible but slower than static memory
allocation.
Fragmentation
● After the partitioning/memory allocation of memory comes the fragmentation.
● When the processes are loaded to and removed from memory, the memory space
that gets freed breaks into little pieces.
● Now, when this happens, no further processes can be allotted memory as their
size becomes too small for that, and hence the memory blocks remain unused.
● The solution to this problem is known as fragmentation.
● In other words, fragmentation can be defined as the issue of a memory that arises
when processes are loaded to and removed from the memory, breaking it into
pieces.
● The fragmentation is mainly of two types:
1. External fragmentation – In external fragmentation, the 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 – In internal fragmentation, the memory block
assigned to processes is bigger. Some portion of the memory may be left
unused, as it cannot reside in any other process.
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 segment which can be allocated to a
process.

The details about each segment are stored in a table called as 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.

Why Segmentation is required?


Till now, we were using Paging as our main memory management technique. Paging is more close
to Operating system rather than the User. It divides all the process into the form of pages
regardless of the fact that a process can have some relative parts of functions which needs to be
loaded in the same page.

Operating system doesn't care about the User's view of the process. It may divide the same
function into different pages and those pages may or may not be loaded at the same time into the
memory. It decreases the efficiency of the system.

It is better to have segmentation which divides the process into the segments. Each segment
contain same type of functions such as main function can be included in one segment and the
library functions can be included in the other segment,

Translation of Logical address into physical address by


segment table
CPU generates a logical address which contains two parts:

1. Segment Number

2. Offset

The Segment number is mapped to the segment table. The limit of the respective segment is
compared with the offset. If the offset is less than the limit then the address is valid otherwise it
throws an error as the address is invalid.

In the case of valid address, the base address of the segment is added to the offset to get the
physical address of actual word in the main memory.
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 compare 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