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

Memory Management

Uploaded by

gaya3 sankar
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)
28 views

Memory Management

Uploaded by

gaya3 sankar
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/ 50

MODULE IV

MEMORY MANAGEMENT
Concept of address spaces, Swapping, Contiguous memory allocation,
fixed and variable partitions, Segmentation, Paging. Virtual memory,
Demand paging, Page replacement algorithms.
Main Memory

• Memory is central to the operation of a modern computer system.


• Memory consists of a large array of bytes, each with its own address.
• The CPU fetches instructions from memory according to the value of the program
counter. (CPU register in the computer processor which has the address of the next
instruction to be executed from memory.)
typical instruction-execution cycle:
1. first fetches an instruction from memory.
2. The instruction is then decoded and may cause operands to be
fetched from memory.
3. After the instruction has been executed on the operands, results
may be stored back in memory.
• the memory unit sees only a stream of memory addresses
• it does not know how they are generated or what they are for
MEMORY MANAGEMENT

• The task of subdividing the memory among different processes is


called memory management.
• Memory management is a method in the operating system to manage
operations between main memory and disk during process execution.
• The main aim of memory management is to achieve efficient
utilization of memory.
• To achieve a degree of multiprogramming and proper utilization of
memory, memory management is important.
Why Memory Management is required?
• Allocation
• Deallocation
• Security
• 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.
Goals of Memory Management
• Maximum utilization of space
• Ability to run larger programs with limited space
Memory Management Techniques

Single
Contiguous
I. Contiguous memory management:
• Each program occupies a single contiguous block of storage
locations, i.e., a set of memory locations with consecutive
addresses.

II. Non-Contiguous memory management :


• The program is divided into different blocks and loaded at different
portions of the memory that need not necessarily be adjacent to
one another.
1. Single contiguous memory management
schemes
• simplest memory management scheme used in the earliest
generation of computer systems.
• main memory is divided into two contiguous areas or partitions.
• operating systems reside permanently in one partition, generally at the lower
memory.
• user process is loaded into the other partition.
• once a process is loaded, it is given full processor's time, and no other
processor will interrupt it.
• does not support multiprogramming
• i.e., it cannot handle multiple programs simultaneously.
2. Multiple Partitioning

• The operating system divide the available main


memory into multiple parts to load multiple
processes into the main memory.
• Thus, multiple processes can reside in
the main memory simultaneously.
Multiple Partitioning schemes can be of two types:
a. Fixed / Static Partitioning
b. Variable / Dynamic Partitioning
a. Fixed / Static Partitioning
• main memory is divided into several fixed-sized partitions.
• These partitions can be of the same size or different sizes.
• Each partition can hold a single process.
• The number of partitions determines the degree of
multiprogramming.
• i.e., maximum number of processes in memory.
Partition Allocation Policy
• First Fit
• Best Fit
• Worst Fit
• Next Fit
First Fit Algorithm
• First Fit algorithm scans the linked list and whenever it finds the first
big enough hole to store a process, it stops scanning and load the
process into that hole.
• Note:
hole=free space
Best Fit Algorithm
• tries to find out the smallest hole possible in the list that can
accommodate the size requirement of the process.
• Its slower because it scans the entire list every time and tries to find
out the smallest hole which can satisfy the requirement the process.
Worst Fit Algorithm
• scans the entire list every time and tries to find out the biggest hole in
the list which can fulfill the requirement of the process.
Next Fit Algorithm
• Similar to First Fit Algorithm except the fact that, it scans the linked
list from the node where it previously allocated a hole.
• Doesn’t scan the whole list, it starts scanning the list from the next
node.
Internal fragmentation
Limitation to Fixed / Static Partitioning
• occurs when the memory is distributed into fixed-sized blocks.
• If the memory allocated to the process is slightly larger than the
memory demanded, then the difference between allocated and
demanded memory is known as internal fragmentation.
• To overcome this we use Variable / Dynamic Partitioning.
b. Variable / Dynamic Partitioning
• Initially RAM is empty, and partitions are made during the run-time
according to process’s need instead of partitioning during system
configure.
• The size of partition will be equal to incoming process initially.
• The partition size varies according to the need of the process so that
the internal fragmentation can be avoided to ensure efficient
utilization of RAM.
• Number of partitions in RAM is not fixed and depends on the number
of incoming process and Main Memory’s size.
External Fragmentation
• Limitation to Variable / Dynamic
Partitioning
• To overcome this we use compaction
and Non-Contiguous memory
management .
Fragmentation
• When the processes are loaded and removed from the memory, they create free
space or hole in the memory and these small blocks cannot be allocated to new
upcoming processes and results in inefficient use of memory.
• This problem is called fragmentation.
• There are two types of fragmentation:
a. Internal Fragmentation
• The process is allocated a memory block of size more than the size of that process.
• Due to this some part of the memory is left unused and this cause internal
fragmentation
b. External Fragmentation
• Although we have total space available that is needed by a process, still we are not
able to put that process in the memory because that space is not contiguous.
• This is called external fragmentation.
Compaction
• a method by which external fragmentation can be reduced by
merging all the free memory together in one large block
• This technique is also called defragmentation.
• This larger block of memory is then used for allocating space
according to the needs of the new processes.
Problem
Solution
Memory Management
• Basic Hardware
• Main memory and the registers built into the processor itself are the
only general-purpose storage that the CPU can access directly.
• There are machine instructions that take memory addresses as
arguments, but none that take disk addresses.
• Therefore, any instructions in execution, and any data being used by
the instructions, must be in one of these direct-access storage
devices.
• If the data are not in memory, they must be moved there before the
CPU can operate on them.
• Registers that are built into the CPU are generally accessible within
one cycle of the CPU clock.
• Completing a memory access may take many cycles of the CPU clock.
• In such cases, the processor normally needs to stall (stop or delay),
since it does not have the data required to complete the instruction
that it is executing.
• The remedy is to add fast memory called cache between the CPU and
main memory, typically on the CPU chip for fast access.
• To manage a cache built into the CPU, the hardware automatically
speeds up memory access without any operating-system control.
Basic hardware
Protection
• For proper system operation we must protect the operating system from
access by user processes.
• On multiuser systems, we must additionally protect user processes from
one another.
• This protection must be provided by the hardware because the operating
system doesn’t usually intervene between the CPU and its memory
accesses.
• Separate per-process memory space protects the processes from each
other and is fundamental to having multiple processes loaded in memory
for concurrent execution.
• To separate memory spaces we need the ability to determine the range
of legal addresses that the process may access and to ensure that the
process can access only these legal addresses.

• We can provide this protection by using two registers :

• a base register : holds the smallest legal physical memory address


• a limit register : specifies the size of the range.
• For example, if the base register holds 300040 and the limit register is 120900,
then the program can legally access all addresses from 300040 through 420939
(inclusive).
• Protection of memory space is accomplished by having the CPU hardware
compare every address generated in user mode with the registers.
• Any attempt by a program executing in user mode to access
operating-system memory or other users’ memory results in a trap to
the operating system, which treats the attempt as a fatal error.
• This scheme prevents a user program from (accidentally or
deliberately) modifying the code or data structures of either the
operating system or other users.
Multistep Processing of a User Program
Addresses
• The symbolic addresses are the addresses used in a source program. The
variable names, symbolic constants and instruction labels are the basic
elements of the symbolic address space.

• A compiler will typically bind these symbolic addresses to relocatable


addresses (such as "14 bytes from the beginning of this module").

• The physical /absolute address consists of the final address generated


when the program is loaded and ready to execute in physical memory; the
loader generates these addresses.
Eg: 74014
• Address Binding
• Usually, a program resides on a disk as a binary executable file.
• To be executed, the program must be brought into memory and placed
within a process.
• The processes on the disk that are waiting to be brought into memory for
execution form the input queue.
• Addresses in the source program are generally symbolic .
• A compiler typically binds these symbolic addresses to relocatable
addresses.
• Each binding is a mapping from one address space to another.
• Classically, the binding of instructions and data to memory addresses can
be done at any step along the way:
1.Compile time. If you know at compile time where the process will reside in
memory, then absolute code can be generated. For example, if you know
that a user process will reside starting at location R, then the generated
compiler code will start at that location and extend up from there.
2.Load time. If it is not known at compile time where the process will reside
in memory, then the compiler must generate relocatable code. In this case,
final binding is delayed until load time. If the starting address changes, we
need only reload the user code to incorporate this changed value.
3.Execution time. If the process can be moved during its execution from one
memory segment to another, then binding must be delayed until run time.
Special hardware must be available for this scheme to work. Most general-
purpose operating systems use this method.
• Logical Versus Physical Address Space
• An address generated by the CPU is referred to as a logical address, whereas an
address seen by the memory unit —that is, the one loaded into memory-
address register of the memory is referred to as a physical address.
• The compile-time and load-time address-binding methods generate identical
logical and physical addresses.
• The execution-time address binding scheme results in differing logical and
physical addresses.
• In this case, logical address is referred to as a virtual address.
• The set of all logical addresses generated by a program is a logical address
space.
• The set of all physical addresses corresponding to these logical addresses is a
physical address space.
• Thus, in the execution-time address-binding scheme, the logical and physical
address spaces differ.
• The run-time mapping from virtual to physical addresses is done by a hardware device
called the memory-management unit (MMU).
• The base register is now called a relocation register.
• The value in the relocation register is added to every address generated by a user
process at the time the address is sent to memory.

• For example, if the base is at 14000, then an


attempt by the user to address location 0 is
dynamically relocated to location 14000.
• An access to location 346 is mapped to
location 14346.
Dynamic Loading
• It has been necessary for the entire program and all data of a process to be in
physical memory for the process to execute.
• The size of a process has thus been limited to the size of physical memory.
• To obtain better memory-space utilization, we can use dynamic loading.
• The advantage of dynamic loading is that a routine is loaded only when it is
needed
• Routine is not loaded until it is called
• Better memory-space utilization; unused routine is never loaded
• All routines kept on disk in relocatable load format
• Useful when large amounts of code are needed to handle infrequently occurring cases
• No special support from the operating system is required
• Implemented through program design
• OS can help by providing libraries to implement dynamic loading
Dynamic linking
• Static linking – Copying all the required system libraries used in the program into the final
executable code. (Both calling program and called program)
• Dynamic linking libraries –system libraries that are linked to user programs when the programs
are run.
• Operating system checks if routine is in processes’ memory address
• If not in address space, add to address space
Swapping
• A process must be in memory to be executed.
• A process, however, can be swapped temporarily out of memory to a
backing store and then brought back into memory for continued
execution.
• Swapping makes it possible for the total physical address space of all
processes to exceed the real physical memory of the system.
• It increases the degree of multiprogramming in a system.
• Standard Swapping
• Standard swapping involves moving processes between main memory and a
backing store.
• The backing store is commonly a fast disk.
• It must be large enough to accommodate copies of all memory images for all
users, and it must provide direct access to these memory images.
• The system maintains a ready queue consisting of all processes whose memory
images are on the backing store or in memory and are ready to run.
• Whenever the CPU scheduler decides to execute a process, it calls the dispatcher.
• The dispatcher checks to see whether the next process in the queue is in memory.
• If it is not, and if there is no free memory region, the dispatcher swaps out a
process currently in memory and swaps in the desired process.
• It then reloads registers and transfers control to the selected process.
• The context-switch time in such a swapping system is fairly high.
• Standard swapping is not used in modern operating systems.
• It requires too much swapping time and provides too little execution time
to be a reasonable memory-management solution.
• Modified versions of swapping, however, are found on many systems,
including UNIX, Linux, and Windows.
• In one common variation, swapping is normally disabled but will start if
the amount of free memory (unused memory available for the operating
system or processes to use) falls below a threshold amount.
• Swapping is halted when the amount of free memory increases.
• Another variation involves swapping portions of processes—rather than
entire processes—to decrease swap time.
• Swapping on Mobile Systems
• Mobile systems typically do not support swapping in any form.
• Mobile devices generally use flash memory rather than more spacious hard disks
as their persistent storage.
• Instead of using swapping, when free memory falls below a certain threshold,
Apple’s iOS asks applications to voluntarily relinquish allocated memory.
• Read-only data (such as code) are removed from the system and later reloaded
from flash memory if necessary.
• Any applications that fail to free up sufficient memory may be terminated by the
operating system.
• Android does not support swapping and adopts a strategy similar to that used by
iOS.
• It may terminate a process if insufficient free memory is available.
• Before terminating a process, Android writes its application state to flash
memory so that it can be quickly restarted.
• Both iOS and Android support paging.

You might also like