MEMORY MANAGEMENT

compiler compiles program code and data and makes a guess about how big a heap and stack the program will need as it executes

the total size N of code, data, guessed heap, and guessed stack is put into the executable file produced by the compiler

the compiler has no idea about what actual physical RAM addresses the program will occupy as it runs so the compiler assumes the program will start at RAM address 0 through total (guessed) size N

the role of the memory manager is to map the process address space (code, data, heap, stack running from pretend addresses 0 to N-1) into actual RAM addresses M1 to M2

static (loadtime) binding (relocation): rewrite all addresses in the compiler-produced code by adding M1

dynamic (runtime) binding (relocation): use base and limit registers, base = M1, limit = N = M2 - M1 + 1

schemes to manage free areas of RAM to allocate for new processes

fixed number of partitions of fixed sizes

variable number of partitions of variable sizes

we have four restrictions or limitations limiting the flexibility of the memory manager

to solve the heap-stack collision problem, modify the compiler so it divides a program it is compiling into two pieces, code-data-heap and stack, and produces two address spaces, code-data-heap from 0 to N1-1 and stack from 231 to 231 + N2-1, where N1 and N2 are still guessed by the compiler

turn stack rightside up and start it at 231

starting the stack at 231 gives the CPU a way to distinguish code-data-heap addresses from stack addresses

two base registers baseCDH and baseS in the CPU instead of one; two limit registers limitCDH and limitS in the CPU instead of one; two base values and two limit values instead of one stored in the process table PCB for each process

format of machine instruction reading or writing data-heap using an address field and baseCDH, limitCDH for runtime hardware address translation

          register       32 bit compiler
           number        generated address
--------------------------------------------
|        |               |                 |
| opcode | Rx            | 0xxxxx...xxxxxx |
|        |               |                 |
--------------------------------------------

format of machine instruction reading or writing stack using an address field and baseS, limitS for runtime hardware address translation

          register       32 bit compiler
           number        generated address
--------------------------------------------
|        |               |                 |
| opcode | Rx            | 1xxxxx...xxxxxx |
|        |               |                 |
--------------------------------------------

when program is to be executed, the memory manager finds two big enough holes for the two address space pieces

if the heap reaches its limit, copy the code-data-heap to a bigger hole and adjust baseCDH and limitCDH

if the stack reaches its limit, copy the stack to a bigger hole and adjust baseS and limitS

but we still have three restrictions or limitations limiting the flexibility of the memory manager that uses the hole list (of variable sized holes) and needs to find two holes from the hole list to execute a program for a user

  1. whole program must be loaded into RAM for it to execute,
  2. program code-data-heap and program stack must be loaded into two contiguous regions of RAM, and
  3. fragmentation (many small holes in the hole list) is a problem (external fragmentation)

virtual memory implemented with paging eliminates the above three restrictions

here are some other concerns about RAM usage handled by virtual memory; the following are ways that an executing program, if loaded entirely in RAM when it begins executing, uses its RAM allocation inefficiently or wastefully

if a program can execute without all of its code and data in RAM all the time, the OS memory manager can use RAM more efficiently; in other words, a process can its RAM allocation more efficiently or less wastefully

the following stuff is explained in more detail in the ``Paging Fact Sheet''

pages of code-data-heap and stack address spaces and page frames of RAM, all of size 212 = 4096 bytes

each process has two page tables inside OS RAM, one for code-data-heap and one for stack

OS hole list becomes list of free page frames

to run a process, OS need load only some of code-data-heap and stack pages into free page frames

address translation done by CPU hardware

          register            32 bit compiler
           number             generated address
                           1      19          12
-----------------------------------------------------
|        |               |   | xxxxxxxxxx  | xxxxxx |
| opcode | Rx            | y |             |        |
|        |               |   | page number | offset |
-----------------------------------------------------

page fault handled by OS software (page fault interrupt handler)

what if no free page frames on free page frame list?

local replacement, fixed allocation

global replacement, variable allocation

external versus internal fragmentation

spatial and temporal locality

TLB (translation lookaside buffer)

factors affecting choice of page size

swapping area on disk


home page: http://elvis.rowan.edu/~hartley/index.html
e-mail: hartley@elvis.rowan.edu