Docsity

Physical Memory, Virtual Memory - Memory Management | CompSci 143, Study notes of Computer Science

Partial preview of the text

Download Physical Memory, Virtual Memory - Memory Management | CompSci 143 and more Study notes Computer Science in PDF only on Docsity!

Part II: Memory Management

Chapter 7: Physical MemoryChapter 7: Physical MemoryChapter 8: Virtual MemoryCh

t^

9 Sh

i^

D t

d C d

i^

M i

Chapter 9: Sharing Data and Code in Main

Memory

CompSci 143A

Spring, 2009

7. Physical Memory

7.1 Preparing a Program for Execution

  • Program Transformations

Program Transformations

  • Logical-to-Physical Address Binding

7 2 Memory Partitioning Schemes7.2 Memory Partitioning Schemes

  • Fixed Partitions– Variable Partitions

Variable Partitions

  • Buddy System

7 3 Allocation Strategies for Variable Partitions7.3 Allocation Strategies for Variable Partitions7.4 Dealing with Insufficient MemoryCompSci 143A

Spring, 2009

Address Binding

• Assign Physical Addresses: Relocation• Static binding

  • Programming time– Compilation time– Linking time– Loading time

• Dynamic binding

y

g

  • Execution time

CompSci 143A

Spring, 2009

Static Address Binding

Static Binding

= At Programming,

Compilation, Linking, and/or Loading TimeCompilation, Linking, and/or Loading Time

CompSci 143A

Figure 7-2Spring, 2009

Address Binding

• How to implement dynamic binding

  • Perform for each address at run time:

pa = address_map(la)

  • Simplest form of

address_map

:

R l

ti

R

i t

l^

RR

R

elocation Register:

pa = la + RR

More general form:

More

general form: Page/Segment Table

(Chapter 8)

CompSci 143A

Spring, 2009

Memory Partitioning Schemes

-^

Fixed Partitions– Single-program systems: 2 partitions (OS/user)

Single program systems: 2 partitions (OS/user)

  • Multi-programmed: partitions of different sizes -^

How to assign processes to partitions

(cf. Fig 7-5)

  • Separate queue for each partition: Some partitions may

be unused

  • Single queue: More complex but more flexible– Single queue: More complex, but more flexible -^

Limitations of fixed partitions– Program size limited to largest partition

g

g

p

  • Internal fragmentation (unused space within partitions)

CompSci 143A

Spring, 2009

Variable Partitions

-^

Memory not partitioned

a priori

-^

Each request is allocated portion of free space

-^

Memory = Sequence of variable-size blocks– Some are occupied, some are free (holes)

E t

l f

t ti

b

E

xternal fragmentation

occurs: memory may be

divided into many small pieces

-^

Adjacent holes (right, left, or both) must be

j^

(^

g

,^

,^

)

coalesced

to prevent increasing fragmentation

CompSci 143A

Figure 7-

Spring, 2009

Linked List Implementation 1

-^

Type

,^

Size

tags at the start of each Block

-^

Holes contain links to predecessor hole and to next hole

p

-^

Must be sorted by physical address

-^

Checking neighbors of released block

b

(= block

C

below):

  • Right neighbor (easy): Use size of

b

  • Left neighbor (clever): Use sizes to find first hole to

b

’s right,

follow its predecessor link to first hole on

b

’s left, and check if it is

p^

,

adjacent to

b

.

CompSci 143A

Figure 7-7a

Spring, 2009

Bitmap Implementation

• Memory divided into fix-size blocks• States of the blocks represented by a binary

States of the blocks represented by a binarystring, the

bitmap

  • State of each block represented by a bit in the bitmap

= free,

= allocated

• Can be implemented as

char

or

int

array (or in

Java as a

byte

array)

Java as a

byte

array)

• Operations use bit masks

  • Release:

&

Boolean bitwise

and

(^

  • Allocate:

|

(Boolean bitwise

or

)^

'

  • Search for free block: Find left-most 0 bit
    • Repeatedly check left most bit and shift mask right

CompSci 143A

  • Repeatedly, check left-most bit and shift mask right

Spring, 2009

Example

A

3 KB

Free

i d

Assume• Memory broken into

B

2 KB

Occupied

C

5 KB

Occupied

• Memory broken into

blocks of size 1KBU

f b t

f^

D

1 KB

Occupied

E

5 KB

Free

• Use arrays of bytes for

memory map

E

5 KB

Free

B[0]
B[1]

• Release block D:

B[1] = B[1] & '11011111'

00011111

11100000

B[0]
B[1]

• Allocate first 2 blocks

of block A:CompSci 143A

B[0] = B[0] | '11000000'

Spring, 2009

The Buddy System

Figure 7-

Sizes: 1, 2, 4, 8, 16 a)

3 blocks allocated& 3 holes left

b)

Block of size 1allocatedallocated

c)

Block 12-13 released

CompSci 143A

Spring, 2009

Allocation Strategies with Variable Partitions

-^

Problem: Given a request for

n

bytes, find hole

≥

n

-^

Goals:

-^

Goals:^ – Maximize memory utilization (minimize

external

fragmentation

  • Minimize search time -^

Search Strategies:–

First-fit:

Always start at same place. Simplest.

Next-fit:

Resume search. Improves distribution of holes.

B

est-fit:

Closest fit. Avoid breaking up large holes.

Worst-fit:

Largest fit. Avoid leaving tiny hole fragments

Fi

t Fit i

ll

th

b

t^

h i

CompSci 143A

-^

First Fit is generally the best choice

Spring, 2009

Used Blocks vs. Holes

-^

How many blocks are used, how many are holes?–

^50

% rule

(Knuth, 1968):

rule

(Knuth,

#holes = p × #full_blocks/

  • p

= probability of inexact match (i.e., remaining hole)

I^

i^

1

  • In practice

p=

, because exact matches are highly unlikely, so

  • Of the total number of (occupied) blocks and holes,

1/3 are holes

CompSci 143A

Spring, 2009

How much memory is unused (wasted)

-^

Utilization depends on the ratio

k=hole_size/block_size

Wh

1

(

i^

b bili

f i

h)

-^

When

p=

(

p

i

s probability of inexact match)

unused_memory = k/(k+2)

-^

Intuition:Intuition:– When

k

,^

unused_memory

(100% empty)

  • When

k=

,^

unused_memory

(50% rule)

Wh

k

d

(100% f ll)

  • When

k

,^

unused_memory

full)

-^

What determines

k

?

The block size

b

relative to total memory size

M

y

  • Determined experimentally via simulations:
    • When

b

≤^

M/

,^

k=0.

and

unused_memory

≈0.

  • When

b=M/

,^

k=

and

unused memory

≈0.

CompSci 143A

,^

_^

y

•^

Conclusion:

M

must be large relative to

b

Spring, 2009