0% found this document useful (0 votes)
16 views41 pages

Dynamic Memory MGT Class - 2023-24

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

Dynamic Memory MGT Class - 2023-24

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

Memory Management

Memory Management
 Fundamental purpose of any program is to manipulate data
and storage in computer memory.
 Memory is required to store programs into memory before
execution.
 When program execution starts it takes memory through OS
and release of memory is return to OS. All these operation is
done by OS.
 While designing a program the programmer should
concentrate on memory allocation and release the memory
when it no longer used.
Memory Management
The memory management perform following task:
1. It searches for the requested block of memory and allocate if
available.
2. Supervise the release block of memory
3. Combine tiny block of memory to bigger one.

There are two types of memory management:


 1. Static memory management
 2. Dynamic memory management
Static Memory Management
 Compile time memory allocation to program.
 Here the memory allocated is fixed and can’t changes during
execution of the program.
For example
1. int i, j; //Two bytes per integer variables
2. float a[5], f; //Four bytes per (total 6) floating point variables
Static Memory Management
Disadvantages
 Once the memory is allocated it can’t be used by another
program.
 There may be wastage of memory if all the allocated memory is
not in use.

The dynamic or run time memory management helps us to


overcome this problem. I
Dynamic Memory Management
 The runtime allocation of memory to program.
 It allow user to manage memory during execution of program.
The memory is allocated according to the user request and
release whenever it is not in used which is performed by O/S.
 This is efficient for multiprogramming and single user
environment in which more than one program execute
simultaneously.
Example: Linked list, tree
Memory Management
C provides the following dynamic allocation and de-allocation
functions
Memory Representation
 The program instructions , global and static variable are
stored in a permanent storage area and local variables are
stored in stack.
 The memory that is located between these two regions is called
heap (free block) used for dynamic memory management.
 The size of heap keeps changing during program execution.
 The free block of memory is non-contiguous block of memory.
They can be accessed in sequence by pointer from one block to
another block.
Memory block allocation technique
1. Fixed block allocation
 In this each block of fixed size. The user can decide the size
of block.
 The O/S keep pointer called AVAIL that point to first free
block.
There are two functions Getnode() and Freenode() and using
these function the user program communicate with O/S.
 The Getnode() used to obtain the memory from heap
through O/S. The O/S return pointer to allocated block.
 Freenode() used to release to memory.
Fixed block allocation

Example: if size of each block is 1KB and a program


request the memory of 1.2 KB.
In this situation the memory allocated is 2KB. Hence extra
memory is allocated but it is not used.
Internal fragmentation:
In fixed block allocation, there is internal fragmentation.
There are unused block allocated internally, which can’t
be used by another program.
2. Variable block Partitioning
 Initially, the whole memory is free and it is considered as
one large block.
 A block of requested size is allocated by calling the
Getnode() function many times.
 When a process / program request for the memory, the OS
searches for the block of free memory and allocate then.
 If a block becomes free, then the OS tries to merge it with its
neighbors if they are also free.
Variable Block Partitioning: example

Chapter 9 12
Variable Block Partitioning: example

Problem of External fragmentation


Storage Allocation
Decide which free block to allocate
to a program
Types:
1. First-fit: choose first free block
from beginning then allocate
them.
2. Next-fit: choose first free block from
last allocation that fits to the
requested size.
3. Best-fit: choose smallest free
block that is close to requested
block size. (also worst fit)

4. Worst-fit: use largest free block


and allocate them.
Dynamic Storage Allocation

First-fit allocation
 Begins to scan the memory from starting of memory. This
allocation is fast.
 Chooses the block that is big enough size to the request
block size.
 This produces the smallest left over block.
 This is fast
Dynamic Storage Allocation

2. Best-fit allocation
1. Chooses the block that is closest in requested block
size.
2. It requires more time for searching all the list to find the
free block.
3. Since smallest block is allocated, the smallest fragment
is left.
4. Memory compaction must be done more often
Dynamic Storage Allocation

3. Next-fit allocation
1. The OS searches the memory from the last allocation &
chooses the next available block that is large enough.
2. Results in quick fragmentation of the largest block of
free memory which usually appears at the end of the
memory.
3. Compaction is required to obtain a large block at the end
of memory.
Dynamic Storage Allocation

4. Worst-fit allocation
1. Scans the whole memory to find the biggest block
and allocate them.
2. Results in Block of substantial size are left.
3. Less fragmentation.
First fit allocation
OS
Initial memory P1 12 KB
mapping
<FREE> 10 KB

P2 20 KB
<FREE> 16 KB

P3 6 KB
<FREE> 4 KB

19
first fit

OS OS

P1 12 KB P1 12 KB
P4 of 3KB
P4 of <FREE> 10 KB P4 3 KB
allocated
3KB here by <FREE> 7 KB
arrives FIRST FIT

P2 20 KB P2 20 KB

<FREE> 16 KB <FREE> 16 KB

P3 6 KB P3 6 KB

<FREE> 4 KB <FREE> 4 KB

20
first fit
OS
OS
P1 12 KB
P1 12 P5 of 15 KB
P5 of KB allocated here P4 3 KB
15KB by <FREE> 7 KB
P4 3
arrive KB FIRST FIT
s
<FREE> 7
P2 20 KB
KB
P5 15 KB

P2 20 <FREE> 1 KB
KB
<FREE> 16 P3 6 KB
KB
<FREE> 4 KB

P3 6
KB 21
Best fit Allocation

 Allocate the smallest block among those that are large


enough for the new process.
 In this method, the OS search the entire list, or it can keep it
sorted and stop when it hits an entry which has a size larger
than the size of new process.
 This algorithm produces the smallest left over block.
 However, it requires more time for searching all the list or
sorting it

22
best fit
OS
P1 12
Initial KB
memor
y <FREE> 10
KB
mappin
g

P2 20
KB
<FREE> 16
KB

P3 6 KB
<FREE> 4
23
KB
best fit
OS OS
P1 12 KB P1 12 KB
P4 of 3KB
P4 of <FREE> 10 <FREE> 10 KB
loaded here
3KB KB
by
arrives BEST FIT
P2 20 KB
P2 20 KB
<FREE> 16 KB
<FREE> 16
KB

P3 6 KB

P3 6 KB P4 3 KB

<FREE> 4 <FREE> 1 KB
KB
24
best fit
OS
OS
P1 12 KB
P1 12 KB P5 of 15 KB
loaded here <FREE> 10 KB
P5 of <FREE> 10
15KB KB by
arrives BEST FIT
P2 20 KB

P2 20 KB P5 15 KB

<FREE> 16 <FREE> 1 KB
KB
P3 6 KB
P4 3 KB
P3 6 KB <FREE> 1 KB
P4 3 KB
<FREE> 1 KB 25
worst fit

Worst Fit :

 Allocate the largest block among those that are large


enough for the new process.

 Again a search of the entire list is needed.

 This algorithm produces the largest over block.

26
worst fit
OS
P1 12 KB
Initial <FREE> 10 KB
memory
mapping

P2 20 KB
<FREE> 16 KB

P3 6 KB
<FREE> 4 KB

27
worst fit
OS OS
P1 12 KB P1 12 KB
P4 of 3KB
P4 of <FREE> 10 <FREE> 10 KB
Loaded
3KB KB
here by
arrives WORST FIT
P2 20 KB
P2 20 KB
P4 3 KB
<FREE> 16
KB <FREE> 13 KB

P3 6 KB

P3 6 KB <FREE> 4 KB

<FREE> 4 KB

28
worst fit

OS
P1 12 KB
No place <FREE> 10 KB
to load
P5 of 15K

P2 20 KB
P4 3 KB
<FREE> 13 KB

P3 6 KB
<FREE> 4 KB

29
worst fit

OS
P1 12 KB

No place to <FREE> 10 KB
load P5 of 15K

P2 20 KB
P4 3 KB
<FREE> 13 KB

Compaction
is needed !! P3 6 KB
<FREE> 4 KB

30
Compaction
 External fragmentation can be resolved through
compaction.
 Compaction mean to move all allocated block to one side of
memory and all small sizes free block to another side of
memory and merge them into larger block so that a request
can be fulfilled.
 Compaction algorithm is expensive, but so is not making
efficient use of memory, especially with a lot of concurrent
processes.

Chapter 9 31
compaction
OS
P1 12 KB
Memory <FREE> 10 KB
mapping
before
compacti
on P2 20 KB
P4 3 KB
<FREE> 13 KB

P3 6 KB
<FREE> 4 KB

32
compaction
OS OS
P1 12 P1 12 KB
KB
P2 20 KB Swap in
P2

Secondary
storage
P2 20 Swap out
KB P4 3 KB
P2
P4 3
KB
P3 6 KB

P3 6
KB

33
compaction

OS
P1 12 KB
P2 20 KB

Secondary
storage
P4 3 KB
Swap out
P4

P3 6 KB

34
compaction

OS
P1 12 KB
P2 20 KB
P4 3 KB

Swap in Secondary
P4 with a storage
different
starting
address

P3 6 KB

35
compaction

OS
P1 12 KB
P2 20 KB
P4 3 KB

Secondary
storage
Swap out
P3

P3 6 KB

36
compaction

OS
P1 12 KB
P2 20 KB
P4 3 KB Swap in
P3
P3 6 KB
Secondary
storage

37
compaction

OS
P1 12 KB
P2 20 KB
P4 3 KB
P3 6 KB
P5 12 KB
<FREE> 12 KB P5 of 15KB is
loaded

38
Garbage collection
 The deallocation of unused memory is called garbage
collection. The deallocation is done by executing the specific
function.
 The free() in C and delete() in C++ are used to release the
memory. Java have automatic garbage collection.
The garbage collector check the entire memory, marked
unused node and release the associated memory.
Garbage collection
Garbage collection consist of two phases:

Marking phase: In this process, all accessible nodes are


marked and one field is kept for marking. This field is marked
with True/False.
Collection Phase: In this process, memory allocated of all the
nodes, which are not marked are released.
Garbage collection is applied when there is not much free
space available.
Memory Management

Thanks

You might also like