Professional Documents
Culture Documents
Lang Proc Lec7
Lang Proc Lec7
The first method is to free each block of heap storage as soon as it becomes unused. This method is generally implemented by means of reference counters counters !hich record ho! many pointers to this block are still in e"istence. #hen a block is first allocated, its reference counter is set to 1. $ach time another link is made pointing to this block, the reference counter is incremented% each time a link to it is broken, the reference counter is decremented. #hen the count reaches 0, the block is inaccessible and hence unusable. &t this point it is returned to the free list.
2
Disadvantages:
'f the blocks that are allocated form a circular structure, their reference counts !ill al!ays remain set to at least 1, and none of the blocks !ill e(er be freed, e(en if all pointers from outside the circular structure to blocks in the circular list are destroyed )ermanent garbage
Solution : *imply prohibit circular or recursi(e structures. 'n a number of applications a circular structure is the most natural and reasonable one to use. Solution ! : )ermanent garbage block can be marked !ith special flags indicating that these blocks re+uire special treatment for garbage collection
,
Solution " : .se a special list head !hose reference counter counts only references from outside the circle. &ll access to blocks in the circular structure are made through this list head. /ra!back The o(erhead in(ol(ed in maintaining the reference counts. This is a more serious ob0ection because it can increase the e"ecution time of the program significantly
Gar#age $olle%tion
&(ailable storage is almost e"hausted )articular re+uest cannot be met &mount of a(ailable storage has decreased beyond a certain predefined point
Gar#age $olle%tion
The first phase consists of tracing all the access paths from all the program and system (ariables through the allocated blocks. $ach block accessed in this !ay is marked The second phase consists of mo(ing through the entire segment of memory, resetting the marks of the marked blocks, and returning to the free list e(ery allocated block that has not been marked.
Gar#age $olle%tion
$(ery time the garbage collector is in(oked, all list and block structures are in a stable state !ith pointers pointing !here they should
4ther!ise the garbage collector !ill not be able to make the proper tracing of all the reference paths, and either some garbage !ill remain uncollected or, more seriously, blocks still in use !ill be freed
The garbage collector can be in(oked by the system at almost any point in program e"ecution, it is re+uired that the use of pointers be disciplined
Gar#age $olle%tion
The algorithm should begin by disabling the garbage collector so that it cannot be in(oked !hile the algorithm is e"ecuting
Gar#age $olle%tion
Disadvantages: 8osts increase as the amount of free storage decreases The reason for the in(erse relationship is, !hen there is little free storage, there is a lot of allocated storage and hence the marking process has to trace through many blocks
*olution7 the collector is in(oked !ell before memory becomes completely allocated
Gar#age $olle%tion
Algorit&'s (or 'ar)ing: 9ere it is assumed that blocks !hich contain pointers to other blocks, thus forming a list type structure
*'B$<)= is the size of block ) *&C$<)= is a field to be used D&RA<)= is a field initially set
10
to false
July 29, 2011
Gar#age $olle%tion
:asic to the algorithm is the strategy of follo!ing each access path until it terminates.
& path is follo!ed until there are no more ?'@A fields to process or until a block is e"amined !hich is already marked because it is also on some other pre(iously marked path
&s this for!ard path is follo!ed, the ?'@A fields !hich !e tra(erse are set to point to the block from !hich !e came <for retracing=
The *&C$ field is used to record !hich of the se(eral ?'@A fields in a block is currently re(ersed
11
Gar#age $olle%tion
;inally the same path is used for return and resetting the mark fields
9ence !e follo! one to its end, re(ersing ?'@A fields, and then trace it back!ard, resetting ?'@A fields and follo!ing still further sub paths.
This process can be altered so as to use a stack to keep track of !hat nodes ha(e been marked rather than temporarily ad0usting the link fields.
The stack approach simplifies the algorithm% ho!e(er, e"tra space is re+uired for the stack implementation
12