Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 1 Heap Management. slide 2 Quote of the Day “Manually managing blocks of memory in C is like juggling bars of soap in a prison shower: It's all.

Similar presentations

Presentation on theme: "Slide 1 Heap Management. slide 2 Quote of the Day “Manually managing blocks of memory in C is like juggling bars of soap in a prison shower: It's all."— Presentation transcript:

1 slide 1 Heap Management

2 slide 2 Quote of the Day “Manually managing blocks of memory in C is like juggling bars of soap in a prison shower: It's all fun and games until you forget about one of them.” - Unknown

3 slide 3 Major Areas of Memory Static area –Fixed size, fixed content, allocated at compile time Run-time stack –Variable size, variable content (activation records) –Used for managing function calls and returns Heap –Fixed size, variable content –Dynamically allocated objects and data structures Examples: ML reference cells, malloc in C, new in Java

4 Heap Storage Memory allocation under explicit programmatic control –C malloc, C++ / Pascal / Java / C# new operation. Memory allocation implicit in language constructs –Lisp, Scheme, Haskel, SML, … most functional languages –Autoboxing/unboxing in Java 1.5 and C# Deallocation under explicit programmatic control –C, C++, Pascal Deallocation implicit –Java, C#, Lisp, Scheme, Haskel, SML, …

5 Heap Storage Deallocation Explicit versus Implicit Deallocation Examples: Implicit: Java, Scheme Explicit: Pascal and C To free heap memory a specific operation must be called. Pascal ==> dispose C ==> free C++ ==> delete Implicit and Explicit: Ada Deallocation on leaving scope In explicit memory management, the program must explicitly call an operation to release memory back to the memory management system. In implicit memory management, heap memory is reclaimed automatically by a “garbage collector”.

6 slide 6 Cells and Liveness Cell = data item in the heap –Cells are “pointed to” by pointers held in registers, stack, global/static memory, or in other heap cells Roots: registers, stack locations, global/static variables A cell is live if its address is held in a root or held by another live cell in the heap

7 slide 7 Garbage Garbage is a block of heap memory that cannot be accessed by the program –An allocated block of heap memory does not have a reference to it (cell is no longer “live”) –Another kind of memory error: a reference exists to a block of memory that is no longer allocated Garbage collection (GC) - automatic management of dynamically allocated storage –Reclaim unused heap blocks for later use by program

8 slide 8 Example of Garbage class node { int value; node next; } node p, q; p = new node(); q = new node(); q = p; delete p;

9 OO Languages and heap allocation Objects are a lot like records and instance variables are a lot like fields. => The representation of objects is similar to that of a record. Methods are a lot like procedures. => Implementation of methods is similar to routines. But… there are differences: Objects have methods as well as instance variables, records only have fields. The methods have to somehow know what object they are associated with (so that methods can access the object’s instance variables)

10 Example: Representation of a simple Java object Example: a simple Java object (no inheritance) class Point { int x,y; public Point(int x, int y) { this.x=x; this.y=y; } public void move(int dx, int dy) { x=x+dx; y=y+dy; } public float area() {...} public float dist(Point other) {... } } class Point { int x,y; public Point(int x, int y) { this.x=x; this.y=y; } public void move(int dx, int dy) { x=x+dx; y=y+dy; } public float area() {...} public float dist(Point other) {... } } (1) (2) (3) (4)

11 Example: Representation of a simple Java object Example: a simple Java object (no inheritance) Point class Point move area dist constructor(1) method(2) method(3) method(4) Point p = new Point(2,3); Point q = new Point(0,0); pqpq class xyxy 2323 xyxy 0000 new allocates an object in the heap

12 Objects can become garbage Point p = new Point(2,3); Point q = new Point(0,0); p = q;

13 Automatic Storage Deallocation (Garbage Collection) Everybody probably knows what a garbage collector is. But here are two “one liners” to make you think again about what a garbage collector really is! 1) Garbage collection provides the “illusion of infinite memory”! 2) A garbage collector predicts the future! It’s a kind of magic! :-) Let us look at how this magic is done!

14 Stacks and dynamic allocations are incompatible Why can’t we just do dynamic allocation within the stack?

15 Where to put the heap? The heap is an area of memory which is dynamically allocated. Like a stack, it may grow and shrink during runtime. Unlike a stack it is not a LIFO => more complicated to manage In a typical programming language implementation we will have both heap-allocated and stack allocated memory coexisting. Q: How do we allocate memory for both

16 Where to put the heap? Let both stack and heap share the same memory area, but grow towards each other from opposite ends! ST SB HB HT Stack memory area Heap memory area Stack grows downward Heap can expand upward

17 How to keep track of free memory? Stack is LIFO allocation => ST moves up/down everything above ST is in use/allocated. Below is free memory. This is easy! But … Heap is not LIFO, how to manage free space in the “middle” of the heap? HB HT Allocated ST SB Free Mixed: Allocated and Free reuse?

18 How to keep track of free memory? How to manage free space in the “middle” of the heap? HB HT => keep track of free blocks in a data structure: the “free list”. For example we could use a linked list pointing to free blocks. Free Next freelist Free Next A freelist! Good idea! But where do we find the memory to store this data structure? A freelist! Good idea! But where do we find the memory to store this data structure?

19 How to keep track of free memory? HB HT Q: Where do we find the memory to store a freelist data structure? A: Since the free blocks are not used for anything by the program => memory manager can use them for storing the freelist itself. HF free block size next free

20 slide 20 Why Garbage Collection? Today’s programs consume storage freely –1GB laptops, 1-4GB deskops, 8-512GB servers –64-bit address spaces (SPARC, Itanium, Opteron) … and mismanage it –Memory leaks, dangling references, double free, misaligned addresses, null pointer dereference, heap fragmentation –Poor use of reference locality, resulting in high cache miss rates and/or excessive demand paging Explicit memory management breaks high-level programming abstraction

21 Examples int *p, *q; … p = malloc(sizeof(int)); q = p; free(p); Dangling pointer in q now float myArray[100]; p = myArray; *(p+i) = … //equivalent to myArray[i] They can be hard to recognize

22 slide 22 The Perfect Garbage Collector No visible impact on program execution Works with any program and its data structures –For example, handles cyclic data structures Collects garbage (and only garbage) cells quickly –Incremental; can meet real-time constraints Has excellent spatial locality of reference –No excessive paging, no negative cache effects Manages the heap efficiently –Always satisfies an allocation request and does not fragment

23 slide 23 Summary of GC Techniques Reference counting –Directly keeps track of live cells –GC takes place whenever heap block is allocated –Doesn’t detect all garbage Tracing –GC takes place and identifies live cells when a request for memory fails –Mark-sweep –Copy collection Modern techniques: generational GC

24 slide 24 Reference Counting Simply count the number of references to a cell Requires space and time overhead to store the count and increment (decrement) each time a reference is added (removed) –Reference counts are maintained in real-time, so no “stop-and-gag” effect –Incremental garbage collection Unix file system uses a reference count for files C++ “smart pointer” (e.g., auto_ptr) use reference counts

25 Reference Counting Every cell has an additional field: the reference count. This field represents the number of pointers to that cell from roots or heap cells. Initially, all cells in the heap are placed in a pool of free cells, the free list. When a cell is allocated from the free list, its reference count is set to one. When a pointer is set to reference a cell, the cell’s reference count is incremented by 1; if a pointer to the cell is deleted, its reference count is decremented by 1. When a cell’s reference count reaches 0, its pointers to its children are deleted and it is returned to the free list.

26 01 0 0 0 Reference Counting Example 1 2 1 1

27 2 1 1 1 Reference Counting Example (Continued) 0 1

28 2 1 1 1 0 0 1 1

29 2 1 1 1 0 0 0 1 1 Returned to free list

30 slide 30 Reference Counting: Strengths Incremental overhead –Cell management interleaved with program execution –Good for interactive or real-time computation Relatively easy to implement Can coexist with manual memory management Spatial locality of reference is good –Access pattern to virtual memory pages no worse than the program, so no excessive paging Can re-use freed cells immediately –If RC == 0, put back onto the free list

31 slide 31 Reference Counting: Weaknesses Space overhead –1 word for the count, 1 for an indirect pointer Time overhead –Updating a pointer to point to a new cell requires: Check to ensure that it is not a self-reference Decrement the count on the old cell, possibly deleting it Update the pointer with the address of the new cell Increment the count on the new cell One missed increment/decrement results in a dangling pointer / memory leak Cyclic data structures may cause leaks

32 slide 32 Reference Counting: Cycles 1 root set Heap space 1 111 112 Memory leak

33 slide 33 T* obj: int cnt: 2 object of type T RefObj *ref RefObj Ref RefObj *ref x y sizeof(RefObj ) = 8 bytes of overhead per reference-counted object sizeof(Ref ) = 4 bytes Fits in a register Easily passed by value as an argument or result of a function Takes no more space than regular pointer, but much “safer” (why?) “Smart Pointer” in C++ Similar to std::auto_ptr in ANSI C++

34 slide 34 Smart Pointer Implementation template class RefObj { T* obj; int cnt; public: RefObj(T* t) : obj(t), cnt(0) {} ~RefObj() { delete obj; } int inc() { return ++cnt; } int dec() { return --cnt; } operator T*() { return obj; } operator T&() { return *obj; } T& operator *() { return *obj; } }; template class Ref { RefObj * ref; Ref * operator&() {} public: Ref() : ref(0) {} Ref(T* p) : ref(new RefObj (p)) { ref->inc();} Ref(const Ref & r) : ref(r.ref) { ref->inc(); } ~Ref() { if (ref->dec() == 0) delete ref; } Ref & operator=(const Ref & that) { if (this != &that) { if (ref->dec() == 0) delete ref; ref = that.ref; ref->inc(); } return *this; } T* operator->() { return *ref; } T& operator*() { return *ref; } };

35 slide 35 Using Smart Pointers Ref proc() { Ref s = new string(“Hello, world”); // ref count set to 1 … int x = s->length(); // s.operator->() returns string object ptr … return s; } // ref count goes to 2 on copy out, then 1 when s is auto-destructed int main() { … Ref a = proc(); // ref count is 1 again … } // ref count goes to zero and string is destructed, along with Ref and RefObj objects

36 slide 36 Mark-Sweep Garbage Collection Each cell has a mark bit Garbage remains unreachable and undetected until heap is used up; then GC goes to work, while program execution is suspended Marking phase –Starting from the roots, set the mark bit on all live cells Sweep phase –Return all unmarked cells to the free list –Reset the mark bit on all marked cells

37 Mark and Sweep Garbage Collection Algorithm pseudo code: void garbageCollect() { mark all heap variables as free for each frame in the stack scan(frame) for each heapvar (still) marked as free add heapvar to freelist } void scan(region) { for each pointer p in region if p points to region marked as free then mark region at p as reachable scan(region at p ) } Q: This algorithm is recursive. What do you think of that?

38 slide 38 root set Heap space Mark-Sweep Example (1)

39 slide 39 root set Heap space Mark-Sweep Example (2)

40 slide 40 root set Heap space Mark-Sweep Example (3)

41 slide 41 root set Heap space Mark-Sweep Example (4) Reset mark bit of marked cells Free unmarked cells

42 slide 42 Mark-Sweep Costs and Benefits Good: handles cycles correctly Good: no space overhead –1 bit used for marking cells may limit max values that can be stored in a cell (e.g., for integer cells) Bad: normal execution must be suspended Bad: may touch all virtual memory pages –May lead to excessive paging if the working-set size is small and the heap is not all in physical memory Bad: heap may fragment –Cache misses, page thrashing; more complex allocation

43 Mark-Compact Collection Remedy the fragmentation and allocation problems of mark-sweep collectors. Two phases: –Mark phase: identical to mark sweep. –Compaction phase: marked objects are compacted, moving most of the live objects until all the live objects are contiguous.

44 Heap Compaction To fight fragmentation, some memory management algorithms perform “heap compaction” once in a while. HB HT HF a b c HB HT a b d d c beforeafter

45 Mark-Compact: Advantages and Disadvantages (Continued) Advantages: –The contiguous free area eliminates fragmentation problem. Allocating objects of various sizes is simple. –The garbage space is "squeezed out", without disturbing the original ordering of objects. This ameliorates locality.

46 Mark-Compact: Advantages and Disadvantages (Continued) Disadvantages: –Requires several passes over the data are required. "Sliding compactors" takes two, three or more passes over the live objects. One pass computes the new location Subsequent passes update the pointers to refer to new locations, and actually move the objects

47 slide 47 Copying Collector Divide the heap into “from-space” and “to-space” Cells in from-space are traced and live cells are copied (“scavenged”) into to-space –To keep data structures linked, must update pointers for roots and cells that point into from-space –Only garbage is left in from-space When to-space fills up, the roles flip –Old to-space becomes from-space, and vice versa

48 Copying Collector Using the Cheney Algorithm (Continued) A simple form of copying traversal is the Cheney algorithm. The immediately reachable objects from the initial queue of objects for a breadth-first traversal. A scan pointer is advanced through the first object location by location. Each time a pointer into From-Space is encountered, the referred-to-object is transported to the end of the queue and the pointer to the object is updated.

49 Copying Collector Using the Cheney Algorithm (Continued) Multiple paths must not be copied to to-space multiple times. When an object is transported to to-space, a forwarding pointer is installed in the old version of the object. The forwarding pointer signifies that the old object is obsolete and indicates where to find the new copy.

50 slide 50 Copying a Linked List from-space to-space root A C B D forwarding address pointer [Cheney’s algorithm] A’B’C’D’ Cells in to-space are packed

51 slide 51 Flipping Spaces to-space from-space forwarding address pointer A’B’C’D’ root

52 slide 52 Copying Collector Tradeoffs Good: very low cell allocation overhead –Out-of-space check requires just an addr comparison –Can efficiently allocate variable-sized cells Good: compacting –Eliminates fragmentation, good locality of reference Bad: twice the memory footprint –Probably Ok for 64-bit architectures (except for paging) When copying, pages of both spaces need to be swapped in. For programs with large memory footprints, this could lead to lots of page faults for very little garbage collected Large physical memory helps

53 Problems with Simple Tracing Collectors Difficult to achieve high efficiency in a simple garbage collector, because large amounts of memory are expensive. If virtual memory is used, the poor locality of the allocation/reclamation cycle will cause excessive paging. Even as main memory becomes steadily cheaper, locality within cache memory becomes increasingly important.

54 Problems with Simple Tracing Collectors (Continued) With a simple semispace copy collector, locality is likely to be worse than mark-sweep. The memory issue is not unique to copying collectors. Any efficient garbage collection involves a trade-off between space and time. The problem of locality is an indirect result of the use of garbage collection.

55 slide 55 Generational Garbage Collection Observation: most cells that die, die young –Nested scopes are entered and exited more frequently, so temporary objects in a nested scope are born and die close together in time –Inner expressions in Scheme are younger than outer expressions, so they become garbage sooner Divide the heap into generations, and GC the younger cells more frequently –Don’t have to trace all cells during a GC cycle –Periodically reap the “older generations” –Amortize the cost across generations

56 slide 56 Generational Observations Can measure “youth” by time or by growth rate Common Lisp: 50-90% of objects die before they are 10KB old Glasgow Haskell: 75-95% die within 10KB –No more than 5% survive beyond 1MB Standard ML of NJ reclaims over 98% of objects of any given generation during a collection C: one study showed that over 1/2 of the heap was garbage within 10KB and less than 10% lived for longer than 32KB

57 slide 57 Young Old root set A B C D E F G Example with Immediate “Aging” (1)

58 slide 58 Young Old root set A B D E F G C Example with Immediate “Aging” (2)

59 Generational Garbage Collection: Example Old GenerationNew Generation Root Set S A B C Memory Usage

60 Generational Garbage Collection: Example (Continued) Old GenerationNew Generation Root Set S A B C Memory Usage R

61 Generational Garbage Collection: Example (Continued) Old GenerationNew Generation Root Set S A B C Memory Usage R D

62 Generational Garbage Collection: Example (Continued) This example demonstrates several interesting characteristics of generational garbage collection: –The young generation can be collected independently of the older generations (resulting in shorter pause times). –An intergenerational pointer was created from R to D. These pointers must be treated as part of the root set of the New Generation. –Garbage collection in the new generation result in S becoming unreachable, and thus garbage. Garbage in older generations (sometimes called tenured garbage) can not be reclaimed via garbage collections in younger generations.

63 Generational Garbage Collection: Issues Choosing an appropriate number of generations: –If we benefit from dividing the heap into two generations, can we further benefit by using more than two generations? Choosing a promotion policy: –How many garbage collections should an object survive before being moved to an older generation?

64 Generational Garbage Collection: Issues (Continued) Tracking intergenerational pointers: –Inter-generational pointers need to be tracked, since they form part of the root set for younger generations. Collection Scheduling –Can we attempt to schedule garbage collection in such a way that we minimize disruptive pauses?

65 Generational Garbage Collection: Multiple Generations Generation 1Generation 2Generation 3Generation 4

66 Generational Garbage Collection: Multiple Generations (Continued) Advantages: –Keeps youngest generation’s size small. –Helps address mistakes made by the promotion policy by creating more intermediate generations that still get garbage collected fairly frequently. Disadvantages: –Collections for intermediate generations may be disruptive. –Tends to increase number of inter-generational pointers, increasing the size of the root set for younger generations. Most generational collectors are limited to just two or three generations.

67 Generational Garbage Collection: Promotion Policies A promotion policy determines how many garbage collections cycles (the cycle count) an object must survive before being advanced to the next generation. If the cycle count is too low, objects may be advanced too fast; if too high, the benefits of generational garbage collection are not realized.

68 Generational Garbage Collection: Promotion Policies (Continued) With a cycle count of just one, objects created just before the garbage collection will be advanced, even though the generational hypothesis states they are likely to die soon. Increasing the cycle count to two denies advancement to recently created objects. Under most conditions, it increasing the cycle count beyond two does not significantly reduce the amount of data advanced.

69 Generational Garbage Collection: Inter- generational Pointers Inter-generational pointers can be created in two ways: –When an object containing pointers is promoted to an older generation. –When a pointer to an object in a newer generation is stored in an object. The garbage collector can easily detect promotion-caused inter- generational pointers, but handling pointer stores is a more complicated task.

70 Generational Garbage Collection: Inter- generational Pointers Pointer stores can be tracked via the use of a write barrier: –Pointer stores must be accompanied by extra bookkeeping instructions that let the garbage collector know of pointers that have been updated. Often implemented at the compiler level.

71 Generational Garbage Collection: Collection Scheduling Generational garbage collection aims to reduce pause times. When should these (hopefully short) pause times occur? Two strategies exist: –Hide collections when the user is least likely to notice a pause, or –Trigger efficient collections when there is likely to be lots of garbage to collect.

72 Generational Garbage Collection: Advantages In practice it has proven to be an effective garbage collection technique. Minor garbage collections are performed quickly. Good cache and virtual memory behavior.

73 Generational Garbage Collection: Disadvantages Performs poorly if any of the main assumptions are false: –That objects tend die young. –That there are relatively few pointers from old objects to young ones. Frequent pointer writes to older generations will increase the cost of the write barrier, and possibly increase the size of the root set for younger generations.

74 Sun HotSpot Sun JDK 1.0 used mark-compact Sun improved memory management in the Java 2 VMs (JDK 1.2 and on) by switching to a generational garbage collection scheme The heap is separated into two regions: –New Objects –Old Objects

75 New Object Region The idea is to use a very fast allocation mechanism and hope that objects all become garbage before you have to garbage collect The New Object Regions is subdivided into three smaller regions: –Eden, where objects are allocated –2 “Survivor” semi-spaces: “From” and “To”

76 New Object Region The Eden area is set up like a stack - an object allocation is implemented as a pointer increment When the Eden area is full, the GC does a reachability test and then copies all the live objects from Eden to the “To” region The labels on the regions are swapped –“To” becomes “From” - now the “From” area has objects

77 New Object Region The next time Eden fills objects are copied from both the “From” region and Eden to the “To” area There’s a “Tenuring Threshold” that determines how many times an object can be copied between survivor spaces before it’s moved to the Old Object region Note that one side-effect is that one survivor space is always empty

78 Old Object Region The old object region is for objects that will have a long lifetime The hope is that because most garbage is generated by short-lived objects that you won’t need to GC the old object region very often

79 Generational Garbage Collection EdenSS1SS2Old EdenSS1SS2Old EdenSS1SS2Old EdenSS1SS2Old First GC Second GC EdenSS1SS2Old New Object RegionOld Object Region

80 Incremental Tracing Collectors Program (Mutator) and Garbage Collector run concurrently. –Can think of system as similar to two threads. One performs collection, and the other represents the regular program in execution. Can be used in systems with real-time requirements. For example, process control systems. –allow mutator to do its job without destroying collector’s possibilities for keeping track of modifications of the object graph, and at the same time –allowing collector to do its job without interfering with mutator

81 Coherence & Conservatism Coherence: A proper state must be maintained between the mutator and the collector. Conservatism: How aggressive the garbage collector is at finding objects to be deallocated.

82 Tri-coloring White – Not yet traversed. A candidate for collection. Black – Already traversed and found to be live. Will not be reclaimed. Grey – In traversal process. Defining characteristic is that it’s children have not necessarily been explored.

83 Incremental Tracing Collectors: Overview Baker’s Algorithm –Based on Cheney’s copy-collect-algorithm –Based on tricolouring Principle: –whenever mutator access a pointer to a white object the object is coloured grey –this ensures that mutator never points to a white object, hence part 1 of the invariant holds At every read mutator does, the invariant must be checked (expensive), if this is the case the object must be forwarded (using the standard forward algorithm) to to-space There are variants: –read-barrier –write-barrier –synchronising on page-level

84 Comparing Garbage Collection Algorithms Directly comparing garbage collection algorithms is difficult – there are many factors to consider. Some factors to consider: –Cost of reclaiming cells –Cost of allocating cells –Storage overhead –How does the algorithm scale with residency? –Will user program be suspended during garbage collection? –Does an upper bound exist on the pause time? –Is locality of data structures maintained (or maybe even improved?)

85 Comparing Garbage Collection Algorithms (Continued) Zorn’s study in 1989/93 compared garbage collection to explicit deallocation: –Non-generational Between 0% and 36% more CPU time. Between 40% and 280% more memory. –Generational garbage collection Between 5% to 20% more CPU time. Between 30 and 150% more memory. Wilson feels these numbers can be improved, and they are also out of date. A well implemented garbage collector will slow a program down by approximately 10 percent relative to explicit heap deallocation.

86 Garbage Collection: Conclusions (Continued) Despite this cost, garbage collection a feature in many widely used languages: –Lisp (1959) –SML, Haskel, Miranda (1980-) –Perl (1987) –Java (1995) –C# (2001) –Microsoft’s Common Language Runtime (2002)

87 Different choices for different reasons JVM –Sun Classic: Mark, Sweep and Compact –SUN HotSpot: Generational (two generation + Eden) -Xincgc an incremental collector that breaks that old-object region into smaller chunks and GCs them individually -Xconcgc Concurrent GC allows other threads to keep running in parallel with the GC –BEA jRockit JVM: concurrent, even on another processor –IBM: Improved Concurrent Mark, Sweep and Compact with a notion of weak references –Real-Time Java Scoped LTMemory, VTMemory, RawMemory.Net CLR –Managed and unmanaged memory (memory blob) –PC version: Self-tuning Generation Garbage Collector –.Net CF: Mark, Sweep and Compact

88 Garbage Collection: Conclusions Relieves the burden of explicit memory allocation and deallocation. Software module coupling related to memory management issues is eliminated. An extremely dangerous class of bugs is eliminated. The compiler generates code for allocating objects The compiler must also generate code to support GC –The GC must be able to recognize root pointers from the stack –The GC must know about data-layout and objects descriptors The choice of Garbage Collector algorithm has to take many factors into consideration

Download ppt "Slide 1 Heap Management. slide 2 Quote of the Day “Manually managing blocks of memory in C is like juggling bars of soap in a prison shower: It's all."

Similar presentations

Ads by Google