memory management

41
Memory Management Professor Yihjia Tsai Tamkang University

Upload: kirby

Post on 23-Feb-2016

28 views

Category:

Documents


0 download

DESCRIPTION

Memory Management. Professor Yihjia Tsai Tamkang University. Overview : memory management. explicit deallocation malloc() + free() implicit deallocation: garbage collection reference counting mark & scan two-space copying. Memory management. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Memory Management

Memory Management

Professor Yihjia Tsai

Tamkang University

Page 2: Memory Management

Overview:memory management

• explicit deallocation• malloc() + free()

• implicit deallocation: garbage collection• reference counting• mark & scan• two-space copying

Page 3: Memory Management

Memory management

What has a compiler to do with memory management?• compiler uses heap-allocated data structures• modern languages have automatic data

(de)allocation• garbage collection part of runtime support

system• compiler usually assists in identifying

pointers

Page 4: Memory Management

Data allocation with explicit deallocation

malloc() • find free block of requested size• mark it in use• return a pointer to it.

free()• mark the block as not in use.

#include <stdlib.h>

void *calloc(size_t nmemb, size_t size);void *malloc(size_t size);void free(void *ptr);void *realloc(void *ptr, size_t size);

Page 5: Memory Management

Heap layout

SIZE

S

0

SIZE

S

1

SIZE

S

0

SIZE

S

1

...

low high

markedfree

markedin use

blockblock

chunkchunk

in use free

pointer touser data

Page 6: Memory Management

Free()

PROCEDURE Free (Block pointer): SET Chunk pointer TO Block pointer – Admin size; SET Chunk pointer .free TO True;

SIZE

S

1

SIZE

S

0

SIZE

S

1

...free

SIZE

S

0

in use

SIZE

S

1

free

Page 7: Memory Management

Malloc()

SIZE

S

1

SIZE

S

0

SIZE

S

1

...free

FUNCTION Malloc (Block size) RETURNS a generic pointer: SET Pointer TO Free block of size (Block size); IF pointer /= NULL: RETURN pointer;

Coalesce free chunks (); RETURN Free block of size (Block size);

SIZE

S

0

in use

SIZE

S

1

free

Page 8: Memory Management

• walk chunks from low to high• check if chunk is free AND large enough• if so, mark chunk in use AND return block pointer

SIZE

S

0

in use

SIZE

S

1

free

Free block of size (request)

SIZE

S

1

SIZE

S

0

SIZE

S

1

...free

SIZE

S

0

block pointer

Page 9: Memory Management

• walk chunks from low to high• check if chunk is free AND large enough• if so, mark chunk in use AND return block pointer

SIZE

S

0

in use

SIZE

S

1

free

Free block of size (request)

• walk chunks from low to high• check if chunk is free AND large enough• if so, mark chunk in use AND return block pointer• optimization: split chunk to free unused part

SIZE

S

1

SIZE

S

0

SIZE

S

1

...free

SIZE

S

0

SIZE

S

1

block pointer

Page 10: Memory Management

Free block of size

FUNCTION Free block of size (Block size) RETURNS a generic pointer: SET Chunk ptr TO Heap low; SET Request TO Block size + Admin size;

WHILE Chunk ptr < Heap high: IF Chunk ptr .free AND Chunk ptr .size >= Request: Split chunk (Chunk ptr, Request) SET Chunk ptr .free TO False; RETURN Chunk ptr + Admin size;

SET Chunk ptr TO Chunk ptr + Chunk ptr .size; RETURN NULL;

Page 11: Memory Management

next

SIZE

S

0

in use

SIZE

S

1

free

Coalesce free chunks ()

• walk chunks from low to high• check if chunk is free• if so, coalesce all subsequent free chunks

SIZE

S

1

SIZE

S

0

SIZE

S

1

...SIZE

S

0

SIZE

S

1

nextnext

Page 12: Memory Management

Coalesce free chunks

PROCEDURE Coalesce free chunks (): SET Chunk ptr TO Heap low;

WHILE Chunk ptr < Heap high: IF Chunk ptr .free: SET Next TO Chunk ptr + Chunk ptr .size; WHILE Next < Heap high AND Next .free: SET Next TO Next + Next .size; SET Chunk ptr .size TO Next - Chunk ptr;

SET Chunk ptr TO Chunk ptr + Chunk ptr .size;

Page 13: Memory Management

Optimizationsfree: poor performance (linear search)malloc: irregular performance (coalesce phase)

solutions:• free lists indexed by size• coalesce at free()

SIZE

S

0

in use

SIZE

S

1

free

SIZE

S

1

SIZE

S

0

SIZE

S

1

...SIZE

S

0

SIZE

S

1

2log(size) 3 4 5 6

free list

use first fieldas next ptr

Page 14: Memory Management

Malloc() with free lists

FUNCTION Malloc (Block size) RETURNS a generic pointer: SET Chunk size TO Block size + Admin size; SET Index TO 2log(Chunk size);

IF Index < 3: SET Index TO 3;

IF Index <= 10 AND Free list[Index] /= NULL: SET Pointer TO Free list[Index]; SET Free list[Index] .next TO Pointer .next; RETURN Pointer + Admin size;

RETURN Free block of size (Block size);

Page 15: Memory Management

Exercise (5 min.)

• give the pseudo code for free() when using free lists indexed by size.

Page 16: Memory Management

Answers

Page 17: Memory Management

Answers

PROCEDURE Free (Block pointer): SET Chunk pointer TO Block pointer – Admin size; SET Index TO 2log(Chunk pointer .size);

IF Index <= 10: SET Chunk pointer .next TO Free list[Index]; SET Free list[Index] TO Chunk pointer; ELSE SET Chunk pointer .free TO True; // Coalesce subsequent free chunks

Page 18: Memory Management

Garbage collection

• memory allocation is explicit (new)• memory deallocation is implicit

• garbage set: all chunks that will no longer be used by the program• chunks without incoming pointers• chunks that are unreachable from non-heap data

Page 19: Memory Management

Example

A

C

D

E

F

B

heap

root set

Page 20: Memory Management

Garbage

A

C

D

E B

heap

root set

B B E E

F

Page 21: Memory Management

Cyclic garbage

C

E

F

B

heaproot set

D A

garbage?

• “no-pointers”: NO• “not-reachable”: YES

Page 22: Memory Management

Compiler assistance:identifying pointers

• pointers inside chunks• user-defined data structures

• compiler: generate self-descriptive chunks

• pointers located outside the heap (root set)• global data + stack

• compiler: generate activation record descriptions

Page 23: Memory Management

Self-descriptive chunks

• bitmap per data type• problem: overhead per chunk / interpretation

• compiler-generated routine per data type• calls GC for each pointer• problem: recursion

• organize data type to start off with n pointers• solution: n can be squeezed into chunk admin

Page 24: Memory Management

Reference counting

• record #pointers to each chunk• reclaim when reference count drops to zero

2

2

1

1

2

heaproot set

A D

B E

FC

1

0 0

1 1

Page 25: Memory Management

Maintaining reference counts

VAR p, q : pointer;...p := q;

PROCEDURE Free recursively (Pointer): FOR each field fi of record Pointer:

IF Points into the heap (fi):

Decrement fi .ref count;

IF fi .ref count = 0:

Free recursively (fi); Free chunk (Pointer);

source

IF Points into the heap (q): Increment q .ref count;IF Points into the heap (p): Decrement p .ref count; IF p .ref count = 0: Free recursively (p);SET p TO q; target

pointer assignment:

Page 26: Memory Management

Mark & scan

A

C

D

E

F

B

heap

root set

• mark all reachable chunks• scan heap for unmarked chunks that can be freed

B E

Page 27: Memory Management

Mark & scanPROCEDURE Mark (Pointer): IF NOT Points into the heap (Pointer): RETURN; SET Pointer .marked TO True; FOR each field fi of record Pointer:

Mark (fi);

PROCEDURE Scan (): SET Chunk ptr TO Heap low;

WHILE Chunk ptr < Heap high: IF Chunk ptr .marked: SET Chunk ptr .marked TO False; ELSE SET Chunk ptr .free TO True;

SET Chunk ptr TO Chunk ptr + Chunk ptr .size;

Page 28: Memory Management

Advanced marking

• problem: mark() is recursive• solution: embed stack in the chunks

each chunk records:• a count denoting which child pointer is next• a pointer to the parent node

Page 29: Memory Management

Advanced marking

210

S ptr

ptr

ptr

110

S ptr

ptr

ptr

free bit

mark bit

pointer cnt

sizeto parent

00

S ptr

ptr

ptr

Page 30: Memory Management

Advanced marking:pointer reversal• avoid additional parent pointer• use the n-th child pointer when visiting child n

210

S ptr

ptr

ptr

110

S ptr

ptr

ptr

to parent

Page 31: Memory Management

Two-space copying

• most chunks have a short live time• memory fragmentation must be addressed

• partition heap in two spaces

copy all reachable chunks to consecutive locations

from to

Page 32: Memory Management

Two-space copying

• most chunks have a short live time• memory fragmentation must be addressed

• partition heap in two spaces

copy all reachable chunks to consecutive locations

from tofrom to

Page 33: Memory Management

Copying to to-space

• copy root set• leave forwarding

pointers

• scan to-space for reachable cells in from-space

C

D

E

F

B

from

to

A

C

A

scanscanscan

Page 34: Memory Management

Copying to to-space

• copy root set• leave forwarding

pointers

• scan to-space for reachable cells in from-space

C

E

F

B

from

to

A

C

scanscanscan

A

D

Page 35: Memory Management

Copying to to-space

• copy root set• leave forwarding

pointers

• scan to-space for reachable cells in from-space

C

E

F

B

from

to

A

C

scanscan

A

D

Page 36: Memory Management

Copying to to-space

• copy root set• leave forwarding

pointers

• scan to-space for reachable cells in from-space

C

E

B

from

to

A

C

scanscan

A

D F

Page 37: Memory Management

Copying to to-space

• copy root set• leave forwarding

pointers

• scan to-space for reachable cells in from-space

C

E

B

from

to

A

C

scanscan

A

D F

Page 38: Memory Management

Copying to to-space

• copy root set• leave forwarding

pointers

• scan to-space for reachable cells in from-space

C

E

B

from

to

A

C

scanscan

A

D F

Page 39: Memory Management

Copying to to-space

• copy root set• leave forwarding

pointers

• scan to-space for reachable cells in from-space

C

E

B

from

to

A

C

A

D F

scan

Page 40: Memory Management

Copying to to-space

• copy root set• leave forwarding

pointers

• scan to-space for reachable cells in from-space

from

toC A

D F

scan

Page 41: Memory Management

Summary

Memory management

• explicit deallocation• malloc() + free()

• implicit deallocation: garbage collection• reference counting• mark & scan• two-space copying