DOC PREVIEW
UT Arlington CSE 3302 - Lecture 10: Memory management

This preview shows page 1-2-3-4-28-29-30-31-58-59-60-61 out of 61 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 61 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE 3302Lecture 10: Memory management28 Sep 2010Nate NystromUniversity of Texas at Arlington• Some content from• Tim Teitelbaum, Cornell CS 412 slides, 2008• Jones, Garbage CollectionMemory management• Global memory• allocated at a fixed location in the process• static fields in Java, singleton objects in Scala, globals and statics in C, module-scoped variables in ML, M3• Stack• local variables, formal parameters, this• local arrays• Registers• local variables, ...• Heap• dynamically allocated data structuresMemory management⬇Stack HeapRegistersGlobalsOutline• Explicit memory management• Automatic memory management• reference counting• Deutsch-Bobrow deferred reference counting• mark and sweep• copying GC• concurrent/incremental GC• generational GC• BDW collectorExplicit memory management• Unix interface:• void&*malloc(size_t&n)•allocate n bytes of storage on the heap and return its address• void&free(void&*addr)•release storage allocated by malloc at address addr•User-level library manages heap, issues brk calls when necessary to grow the heap• C++: new/delete usually just call malloc/freeMalloc implementation• Blocks of unused memory stored in a freelist• malloc finds unused block on freelist• free puts block onto head of freelist• Simple, but:• External fragmentation = small free blocks scattered in the heap• Cannot alloc a large block even if sum of all free blocks is enough• malloc can be O(|heap|)freelistBuddy system• Maintain freelists for different allocation sizes, all powers of 2•malloc(n)• round n up to nearest power of 2• if no block of size n –– i.e., freelist(n) is empty• get block of size 2n• split: return block of size n, add other block (its buddy) to freelist(n)• free• coalesce free buddies of size n into a single free block of size 2n•Internal fragmentation: alloc larger blocks because of rounding• Trade external for internal fragmentation•malloc, free are O(1)Aside• How to check if an integer n is a power of 2• n&&&(n91)&!=&0• How to round an integer n up to the next power of 2• p&=&1;&while&(p&<&n)&p&<<=&1;• Without branching:• n&9=&1n&|=&(n&>>>&1)n&|=&(n&>>>&2)n&|=&(n&>>>&4)n&|=&(n&>>>&8)n&|=&(n&>>>&16)return&n&+&1Problems• Node&*x&=&new&Node(“happy”)• Node&*p&=&x;• delete&x;&&&//&But&I’m&not&dead&yet!• Node&*y&=&new&Node(“sad”);• cout&<<&p9>data&<<&endl;&&&&//&sadProblems•Might forget to call free•Might free a pointer not returned by malloc, corrupting the freelist•Might call free twice on same address, corrupting the freelistProblems• Makes modular programming more difficult• Every interface needs to agree on a contract• Have to know what code “owns” a given object so that objects are deleted exactly onceGarbage collection• Better: automatically collect unused memory• Gives the programmer the illusion that they have infinite memoryGarbage collection• Garbage collector is usually the most complex part of the run-time environment• Want to delete objects if they won’t be used again• This is undecidable!• So must be conservative• Use reachability as an approximation of liveness:• if there is no way to reach the object from globals, stack, registers, then object cannot be used again• might still retain objects that won’t be used again• but will not free objects that will be used againObject graph• Stack, registers, globals are roots of the object graph• Anything reachable from the roots is live, all else is garbage⬇StackHeapRegistersObject graph• Stack, registers, globals are roots of the object graph• Anything reachable from the roots is live, all else is garbage⬇StackHeapRegistersGC implementationReference counting• Idea:• associate a reference count with each object• number of references (pointers) to the object• Keep track of reference counts• For assignment x = e• decrement ref count for object referenced by x (if any)• increment ref count for object referenced by e• do the assignment• When reference count hits 0, object is unreachable => free itProblem⬇111222Problem⬇011222Remove a reference from the stackDecrement rcProblem⬇11122Deallocate objectDecrement rc for all its out-referencesProblem: cycles⬇11122Reference counting doesnot collect cycles!Problem: performance•Consider assignment: x.f&=&y• Without ref counts:[tx$+$off]$=$ty• With ref counts:t1$=$[tx$+$off]c$=$[t1$+$rc]c$=$c$.$1[t1$+$rc]$=$cif$(c$==$0)$reclaim_object(t1)c$=$[ty$+$rc]c$=$c$+$1[ty$+$rc]$=$c[tx$+$off]$=$ty• Large run-time overhead!•Worse: reclaim_object might trigger traversal of large graph to decrement ref counts in the heapDeutsch-Bobrow deferred rc• Don’t count reference from the stack• When rc goes to 0, insert into the zero count set (ZCS)• When ZCS is full• scan stack, incrementing counts of all objects referred to• scan ZCS, reclaim any objects with zero count• set ZCS to empty• scan stack, decrementing counts of all objects referred to• if rc goes to 0, insert into new ZCS• Still can’t collection cyclesMark-sweep GC• The classic algorithm• Two phases:• Mark phase• start from roots, trace object graph, marking every object reached• Sweep phase• iterate through all objects in the heap• reclaim unmarked objects• clear marks• optional: compact live objects in heap (called mark-compact)Object layout• Can use bit vector to record marks• Or store a mark bit in the object header.• Add another word to the header:• [mark bit]• [dispatch table]• [fields...]• Or: just use a bit of the dispatch table pointer• pointers aligned 4 have 2 free bits• need to mask off lsb on method dispatchMark phaseImplemented as depth-first search of object graphNatural recursive implementationfor&each&ref&p&in&rootSet:&&&&mark(p)mark(p)&{&&&&if&(*p&marked)&return&&&&mark&*p&&&&for&each&reference9type&field&x&in&*p:&&&&&&&&mark(p9>x)}Mark phaseQuestion: what happens when we try to mark a long linked list recursively?Mark phasestack&=&new&Stack()for&each&ref&p&in&rootSet:&&&&stack.push(p)while&(!&stack.empty)&{&&&&p&=&stack.pop&&&&if&(*p&marked)&continue&&&&mark&*p&&&&for&each&reference9type&field&x&in&*p:&&&&&&&&stack.push(p9>x)}Mark phaseQuestion: what happens when we try to mark a long linked list while maintaining a stack?Deutsch-Waite-Schorr algorithm• Idea:• during DFS, each pointer is followed only once• reverse the pointers


View Full Document

UT Arlington CSE 3302 - Lecture 10: Memory management

Documents in this Course
Smalltalk

Smalltalk

11 pages

Syntax

Syntax

5 pages

Syntax

Syntax

5 pages

JAVA

JAVA

57 pages

Semantics

Semantics

41 pages

Control

Control

74 pages

Load more
Download Lecture 10: Memory management
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 10: Memory management and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 10: Memory management 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?