310 likes | 557 Views
Garbage Collection. Heap Management Reachability Reference counting garbage collectors. Heap management. Heap is a portion of the memory where the data lives indefinitely Not associated with function activation The allocated memory becomes reusable
E N D
Garbage Collection Heap Management Reachability Reference counting garbage collectors
Heap management • Heap is a portion of the memory where • the data lives indefinitely • Not associated with function activation • The allocated memory becomes reusable • When the programmer explicitly frees it • Program execution is over • Memory manager allocates and deallocates space within heap
Features of a good memory manager • Space Efficiency • Program efficiency • Low overhead
Locality in programs • Temporal Locality • A program is said to have temporal locality, if the memory locations it accesses are likely to be accessed within a short time • Spatial Locality • A program is said to have spatial locality, if memory locations close to the location accessed are likely to be accessed within a short period of time • Locality allows us to take advantage of the memory hierarchy
Examples • Spatial locality • Mostly the next instruction in a program • Compiler can place the next basic block in the cache to improve the spatial locality • Temporal locality • A portion of the program which is executed when a condition is mostly true
Memory manager • Allocation • Searches the available chunks of memory • Allocates the chunk according to the requested size • If no such chunk is available, it returns null • Deallocation • Returns the freed memory (freed explicitly by the programmer) • The returned memory is reused
fragmentation • As a result of allocation and deallocation of memory, a large number of small sized chunks are created in the heap memory. • These are called holes • Contiguous holes are merged to form a bigger chunk of memory • Memory manager maintains the information and does the allocation of the appropriate sized chunk to the user object.
Allocation policies • First fit • Takes less time • Less efficient • Best fit • Efficiently utilized the available chunks • Binning is used to maintain information about the sizes of the chunks • Leaves the smallest sized hole
Managing free space • Free adjacent chunks are merged to form a larger chunk • A simple allocation deallocation scheme is to keep a bit map, with one bit for each chunk in the bin (1: occupied, 0 free) • Data structures to support merging of adjacent free chunks • Boundary tags • A doubly linked list, Embedded Free list
Boundary tag 0 200 200 0 0 100 100 0 1 120 120 1 0 50 50 0 Chunk A Chunk B Chunk C Chunk D • Contiguous free chunks can be merged to form a larger chunk • If any occupied chunk is freed, then we can merge with one of its free neighbors • Automatic garbage collection can eliminate fragmentation altogether if it moves all the allocated objects to contiguous storage
Manual Deallocation Requests • C and C++ allow manual memory deallocation of data • User is assumed to be utilizing the memory efficiently by knowing the need of the data object in future.(difficult to predict !!!) • Any data that is not needed can be freed by the user explicitly
Problems caused by manual memory management • Manual memory management is error prone. • The two frequent errors are • Dangling pointer dereference error • Memory leak error • Automatic garbage collection gets rid of memory leaks by deallocating all the garbage
Dangling pointer: associated problems • May refer to a location that has been deallocated and available for reallocation • If reallocated to a new variable, the same pointer reference may access a value that is not meant for it • The same pointer may even update the value arbitrarily.
Garbage collection • The data that cannot be referenced is generally known as garbage. • Garbage collection is the reclamation of the chunks of storage holding objects that can no longer be accessed by a program • The type of the objects known at run time helps the garbage collector free the appropriate sized space in the heap.
Mutator • A user program is referred to as the mutator. • Mutator modifies the collection of objects by acquiring space from the memory manager. • The mutator may introduce and drop references to the objects • Objects become garbage when the mutator program cannot “reach” them.
Basic operations by a mutator that changes the set of reachable objects • Object allocation • Memory manager returns the reference to each newly allocated chunk of memory • Adds new members to the set of reachable objects • Parameter Passing and Return Values • References of objects are passed and returned • Objects pointed to by these references remain reachable
Basic operations by a mutator that changes the set of reachable objects • Reference Assignments • Assignments of the form p=q has two effects • First: the original reference in p is lost • If this object had the reference to the next object, the reference to it is also lost (all others whose references are contained within this object are also lost) • Second: p references the object pointed to by q • Procedure Returns • If the activation frame holds any reference, then it is lost when the procedure activation is over
Reachability • The transitions are traced as the reachable objects turn unreachable OR • Periodically locate all the reachable objects and then infer that all the other objects are unreachable
Reference Counting • A count of the references to an object is maintained • When the count goes to zero, the object becomes unreachable
Maintaining return counts(RC) • Object Allocation: RC of new object is set to 1 • Parameter Passing: RC of each object passed to the the procedure is incremented by 1 • Reference Assignments: for u=v, (u,v being the refs.) the RC of the object referred by v goes up by 1, and the count for the old object referred to by u goes down by 1.
Maintaining return counts(RC) • Procedure returns: when a procedure exits, • the references held by all local variables should be decremented by 1 • If multiple variables hold references of the same object, then the objects count must be decremented once for each such reference • Transitive Loss of Reachability • When the reference count of an object becomes zero, the count of each object pointed to by a reference within that object Give Examples…. Give Examples….
Disadvantages of Reference Counting • Cannot collect unreachable, cyclic data structures • Overhead of RC is high because additional operations are introduced with each reference assignment • This overhead is proportional to the amount of computation in the program and not to just to the number of objects referred
Advantage • Garbage collection is performed in an incremental fashion
Class assignment What happens to the reference counts of the objects? F When Pointer from A to B is deleted A B C When Pointer from B to F is deleted E D
Reading Assignment • Mark and Sweep Garbage collection