Memory Management. Process must be loaded into memory before being executed. Memory needs to be...

Preview:

Citation preview

Memory Management

Memory ManagementProcess must be loaded into memory before

being executed.Memory needs to be allocated to ensure a

reasonable supply of ready processes to consume available processor time

Memory Management RequirementsMemory Management Techniques determine:

How the memory is to be (logically) subdivided? Where and how a process resides in memory? How addressing is performed? How process can be relocated? How memory is to be protected? How memory can be shared by processes? How to logical and physically organize memory

Requirements Minimize executable memory access time Maximize executable memory size Executable memory must be cost-effective

Memory Requirements of a Process

Mono-programming

Multi-Programming

Creating an Executable Program

AddressesThe 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.

The compiler converts a symbolic addresses into a relative/fixed address.

The physical address consists of the final address generated when the program is loaded and ready to execute in physical memory; the loader generates these addresses.

Binding of Instructions and Data to MemoryAddress binding of instructions and data to memory

addresses can happen at three different stages.Compile time: If memory location known a priori,

absolute code can be generated; must recompile code if starting location changes.

Load time: The compiler must generate relocatable code if memory location is not known at compile time. The final binding is delayed until load time.

Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e.g., base and limit registers).

Absolute Object Module Code Segment Relative Address Generated Code 0000 (Other modules) ... 1008 entry proc_a ... 1220 load =7, R1 1224 store R1, 0136 1228 push 1036 1232 call 2334 ... 1399 (End of proc_a) ... (Other modules) 2334 entry put_record ... 2670 (optional symbol table) ... 2999 (last location in the code segment) Data Segment Relative Address Generated variable space ... 0136 [Space for gVar variable] ... 1000 (last location in the data segment)

Relocatable Object Module Code Segment Relative Address Generated Code 0000 ... ... 0008 entry proc_a ... 0220 load =7, R1 0224 store R1, 0036 0228 push 0036 0232 call ‘put_record’ ... 0400 External reference table ... 0404 ‘put_record’ 0232 ... 0500 External definition table ... 0540 ‘proc_a’ 0008 ... 0600 (symbol table) ... 0799 (last location in the code segment) Data Segment Relative Address Generated variable space ... 0036 [Space for gVar variable] ... 0049 (last location in the data segment)

Addresses are FIXED at compile/assemble time.

You need an absolute loader to load this program

A relocatable loader is required

Addresses are fixed by the loader

Logical vs. Physical Address SpaceThe concept of a logical address space that is

bound to a separate physical address space is central to proper memory management.Logical address – generated by the CPU; also

referred to as virtual address.Physical address – address seen by the memory

unit. Logical and physical addresses are the same in

compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme.

Memory Management Unit - (MMU)MMU is a hardware

device that maps virtual to physical address at run-time.

 The user program deals with logical addresses; it never sees the real physical addresses.

Memory Management Techniques

MMT

Contiguous Non-contiguous

Single Partition Multiple Partition

Fixed

Paging Segmentation Combined

Dynamic

Virtual

Single-Partition AllocationA single processes in loaded into the memory

at a time.Commercially available in 1940s and 1950sEntire program loaded into memory

Single-Partition AllocationAdvantages:

SimplicityNo special hardware required

Disadvantages:CPU wastedMain memory not fully usedLimited job sizeNo support for multiprogramming

Multiple-Partition Allocation – Fixed Partitions

Multiple-Partition Allocation – Fixed Partitions

Multiple-Partition Allocation – Fixed PartitionsDisadvantages

Requires contiguous loading of entire program Job allocation method

First available partition with required sizeTo work well:

All jobs must be same size and memory size known ahead of time

Arbitrary partition size leads to undesired resultsPartition too small - Large jobs have longer

turnaround timePartition too large - Memory waste

Multiple-Partition Allocation – Dynamic Partitions

Merging of holes

Pros and Cons of Dynamic AllocationAdvantages

Efficient use of memory

DisadvantagesPartition managementCompaction or external fragmentationInternal fragmentation

Dynamic Allocation Placement AlgorithmsFirst-fit: Allocate the first hole that is big

enough. The search can start either at the beginning of the set of holes or where the previous first-fit search was ended.

If entire list searched in vain

Then job is placed into waiting queue

Else Memory Manager fetches next job

Dynamic Allocation Placement AlgorithmsBest-fit: Allocate the smallest hole that is

big enough; must search entire list, unless ordered by size. Produces the smallest leftover hole.

Entire table searched before allocation

Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole.

First-fit and best-fit better than worst-fit in terms of speed and storage utilization.

FragmentationExternal Fragmentation – total memory

space exists to satisfy a request, but it is not contiguous.

Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used.

External fragmentation, Can I run 7KBH process?

Solutions to External FragmentationCompactionContiguous Allocation – Paging,

Segmentation

Compaction

Paging

Memory Allocation in Paging

Address Translation

Address Translation

Translating Logical Address into Physical Address

Implementation of Page TablePage table for each process is kept in main memory.Page-table base register (PTBR) points to the page

table.Page-table length register (PTLR) indicates size of the

page table.In this scheme every data/instruction access requires

two memory accesses. One for the page table and one for the data/instruction.

The two memory access problem can be solved by the use of a special fast-lookup hardware cache called associative memory or translation look-aside buffers (TLBs).

Associative Memory

Pros and Cons of PagingAdvantages:

Efficient memory useSimple partition management due to discontinuous

loading and fixed partition sizeNo compaction is necessaryEasy to share pages

DisadvantagesJob size <= memory sizeInternal fragmentationNeed special hardware for address translationSome main memory is used for page tableAddress translation lengthens memory cycle times

Segmentation

Memory Allocation in Segmentation

Address Mapping in SegmentationThe user specifies each address by two

quantities: segment name/number and offset.   <segment-number, offset> Mapping from logical address to physical

address is done with the help of a segment table.Segment table – maps two-dimensional physical

addresses; each table entry has:base – contains the starting physical address where the

segments reside in memory. limit – specifies the length of the segment.

Address Mapping in Segmentation

Segmentation with Paging

Comparison of Paging and Segmentation

Recommended