1 / 41

Memory Management

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.

kirby
Download Presentation

Memory 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. Memory Management Professor Yihjia Tsai Tamkang University

  2. Overview:memory management • explicit deallocation • malloc() + free() • implicit deallocation: garbage collection • reference counting • mark & scan • two-space copying

  3. 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

  4. Data allocation with explicit deallocation #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); malloc() • find free block of requested size • mark it in use • return a pointer to it. free() • mark the block as not in use.

  5. S I Z E S I Z E S I Z E S I Z E S S S S 1 0 1 0 marked in use marked free pointer to user data Heap layout chunk chunk block block ... in use free low high

  6. S I Z E S I Z E S I Z E S I Z E S I Z E free S S S S S 0 1 1 0 1 Free() ... in use free PROCEDURE Free (Block pointer): SET Chunk pointer TO Block pointer – Admin size; SET Chunk pointer .free TO True;

  7. S I Z E S I Z E S I Z E S I Z E S I Z E in use free S S S S S 0 1 1 1 0 Malloc() ... 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);

  8. S I Z E S I Z E S I Z E S I Z E S I Z E S I Z E in use free S S S S S S 0 1 0 0 1 1 block pointer 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 ... free

  9. S I Z E S I Z E S I Z E S I Z E S I Z E S I Z E S I Z E in use free S S S S S S S 0 1 1 1 1 0 0 block pointer 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 • 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 ... free

  10. 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;

  11. S I Z E S I Z E S I Z E S I Z E S I Z E S I Z E S I Z E in use free S S S S S S S next next next 1 1 0 1 0 1 0 Coalesce free chunks () • walk chunks from low to high • check if chunk is free • if so, coalesce all subsequent free chunks ...

  12. Coalesce free chunks PROCEDURECoalesce 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;

  13. S I Z E S I Z E S I Z E S I Z E S I Z E S I Z E S I Z E in use free S S S S S S S 1 0 0 1 1 1 0 ... use first field as next ptr Optimizations free: poor performance (linear search) malloc: irregular performance (coalesce phase) solutions: • free lists indexed by size • coalesce at free()

  14. 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);

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

  16. Answers

  17. 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

  18. 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

  19. A D E F C B Example heap root set

  20. A D E E E F C B B B Garbage heap root set

  21. garbage? E F D A C B Cyclic garbage heap root set • “no-pointers”: NO • “not-reachable”: YES

  22. 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

  23. 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

  24. 0 2 1 1 2 1 0 1 1 2 Reference counting heap root set • record #pointers to each chunk • reclaim when reference count drops to zero A D B E C F

  25. 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 Maintaining reference counts pointer assignment: VAR p, q : pointer; ... p := q; source 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);

  26. A D E F E C B B Mark & scan heap root set • mark all reachable chunks • scan heap for unmarked chunks that can be freed

  27. Mark & scan PROCEDURE 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;

  28. 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

  29. S p t r p t r p t r p t r p t r p t r p t r p t r p t r 0 0 Advanced marking to parent size S 2 pointer cnt 1 0 mark bit free bit S 1 1 0

  30. p t r p t r p t r p t r p t r Advanced marking:pointer reversal • avoid additional parent pointer • use the n-th child pointer when visiting child n to parent S p t r 2 1 0 S 1 1 0

  31. 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

  32. to from 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

  33. D E F A A C B C scan scan scan Copying to to-space from • copy root set • leave forwarding pointers • scan to-space for reachable cells in from-space to

  34. D scan scan scan Copying to to-space from A • copy root set • leave forwarding pointers • scan to-space for reachable cells in from-space B E C F to A C

  35. D scan scan Copying to to-space from A • copy root set • leave forwarding pointers • scan to-space for reachable cells in from-space B E C F to A C

  36. D scan scan Copying to to-space from A • copy root set • leave forwarding pointers • scan to-space for reachable cells in from-space B E C to A C F

  37. D scan scan Copying to to-space from A • copy root set • leave forwarding pointers • scan to-space for reachable cells in from-space B E C to A C F

  38. D scan scan Copying to to-space from A • copy root set • leave forwarding pointers • scan to-space for reachable cells in from-space B E C to A C F

  39. D scan Copying to to-space from A • copy root set • leave forwarding pointers • scan to-space for reachable cells in from-space B E C to A C F

  40. D scan Copying to to-space from • copy root set • leave forwarding pointers • scan to-space for reachable cells in from-space to A C F

  41. Summary Memory management • explicit deallocation • malloc() + free() • implicit deallocation: garbage collection • reference counting • mark & scan • two-space copying

More Related