Presentation is loading. Please wait.

Presentation is loading. Please wait.

Heap Management.

Similar presentations

Presentation on theme: "Heap Management."— Presentation transcript:

1 Heap Management

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 Major Areas of Memory Static area Run-time stack Heap
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 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”. 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

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 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 Example of Garbage class node { int value; p = new node(); 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) { ... } (1) (2) (3) (4)

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

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! SB Stack memory area ST Stack grows downward Heap can expand upward HT Heap memory area HB

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? SB Free HT Allocated Mixed: Allocated and Free reuse? ST Free HB

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

19 How to keep track of free memory?
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. HT HF free block size HF next free HB

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 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 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 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 Reference Counting Example
1 2 1 1 1

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

28 Reference Counting Example (Continued)
1 2 1 1 1 1

29 Reference Counting Example (Continued)
1 Returned to free list 1 2 1 1 1

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 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 Reference Counting: Cycles
Heap space Memory leak root set 1 1 1 1 1 1 2 1

33 “Smart Pointer” in C++ Similar to std::auto_ptr<T> in ANSI C++
object of type T Ref<T> RefObj<T> x T* obj: int cnt: 2 RefObj<T> *ref y RefObj<T> *ref 智能指针的关键技术:在于构造栈上对象的生命期控制                 堆上构造的对象的生命期.因为在智能指针的内部,存储                 着堆对象的指针,而且在构析函数中调用delete行为. sizeof(RefObj<T>) = 8 bytes of overhead per reference-counted object sizeof(Ref<T>) = 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?)

34 Smart Pointer Implementation
template<class T> class Ref { RefObj<T>* ref; Ref<T>* operator&() {} public: Ref() : ref(0) {} Ref(T* p) : ref(new RefObj<T>(p)) { ref->inc();} Ref(const Ref<T>& r) : ref(r.ref) { ref->inc(); } ~Ref() { if (ref->dec() == 0) delete ref; } Ref<T>& operator=(const Ref<T>& 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; } }; template<class T> 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; } }; 智能指针的关键技术:在于构造栈上对象的生命期控制                 堆上构造的对象的生命期.因为在智能指针的内部,存储                 着堆对象的指针,而且在构析函数中调用delete行为.

35 Using Smart Pointers Ref<string> proc() {
Ref<string> 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<string> a = proc(); // ref count is 1 again } // ref count goes to zero and string is destructed, along with Ref and RefObj objects

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 Mark-Sweep Example (1) Heap space root set

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

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

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

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. before after HT a HF HT a b c b c d d HB HB

45 Mark-Compact: Advantages and Disadvantages (Continued)
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)
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 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 Copying a Linked List [Cheney’s algorithm] Cells in to-space
from-space pointer root A forwarding address B C D to-space A’ B’ C’ D’ Cells in to-space are packed

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

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 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 Objects are segregated into different areas of memory based on their age. Areas containing newer objects are garbage collected more frequently. After an object has survived a given number of collections, it is promoted to a less frequently collected area.

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 Example with Immediate “Aging” (1)
C root set A D Young B E F Old G

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

59 Generational Garbage Collection: Example
Initial state. Point out memory usage, memory divisions. We are going to overwrite the first root node with a pointer to a new object, R. S Root Set Memory Usage Memory Usage Old Generation New Generation

60 Generational Garbage Collection: Example (Continued)
Note memory is almost used up. R now makes a request for a new node, D, to be created but there is insufficient memory. A garbage collection is initiated. S Root Set Memory Usage Memory Usage Old Generation New Generation

61 Generational Garbage Collection: Example (Continued)
All objects in New generation are moved to old generation. Garbage nodes are freed. Allocation for D is successfully completed. S Root Set Memory Usage Memory Usage Old Generation New Generation

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

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
New Object Region Old Object Region Eden SS1 SS2 Old First GC Eden SS1 SS2 Old Eden SS2 SS1 Old Second GC Eden SS2 SS1 Old Eden SS2 SS1 Old

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 Mutator: The program running alongside the garbage collection system.

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. 增量收集算法的基础仍是传统的标记清除和复制算法。增量收集算法通过对进程间冲突的妥善处理,允许垃圾收集进程以分阶段的方式完成标记、清理或 复制工作。增量算法的具体机制可以用不同,但其思想都是把堆栈分为多个域,每次仅从一个域收集垃圾,从而造成较小的应用程序中断,其中比较著名的是火车 (Train)机制与三色(三元色,Tri-Color)标记机制。以下介绍一下三元色机制的概念。          在垃圾收集中,所有的对象可能出于其下的三种颜色中的一种:黑色,灰色与白色。各自代表:          黑色(black):表示此节点已被访问到,且是live的(其子节点也被访问到),不会被回收(reclaimed);          灰色(gray):表示此节点已在处理列表中,但其子节点尚未被检查;          白色(white):表示此节点没有访问过,在GC完成后被视为垃圾对象;          在标记之前先将堆中所有的分配单元置成白色,然后按深度优先算法遍历每一个单元。当垃圾收集器遍 历一个分支的时候,如果一个分配单元及与之相关联的单元都被遍历到,则将其标记成黑色。如果一个单元被遍历到,但是与之相关联的单元尚未被遍历,则将该单 元标记成灰色。这时,垃圾收集器将继续遍历与该灰色单元相关联的单元,直到这些相关联的单元全部被遍历到,才能将这个灰色单元标记成黑色。最后当所有被遍 历到的单元都被标记成黑色的时候, 将堆中被标记成白色的分配单元回收。          收集器处理过程中,去检测节点是否是能到达的(reachable),此活动中依靠找到从灰色节点到白色节点的边缘与把白色节点染色成灰色。因此每步跟踪包含选择一个灰色节点并且把其白色子节点标记成灰色。 [1] 灰色节点只有可能连接到另外的灰色的节点或黑色的节点,那么把它标记为黑色; [2] 当没有灰色的节点剩下的时候,所有可到达的部分都已找到,余下的白色的节点都被回收。          注意,黑色节点不可能有白色的子节点!也就是说黑色节点不能指向白的节点,这一点叫做不变量(invariant)。 为了保护与防止此种情况的发生,引入了读屏障(Read Barrier)与写屏障(Write Barrier)。 读屏障:当mutator存储一个从黑色转到白色的节点时引用,把其父或子节点置成灰色;(Detects an attempt to read a white object and immediately colors it gray) x.f = y;    ->     black2gray(x).f = y; 写屏障:当mutator请求一个白色节点时,把它置成灰色;(Traps attempts to write a pointer into an object) x.f = y;    ->     x.f = white2gray(y);

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?) For reference counters, residency is not an issue. Tracing collectors will be invoked more frequently if the occupancy of the heap is high.

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 "Heap Management."

Similar presentations

Ads by Google