Memory Management

therapistarmySoftware and s/w Development

Dec 14, 2013 (3 years and 6 months ago)

54 views

12/14/2013

IT 327

1

Memory Management
(Ch 14)


Dynamic memory allocation



Language systems provide an important hidden player:

Runtime memory manager



Activation records


Objects


Explicit allocations:
new
,
malloc
, etc.


Implicit allocations: strings, file buffers, arrays with
dynamically
varying size
, etc.

It is a 800
-
pages topic, but here

we want to do it in one/two class!

12/14/2013

IT 327

2

Memory Model


For now, assume that the
OS

grants each
running program
one or more fixed
-
size
regions

of memory for dynamic allocation


We will model these regions as an
array



Our textbook shows an examples of memory
management code in Java

Yes! It’s OS’s job!!

12/14/2013

IT 327

3

Stacks Of Activation Records


Activation records must be allocated
dynamically


Allocate
on call

and de
-
allocate
on return


Stack of activation records:
push
and
pop

A simple memory management problem

12/14/2013

IT 327

4

A Stack Illustration

An empty stack of 8
words.

grow

reg.

12/14/2013

IT 327

5

The manager
program calls
m.push(3).

Some program
P

needs 3 words

P

gets this

pointer

12/14/2013

IT 327

6

Some other program Q needs 2 words

The manager
program calls
m.push(2).

P gets this

pointer

Q gets this

pointer

wasted

12/14/2013

IT 327

7

Heap


Stack is easy to do, but



Allocations and deallocations may come in any

order?



A
heap

is a pool of blocks of memory



At the beginning there is only one block



(but not always the case)


There are many approaches for doing this…

12/14/2013

IT 327

8

How to deal blocks to the requests?

A pool of free blocks are linked, initially containing one big
free block, then fragmented into small blocks


Search the free list for first adequate block


If there is extra space in the block, return the unused
portion to front of the free list


Allocate requested portion (at the lower end)


The easiest approach:
First Fit

for
request

To
free
a block, just add to the front of the free list

12/14/2013

IT 327

9

Heap implementation


in an array

Size of this block

Pointer to
next block,

-
1 means no
next block.

12/14/2013

IT 327

10

Size of this free block

Size of this block for P

Some program P

requests 4 words

P gets this

pointer

12/14/2013

IT 327

11

Size of this block

Q gets this

pointer

Some program Q

requests 2 words

12/14/2013

IT 327

12

P is done

12/14/2013

IT 327

13

Some program R

requests 1 word

R gets this

pointer

12/14/2013

IT 327

14

A Problem


Consider this sequence:









The manager needs to
coalesce

adjacent free
blocks.

1.
p1=m.allocate(4);

2.
p2=m.allocate(4);

3.
m.deallocate(p1);

4.
m.deallocate(p2);

5.
p3=m.allocate(7);

0

5

-
1

5

-
1

5

5

5

5

p1

p1

p2

5

-
1

5

p2


top


top


top


top

How?

12/14/2013

IT 327

15

Improvement strategy


Quick lists
, a separate list, same sized blocks




Delayed coalescing

Keep separate free lists for
popular (small) block sizes

12/14/2013

IT 327

16

p1=m.allocate(4);

p2=m.allocate(1);

m.deallocate(p1);

p3=m.allocate(5);

The final allocation will fail because
of fragmentation.

Another problem:





Fragmentation

DeFragmentation

12/14/2013

IT 327

17

Heap Mechanisms

(to manage heaps.)


Three major issues:


Placement

where to allocate a block


Splitting

when and how to split large blocks


Coalescing

when and how to recombine


Many variety and different refinements

12/14/2013

IT 327

18

Placement:
Where to allocate a block



(FF) First Fit

from a stack (
FIFO
) of free list


(BF) Best Fit …


(NF) Next Fit …


Some mechanisms use a more scalable data
structure like a
Balanced Binary Tree

12/14/2013

IT 327

19

Splitting:
When and how to split large blocks


Split
to requested
size



Sometimes, less splitting gives better results
e.g., allocate
more than requested
or
rounding up allocation size to
some

multiple
, for example 2
n


12/14/2013

IT 327

20

Coalescing:
When and how to recombine
adjacent free blocks


Several varieties:


No coalescing


Eager coalescing


Delayed coalescing

12/14/2013

IT 327

21

Current Heap Links



Some

systems track
current heap links


A
current heap link

is a memory
address (location in the heap)

where the running program

may use

a = new X(i);

b = new X(j);

a = b;

a

b

What if

12/14/2013

IT 327

22

Tracing Current Heap Links

IntList a =


new IntList(null);

int b = 2;

int c = 1;

a = a.cons(b);

a = a.cons(c);


Where
are the current
heap links in this
picture?

1

2

3

1

2

3

12/14/2013

IT 327

23

To Find Current Heap Links


Start with the
root set
: memory locations outside
of the heap with
links into the heap


We have to check:


Active activation records (if on the stack)


Static variables, etc.









For each memory location in the set, look at the
allocated
block it points to
, and
add all the
memory locations

in that block


Repeat until no new locations are found

{
a

b c }

They are not in the active activation record.

12/14/2013

IT 327

24

Possible Errors In Current Heap Links

1.
Exclusion errors
: a memory location that actually is a current heap
link is left out.
(This type of error is not allowed)

Current
Activation
Record

p

z:200

r[3]

Static
variables

s

x:123

y:100

456

a

b

c

d

2000

3000

4000

Heap

Current heap links

p, r, a

12/14/2013

IT 327

25

Possible Errors In Current Heap Links

2.
Unused

inclusion errors
: a memory location is included, but the
program
never actually uses

the value stored there


Current
Activation
Record

p

z:200

r[3]

Static
variables

s

x:123

y:100

456

a

b

c

d

2000

3000

4000

Heap

Current heap links

s, p, r, a, c,
d

How do I know this is
the case?

12/14/2013

IT 327

26

Possible Errors In Current Heap Links

3.
Used

inclusion errors (mistaken)
: a memory location is included, but the
program uses the value stored there
as something other

than a heap
address (e.g. int)



Current
Activation
Record

p

z:200

r[3]

Static
variables

s

x:123

y:100

456

a

b

c

d

2000

3000

4000

Heap

Current heap links

s, p, r, a, c,
y, z

How do I know this is
the case?

12/14/2013

IT 327

27

Errors Are Unavoidable


For heap manager purposes,
exclusion errors are
unacceptable


Therefore, we
must

include a location if it
might

be
used as a heap link, and


This makes
unused inclusion

errors

unavoidable


Depending on the language,
used inclusions errors
(mistaken)
may also be unavoidable

12/14/2013

IT 327

28

An application for current heap links:

Heap Compaction/Defragmentation


Manager can re
-
allocate
blocks:


Copy the block to a new
location


Update all links to (or
into) that block



So it can
compact

the heap,
moving all allocated blocks
to one end, leaving one big
free block and no
fragmentation

Q

P

P

Q

S

R

T

R

S

T

12/14/2013

IT 327

29


There are so many errors caused by improper de
-
allocation


(e.g., Bad or without class destructors in C/C++)



It is a
burden

on the programmer …

Therefore, let the language system do the job
automatically
---

Garbage Collection

12/14/2013

IT 327

30

Garbage Collection


1.
Mark and sweep

2.
Copying

3.
Reference counting

Three Major Approaches


12/14/2013

IT 327

31

Mark And Sweep


uses
current heap links

in a two
-
stage process:



Mark
: find the
live heap links

and mark all the heap
blocks pointed by them



Sweep
: make a pass over the heap and return unmarked
blocks to the free pool









-

both kinds of inclusion errors are tolerated,




-

blocks are not moved,
fragmentation may remain

12/14/2013

IT 327

Mark And Sweep

P

S

R

T

P

S

R

T

Mark
: live heap
links (in red)

Sweep
: return
unmarked blocks
to the free pool


blocks are not moved / fragmentations may remain

Free blocks

Free blocks

32

12/14/2013

IT 327

33

Copying Collection


divide memory in half, and uses only one half at a
time


When one half becomes full, find live heap links,
and copy live blocks to the other half


Fragmentation is eliminated


Moves blocks

Cannot tolerate used
inclusion
errors. Why?

Because that used variable will be mistaken
as a link and hence modified

12/14/2013

IT 327

34

Q

P

P

Q

S

R

T

R

S

T

Copying
Collection




Moves blocks:
cannot tolerate used
inclusion errors (mistake)

X=700

X=100

12/14/2013

IT 327

35

Reference Counting


Each block has a counter of heap links to it


Incremented when a heap link is copied,
decremented when a heap link is discarded


When counter goes to zero, block is garbage
and can be freed


Does not use current heap links

12/14/2013

IT 327

36

Reference Counting

12/14/2013

IT 327

37

Reference Counting Problem

Reference counters
are not zero

But all blocks are
garbage.

Garbage cycles can’t
be identified

12/14/2013

IT 327

38

Reference Counting


Problem 1:
cycles of garbage


Problem 2:

overhead of updating reference
counters is high



One advantage
: more uniform. I.e., cost is
incremental, no big pause while collecting

12/14/2013

IT 327

39

Garbage Collecting Refinements


Generational
collectors


Divide block into
generations

according to age


Garbage collect in younger generations more
often (using previous methods)


Incremental

collectors


Collect garbage a little at a time


Avoid the uneven performance like mark
-
and
-
sweep and copying collectors

12/14/2013

IT 327

40

Languages with Garbage Collectors


Required: Java, ML


encouraged: Ada


difficult to have: C, C++


But not impossible, even for C and C++


There are libraries that replace the usual
malloc
/
free

with a garbage
-
collecting
manager

12/14/2013

IT 327

41

Conclusion


Memory management is an important
hidden player in language systems


Performance and reliability are critical


Different techniques are difficult to
compare, since every run of every program
makes different memory demands


Still an active area

of language systems
research and experimentation