1 / 21

Lecture 10: Heap Management

Lecture 10: Heap Management. CS 540 GMU Spring 2009. Process Address Space. Each process has its own address space : Text section (text segment) contains the executable code Data section (data segment) contains the global variables

tate
Download Presentation

Lecture 10: Heap Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 10: Heap Management CS 540 GMU Spring 2009

  2. Process Address Space • Each process has its own address space: • Text section (text segment) contains the executable code • Data section (data segment) contains the global variables • Stack contains temporary data (local variables, return addresses..) • Heap, which contains memory that is dynamically allocated at run-time. stack heap data text

  3. Heap Management • Heap is used to allocate space for dynamic objects • May be managed by the user or by the runtime system • In a perfect world:

  4. User Heap Management User library manages memory; programmer decides when and where to allocate and deallocate • void* malloc(longn) • void free(void*addr) • Library calls OS for more pages when necessary How does malloc/free work? • Blocks of unused memory stored on a freelist • malloc: search free list for usable memory block • free: put block onto the head of the freelist

  5. User Heap Management Drawbacks • malloc is not free: we might have to do a search to find a big enough block • As program runs, the heap fragments, leaving many small, unusable pieces • Have to have a way to reclaim and merge blocks when freed. • Memory management always has a cost. We want to minimize costs and, these days, maximize reliability

  6. User Heap Management • Pro: performance • Con: safety

  7. Automatic Heap Management Garbage collection – reclamation of chunks of storage holding objects that can no longer be accessed by a program • Originated with Lisp • New languages tend to have automatic management • Less error prone but performance penalty • Assumptions: type info is available at runtime (how large a block is, where are pointers), pointers are always to start of block

  8. Automatic Heap Management Issues: • How much does this increase the runtime of a program? • Space – need to manage free/used space • Pause time – incremental vs. ‘stop the world’ • Influences on data locality • Different types/sizes of objects

  9. Locating Garbage r1 S T A C K r2

  10. Locating Garbage r1 S T A C K r2

  11. Object reachability The set of reachable objects changes as a program executes- • Object allocations • Parameters & return values • Reference assignments • Stack-based variables

  12. Reference Counting • Keep a count of pointers to each object • performance overhead each time the reachable set can change • storage overhead since each object needs a count field • Zero references  garbage that can be removed (applied transitively) • > zero references  not garbage??

  13. Reference Counting 0 r1 2 S T A C K 0 2 1 2 2 r2 1 1 0

  14. What if r1 is removed? r1 1 S T A C K 1 1 2 1 r2 1

  15. Trace Collecting • When the heap starts to fill, pause the program and reclaim • ‘Stop the world’ – pauses can be significant • Lots of versions • Mark & sweep • Mark & compact • …

  16. Mark & Sweep Basic idea: • maintain a linked list, called the free list, that contains all of the heap blocks that can be allocated • if the program requests a heap block but the free list is empty, invoke the garbage collector • starting from the pointers in the stack, data area and registers*, trace and mark all reachable heap blocks • sweep and collect all unmarked heap blocks, putting each one back on the free list (merging as appropriate) • sweep again, unmarking all heap blocks First developed by McCarthy in 1960 for Lisp *detecting what is a pointer is easier said than done

  17. Mark & Sweep Issues: • How to detect pointers? • Need to keep a bit that we can use for the algorithm in every object • In Lisp, the program execution would pause while the system did garbage collecting

  18. Mark & Compact Basic idea (Fig 7.27 in text): • To garbage collect • Starting from the pointers in the stack, data area and registers, find and mark all reachable heap blocks • Scan the marked nodes and compute a new address for each, based on where it should be relocated to have all of the blocks contiguous • Move each block to its new location, updating any internal pointers into the heap based on step 2. Update any program & stack variables based on the new assignments as well.

  19. Generational Collection • Generational collection • Idea: An object that survives its first round of garbage collection is likely to survive later (i.e. objects tend to die young)

  20. A Generational algorithm • Memory is divided into N partitions • New objects are always allocated into partition 0 • When partition 0 fills, it is garbage collected (via some technique). Anything that survives is moved to partition 1 (leaving 0 empty). • Keep doing this – eventually partition i (> 0) will fill. Once it fills, garbage collect it and put surviving elements into partition i+1.

  21. Generational Garbage Collection

More Related