0% found this document useful (0 votes)
37 views12 pages

Lang Proc Lec7

Run-time storage organization and management techniques include free-as-you-go storage release using reference counting and garbage collection. Garbage collection involves tracing all access paths from program variables to allocated blocks during a marking phase and then freeing unmarked blocks in a second phase. It is invoked when available storage is low to collect unused memory and make it available again without programmer intervention.

Uploaded by

Sanjay Raichura
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views12 pages

Lang Proc Lec7

Run-time storage organization and management techniques include free-as-you-go storage release using reference counting and garbage collection. Garbage collection involves tracing all access paths from program variables to allocated blocks during a marking phase and then freeing unmarked blocks in a second phase. It is invoked when available storage is low to collect unused memory and make it available again without programmer intervention.

Uploaded by

Sanjay Raichura
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

RUN-TIME STORAGE ORGANIZATION AND MANAGEMENT

July 29, 2011

Run time storage organization and management

Free-as-You-Go Storage Release

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

July 29, 2011

Free-as-You-Go Storage Release

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
,

July 29, 2011

Free-as-You-Go Storage Release

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

July 29, 2011

Gar#age $olle%tion

This method makes use of a special routine !hich is in(oked !hene(er

&(ailable storage is almost e"hausted )articular re+uest cannot be met &mount of a(ailable storage has decreased beyond a certain predefined point

July 29, 2011

Gar#age $olle%tion

The garbage collection algorithm generally has t!o phases2

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.

July 29, 2011

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

July 29, 2011

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

#hat happens if algorithm stops due to lack of storage7

July 29, 2011

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

July 29, 2011

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

;R$$<)= contains a (alue

e+ual to 1 > the number of ?'@A fields

*'B$<)= is the size of block ) *&C$<)= is a field to be used D&RA<)= is a field initially set
10

in the marking process


:lock structure for garbage collection

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

July 29, 2011

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

July 29, 2011

12

You might also like