Docsity

Memory Allocation III, Slides of Data Structures and Algorithms

Partial preview of the text

Download Memory Allocation III and more Slides Data Structures and Algorithms in PDF only on Docsity!

CSE351, Autumn 2018

L26: Memory Allocation III

Memory Allocation III CSE 351 Autumn 2018 Instructor: Justin Hsia Teaching Assistants: Akshat AggarwalAn WangAndrew HuBrian DaiBritt HendersonJames ShinKevin BiKory WatsonRiley GermundsonSophie TianTeagan Horkan

https://xkcd.com/835/

CSE351, Autumn 2018

L26: Memory Allocation III

Administrivia^ ^ Homework 5 due tonight^ ^ Lab 5 due next Friday (12/7)^ ^ Recommended that you watch the Lab 5 helper videos^ ^ Final Exam:

Wed, Dec. 12 @ 12:30pm in KNE 120

^ Review Session: Sun, Dec. 9 @ 5:00 pm in EEB 105 ^ Cumulative (midterm clobber policy applies)^ •^ Midterm portion will be “harder” than the Midterm ^ You get TWO double‐sided handwritten 8.

11” cheat

sheets^ •^ Recommended that you reuse or remake your midterm cheat sheet

CSE351, Autumn 2018

L26: Memory Allocation III

Segregated List (SegList) Allocators^ ^ Each

size class

of blocks has its own free list

^ Organized as an array of free lists ^ Often have separate classes for each small size ^ For larger sizes: One class for each two‐power size

Size class(in bytes)^1632 48‐64 80‐inf

CSE351, Autumn 2018

L26: Memory Allocation III

Allocation Policy Tradeoffs^ ^ Data structure of blocks on lists^ ^ Implicit (free/allocated), explicit (free), segregated (manyfree lists) – others possible!^ ^ Placement policy: first‐fit, next‐fit, best‐fit^ ^ Throughput vs. amount of fragmentation^ ^ When do we split free blocks?^ ^ How much internal fragmentation are we willing to tolerate?^ ^ When do we coalesce free blocks?^ ^ Immediate coalescing:

Every time

free^ is called

^ Deferred coalescing:

Defer coalescing until needed

-^ e.g. when scanning free list for

malloc

or when external

fragmentation reaches some threshold

CSE351, Autumn 2018

L26: Memory Allocation III

Memory Allocation^ ^ Dynamic memory allocation^ ^ Introduction and goals^ ^ Allocation and deallocation (free)^ ^ Fragmentation^ ^ Explicit allocation implementation^ ^ Implicit free lists^ ^ Explicit free lists (Lab 5)^ ^ Segregated free lists^ ^ Implicit deallocation: garbage collection^ ^ Common memory‐related bugs in C

CSE351, Autumn 2018

L26: Memory Allocation III

Wouldn’t it be nice…^ ^ If we never had to free memory?^ ^ Do you free objects in Java?^ ^ Reminder:

implicit

allocator

CSE351, Autumn 2018

L26: Memory Allocation III

Garbage Collection^ ^ How does the memory allocator know when memorycan be freed?^ ^ In general, we cannot know what is going to be used in thefuture since it depends on conditionals^ ^ But, we can tell that certain blocks cannot be used if theyare

unreachable

(via pointers in registers/stack/globals)

^ Memory allocator needs to know what is a pointerand what is not – how can it do this?^ ^ Sometimes with help from the compiler

CSE351, Autumn 2018

L26: Memory Allocation III

Memory as a Graph^ ^ We view memory as a directed graph^ ^ Each allocated heap block is a node in the graph^ ^ Each pointer is an edge in the graph^ ^ Locations not in the heap that contain pointers into the heap are called^ root

nodes (e.g. registers, stack locations, global variables)

A node (block) is

reachable

if there is a path from any root to that node

Non‐reachable nodes are

garbage^ (cannot be needed by the application)

Root nodesHeap nodes

reachable not reachable(garbage)

CSE351, Autumn 2018

L26: Memory Allocation III

Classical GC Algorithms^ ^ Mark‐and‐sweep collection

(McCarthy, 1960)

^ Does not move blocks (unless you also “compact”)  Reference counting (Collins, 1960) ^ Does not move blocks (not discussed)  Copying collection (Minsky, 1963) ^ Moves blocks (not discussed)  Generational Collectors (Lieberman and Hewitt, 1983) ^ Most allocations become garbage very soon, sofocus reclamation work on zones of memory recently allocated.  For more information: ^ Jones, Hosking, and Moss,

The Garbage Collection Handbook: The Art of

Automatic Memory Management

, CRC Press, 2012.

^ Jones and Lin,

Garbage Collection: Algorithms for Automatic Dynamic

Memory , John Wiley & Sons, 1996.

CSE351, Autumn 2018

L26: Memory Allocation III

Mark and Sweep Collecting^ ^ Can build on top of

malloc

/free^

package

^ Allocate using

malloc

until you “run out of space”

^ When out of space:^ ^ Use extra

mark bit

in the header of each block

^ Mark:

Start at roots and set mark bit on each reachable block

^ Sweep:

Scan all blocks and free blocks that are not marked

Before mark

root

After mark

Mark bit set

After sweep

free free

Arrows are NOTfree list pointers

CSE351, Autumn 2018

L26: Memory Allocation III

Mark^ ^ Mark using depth‐first traversal of the memory graph

ptr^ mark(

ptr^ p)^ {

//^ p:^ some

word^ in

a^ heap^

block

if^ (!is_ptr(p))

return

;^ //^ do

nothing

if^ not^

pointer

if^ (markBitSet(p))

return ;

//^ check

if^ already

marked

setMarkBit(p);

//^ set^ the

mark^ bit

for^ (i=0;

i<length(p);

i++)^ //

recursively

call^ mark

on

mark(p[i]);

//^ all

words^ in

the^ block

return ; } Before mark

root

After mark

Non‐testableMaterial Mark bit set

CSE351, Autumn 2018

L26: Memory Allocation III

Sweep^ ^ Sweep using sizes in headers

ptr^ sweep(

ptr^ p,^ ptr

end)^ {^

//^ ptrs^

to^ start

&^ end^ of

heap

while^ (p

<^ end)^

{^

//^ while

not^ at^

end^ of^ heap

if^ (markBitSet(p))

//^ check

if^ block

is^ marked

clearMarkBit(p);

//^ if^ so,

reset^ mark

bit

else^ if^

(allocateBitSet(p))

//^ if^ not

marked,

but^ allocated

free(p);

//^ free^

the^ block

p^ +=^ length(p);

//^ adjust

pointer

to^ next

block

Non‐testableMaterial

After mark

Mark bit set

After sweep

free free

CSE351, Autumn 2018

L26: Memory Allocation III

Memory‐Related Perils and Pitfalls in C

Program stop Slide possible?^

Fixes:

A)^ Dereferencing a non‐pointer B)^ Freed block – access again C)^ Freed block – free again D)^ Memory leak – failing to free memory E)^ No bounds checking F)^ Reading uninitialized memory G)^ Referencing nonexistent variable H)^ Wrong allocation size

CSE351, Autumn 2018

L26: Memory Allocation III

Find That Bug! (Slide 20)

char^ s[8]; int^ i; gets(s);

/* reads "123456789" from stdin */

Error^

Prog stop^

Fix:

Type:^

Possible?