Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free.

Similar presentations


Presentation on theme: "Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free."— Presentation transcript:

1 Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free or Pascal ’ s new/dispose *Programmer explicitly requests chunk of memory _Object may be explicitly free or garbage collected _Object may be explicitly free or garbage collected *Implemented as library routines to manage pool of storage _Must be fast since heavily used _Must be fast since heavily used _Should not require much extra memory as overhead _Should not require much extra memory as overhead

2 Simple Alloc *Allocator needs to record information about size of object _Store header immediately before object _Store header immediately before object _ struct header { _ struct header { struct header *next; struct header *next; int size; int size; } *alloc (int size){ for (p=free_list; p!=NULL ; p=p->next) for (p=free_list; p!=NULL ; p=p->next) if (p->size >size) if (p->size >size) then (remove from list and return) then (remove from list and return) allocate n new blocks of size s; allocate n new blocks of size s; add n-1 new blocks to free list; add n-1 new blocks to free list; return other block; return other block;}

3 Simple Free *To free block return it to the free list *free (char *block) { struct header *h =block – sizeof (header); struct header *h =block – sizeof (header); h->next=freelist; h->next=freelist; freelist=h; freelist=h; } *Allocate will fail if program writes over header

4 Shortcomings of Simple Approach:- *Wasteful of space _alloc grabs first block that fits (first-fit) _alloc grabs first block that fits (first-fit).Could use best-fit if memory is scarce.Could use best-fit if memory is scarce _Pre-allocates N blocks that may not be used _Pre-allocates N blocks that may not be used.Have fewer possible sizes (power of 2).Have fewer possible sizes (power of 2) _Never coalesces adjacent blocks to make larger blocks _Never coalesces adjacent blocks to make larger blocks.Leads to memory fragmentation.Leads to memory fragmentation.Need memory map to find adjacent free blocks.Need memory map to find adjacent free blocks*Slow _Should have separate free list for different sized objects _Should have separate free list for different sized objects

5 Garbage Collection *In large complex program, must be careful to free allocated memory _A small memory leak can crash long-running program _A small memory leak can crash long-running program _30% of code is devoted to reclaiming storage _30% of code is devoted to reclaiming storage *Alternative is garbage collection _The system detects when object cannot be used since there are no pointers to it _The system detects when object cannot be used since there are no pointers to it.Conservative approximation of when object will not be used.Conservative approximation of when object will not be used.Automatically reclaim the space.Automatically reclaim the space.Programmer discards object by discarding pointers.Programmer discards object by discarding pointers

6 Reference Counting *add field to each object that records number of pointers to object *When pointer to object is created, increment field _Pointer created when object created or existing pointer copied _Pointer created when object created or existing pointer copied *When pointer to object is discarded,decrement field _Pointer discarded when overwritten _Pointer discarded when overwritten *When count fails to zero, object is garbage *Problems:_ _Need extra instructions throughout program _Need extra instructions throughout program _Adds 20-30% to execution time _Adds 20-30% to execution time _Does not reclaim cycles _Does not reclaim cycles 11

7 Mark & Sweep GC *Start marking memory at the root pointers _All global and local variables _All global and local variables *Follow all pointers, marking the memory in each object as in use _Need to know size and type of all objects _Need to know size and type of all objects *When finished, unmarked memory is garbage that can be reclaimed *Problems:- _Time is proportional to amount of memory in use _Time is proportional to amount of memory in use _Need extra memory for marks and traverse pointers _Need extra memory for marks and traverse pointers Local Variables Local Variables Global Variables Global Variables

8 Generation Scavenging *Earlier techniques did not work well for large memories _Long pauses in garbage collect _Long pauses in garbage collect _Caused a lot of paging _Caused a lot of paging *Measurements showed most objects live a short time _Programs create many short-lived objects and a few _Programs create many short-lived objects and a few long-lived objects long-lived objects.Newly allocated objects are likely to become garbage.Newly allocated objects are likely to become garbage shortly shortly _Separate objects by how long they have lived and collected young objects more frequently _Separate objects by how long they have lived and collected young objects more frequently Generations Generations 123Tenured


Download ppt "Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free."

Similar presentations


Ads by Google