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

OS Memory Defragmentation Process

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 views57 pages

OS Memory Defragmentation Process

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

Some Slides adapted from the book, Operating Systems:

Internals and Design Principles, 6/E


William Stallings

Memory Management
Addressing
The need for memory
management
• Memory is cheap today, and getting
cheaper
– But applications are demanding more and
more memory, there is never enough!
• Memory Management, involves swapping
blocks of data from secondary storage.
• Memory I/O is slow compared to a CPU
– The OS must cleverly time the swapping to
maximise the CPU’s efficiency
Memory Management

Memory needs to be allocated to ensure a


reasonable supply of ready processes to
consume available processor time
Memory Management
Terms
Table 7.1 Memory Management Terms

Term Description
Frame Fixed-length block of main
memory.
Page Fixed-length block of data in
secondary memory (e.g. on disk).
Segment Variable-length block of data that
resides in secondary memory.
Memory Management
Requirements
• Relocation
• Protection
• Sharing
• Logical organisation
• Physical organisation
Requirements: Relocation
• The programmer does not know where the
program will be placed in memory when it
is executed,
– it may be swapped to disk and return to main
memory at a different location (relocated)
• Memory references must be translated to
the actual physical memory address
Requirements: Protection
• Processes should not be able to reference
memory locations in another process
without permission
• Impossible to check absolute addresses at
compile time
• Must be checked at run time
Requirements: Sharing
• Allow several processes to access the
same portion of memory
• Better to allow each process access to the
same copy of the program rather than
have their own separate copy
Requirements: Logical
Organization
• Memory is organized linearly (usually)
• Programs are written in modules
– Modules can be written and compiled
independently
• Different degrees of protection given to
modules (read-only, execute-only)
• Share modules among processes
• Segmentation helps here
Requirements: Physical
Organization
• Cannot leave the programmer with the
responsibility to manage memory
• Memory available for a program plus its
data may be insufficient

• Programmer does not know how much


space will be available
Memory Structure

Memory is a continuous set


of bits referenced by
specific addresses
10-12
Memory Management
Policies/ Schemes
• Single Contiguous Allocation
• Fixed Partitioning
• Dynamic Partitioning
• Relocatable Dynamic Partitioning
• Simple Paging
• Simple Segmentation
• Virtual Memory Paging
• Virtual Memory Segmentation
Memory Management
Schemes – Early Systems

• Single Contiguous Allocation


• Fixed Partitioning
• Dynamic Partitioning
• Relocatable Dynamic Partitioning
Single Contiguous Memory
Management
Single Contiguous Memory
Management
• There are only two
programs in memory
– The operating system
– The application
program
• This approach is
called single
Main memory divided
into two sections
contiguous memory
management
Single Contiguous
Memory Management

• A logical address is simply an integer


value relative to the starting point of the
program
• To produce a physical address, we add a
logical address to the starting address of
the program in physical main memory

10-17
Single Contiguous Memory
Management

binding a logical address to


10-18 a physical one
Partitioning
• An early method of managing memory
– Pre-virtual memory
– Not used much now
• But, it will clarify the later discussion of
virtual memory if we look first at
partitioning
– Virtual Memory has evolved from the
partitioning methods
Fixed Partitioning
Fixed Partitioning
• Equal-size partitions (see fig 7.3a)
– Any process whose size is less than
or equal to the partition size can be
loaded into an available partition
• The operating system can swap a
process out of a partition
– If none are in a ready or running
state
Fixed Partitioning Problems
• A program may not fit in a
partition.
6M
– The programmer must design the
program with overlays
• Main memory use is inefficient.
– Any program, no matter how small, 4M

occupies an entire partition.


– This is results in internal
fragmentation.
Solution – Unequal Size
Partitions
• Lessens both problems
– but doesn’t solve completely
• In Fig 7.3b,
– Programs up to 16M can be
accommodated without overlay
– Smaller programs can be placed in
smaller partitions, reducing internal
fragmentation
Placement Algorithm
• Equal-size
– Placement is trivial (no options)
• Unequal-size
– Can assign each process to the smallest
partition within which it will fit
– Queue for each partition
– Processes are assigned in such a way as to
minimize wasted memory within a partition
Fixed Partitioning
Remaining Problems with
Fixed Partitions
• The number of active processes is limited
by the system
– I.E limited by the pre-determined number of
partitions
• A large number of very small process will
not use the space efficiently
– In either fixed or variable length partition
methods
Dynamic Partitioning
Dynamic Partitioning
• Partitions are of variable length and
number
• Process is allocated exactly as much
memory as required
Dynamic Partitioning
Example
OS (8M)
• External Fragmentation
• Memory external to all
P2
P1
(14M) processes is fragmented
(20M)
Empty (6M) • Can resolve using
Empty
P4(8M)
P2
(56M)
compaction
(14M)
Empty (6M) – OS moves processes so
P3
that they are contiguous
(18M) – Time consuming and
wastes CPU time
Empty (4M)
Dynamic Partitioning
• Operating system must decide which free
block to allocate to a process
• Best-fit algorithm
– Chooses the block that is closest in size to the
request
– Worst performer overall
– Since smallest block is found for process, the
smallest amount of fragmentation is left
– Memory compaction must be done more often
Dynamic Partitioning
• First-fit algorithm
– Scans memory form the beginning and
chooses the first available block that is large
enough
– Fastest
– May have many process loaded in the front
end of memory that must be searched over
when trying to find a free block
Dynamic Partitioning
• Next-fit
– Scans memory from the location of the last
placement
– More often allocate a block of memory at the
end of memory where the largest block is
found
– The largest block of memory is broken up into
smaller blocks
– Compaction is required to obtain a large block
at the end of memory
Allocation
Best-Fit vs. First-Fit
Best-Fit
First-Fit
• More
Increases
complex
memory
algorithm
use
• Searches
Memory allocation
entire table
takes
before
moreallocating
time
• memory
Reduces internal fragmentation
• Results in a smaller “free” space (sliver)

34
Release of Memory Space :
Deallocation
• Deallocation for fixed partitions is simple
– Memory Manager resets status of memory block to
“free”.

• Deallocation for dynamic partitions tries to


combine free areas of memory whenever
possible
– Is the block adjacent to another free block?
– Is the block between 2 free blocks?
– Is the block isolated from other free blocks?

35
Algorithm to Deallocate
Memory Blocks
If job_location is adjacent to 1+ free Else
blocks merge both blocks into one
Then mem_size(counter-1) =
If job_location is between 2 free mem_size(counter-1) +
blocks job_size
Then merge all 3 blocks into 1 Else
block search for null entry in free
mem_size(counter-1) = memory list
mem_size(counter-1) + enter job_size and
job_size beginning_address in entry slot
+ mem_size(counter+1) set its status to “free”
Set status of
mem_size(counter+1) to null
entry
36
Case 1: Joining 2 Free Blocks
Before Deallocation After Deallocation
Beginning Memory Status Beginning Memory Status
address block size address block size
4075 105 Free 4075 105 Free

5225 5 Free 5225 5 Free


6785 600 Free 6785 600 Free
7560 20 Free 7560 20 Free
(7600) (200) (Busy)1 *7600 205 Free
*7800 5 Free 10250 4050 Free
10250 4050 Free 15125 230 Free
15125 230 Free 24500 1000 Free
24500 1000 Free

Understanding Operating Systems 37


Case 2: Joining 3 Free Blocks
Before Deallocation After Deallocation
Beginning Memory Status Beginning Memory Status
address block size address block size
4075 105 Free 4075 105 Free

5225 5 Free 5225 5 Free


6785 600 Free 6785 600 Free
7560 20 Free 7560 245 Free
(7600) (200) (Busy)1 * (null)
*7800 5 Free 10250 4050 Free
10250 4050 Free 15125 230 Free
15125 230 Free 24500 1000 Free
24500 1000 Free

Understanding Operating Systems 38


Case 3: Deallocating an
Isolated Block

Busy List Before Busy List After

Beginning Memory Status Beginning Memory Status


address block size address block size
7805 1000 Busy 7805 1000 Busy
*8805 445 Busy * (null entry)
9250 1000 Busy 9250 1000 Busy

Understanding Operating Systems 39


Relocatable Dynamic Partitions

Understanding Operating Systems 40


Relocatable Dynamic Partitions
• Memory Manager relocates programs to gather all
empty blocks and compact them to make 1 memory
block.

• Memory compaction (garbage collection,


defragmentation) performed by OS to reclaim
fragmented sections of memory space.

• Memory Manager optimizes use of memory &


improves throughput by compacting & relocating.

Understanding Operating Systems 41


Compaction Steps
• Relocate every program in memory so they’re
contiguous.

• Adjust every address, and every reference to an


address, within each program to account for
program’s new location in memory.

• Must leave alone all other values within the


program (e.g., data values).

42
Original Assembly Language
Program
A: EXP 132, 144, 125, 110 ;the data values
BEGIN: MOVEI 1,0 ;initialize register 1
MOVEI 2,0 ;initialize register 2
LOOP: ADD 2,A(1) ;add (A + reg 1) to reg 2
ADDI 1,1 ;add 1 to reg 1
CAIG 1,4-1 ;is reg 1 > 4-1?
JUMPA LOOP ;if not, go to Loop
MOVE 3,2 ;if so, move reg 2 to reg 3
IDIVI 3,4 ;divide reg 3 by 4,
;remainder to register 4
EXIT ;end
END

Understanding Operating Systems 43


Assembly Language Program
Loaded into Memory
000000’ 000000 000132 A: EXP 132,144,125,110
000001’ 000000 000144
000002’ 000000 000125
000003’ 000000 000110

000004’ 201 01 0 00 000000 BEGIN: MOVEI 1,0


000005’ 201 02 0 00 000000 MOVEI 2,0
000006’ 270 02 0 01 000000’ LOOP: ADD 2,A(1)
000007’ 271 01 0 00 000001 ADDI 1,1
000008’ 307 01 0 00 000003 CAIG 1,4-1
000009’ 324 00 0 00 000006’ JUMPA LOOP
000010’ 200 03 0 00 000002 MOVE 3,2
000011’ 231 03 0 00 000004 IDIVI 3,4
000012’ 047 00 0 00 000012 EXIT

000000 END

Understanding Operating Systems 44


Program in Memory During
Compaction & Relocation
• Free list & busy list are updated
– free list shows partition for new block of free memory
– busy list shows new locations for all relocated jobs

• Bounds register stores highest location in memory


accessible by each program.

• Relocation register contains value that must be


added to each address referenced in program so it
can access correct memory addresses after
relocation.

Understanding Operating Systems 45


Fragmentation example
Compaction
Memory Before & After
Compaction

Understanding Operating Systems 48


Contents of relocation register & close-up of Job 4 memory
area (a) before relocation & (b) after relocation and
compaction (Figure 2.6)

Understanding Operating Systems 49


More Overhead is a Problem with
Compaction & Relocation
• Timing of compaction (when, how often) is
crucial.

• Approaches to timing of compaction:


1. Compact when certain percentage of
memory is busy (e.g., 75%).
2. Compact only when jobs are waiting.
3. Compact after certain amount of time.

Understanding Operating Systems 50


Addresses
• Logical
– Reference to a memory location independent
of the current assignment of data to memory.
• Relative
– Address expressed as a location relative to
some known point.
• Physical or Absolute
– The absolute address or actual location in
main memory.
Relocation
Registers Used during
Execution
• Base register
– Starting address for the process
• Bounds register
– Ending location of the process
• These values are set when the process is
loaded or when the process is swapped in
Registers Used during
Execution
• The value of the base register is added to
a relative address to produce an absolute
address
• The resulting address is compared with
the value in the bounds register
• If the address is not within bounds, an
interrupt is generated to the operating
system
Memory Management
• Early schemes were limited to storing entire
program in memory.
– Fragmentation.
– Overhead due to relocation. Problems

• More sophisticated memory schemes now that:


– Eliminate need to store programs contiguously.
– Eliminate need for entire program to reside in memory
during execution.

55
Key Terms
• address • first-fit memory
• best-fit memory allocation
allocation • fixed partitions
• bounds register • internal
• compaction fragmentation
• deallocation • multiprogramming
• dynamic partitions • relocatable
• external dynamic partitions
fragmentation • relocation
Understanding Operating Systems 56
Resources
• Understanding Operating Systems
– Ida Flynn

• Operating Systems: Internals and Design


Principles, 6/E
– William Stallings

You might also like