reference counters associate a counter with each heap item whenever a heap item is created, such as...
Post on 22-Dec-2015
225 views
TRANSCRIPT
Reference Counters• Associate a counter with each heap item• Whenever a heap item is created, such as by a new
or malloc instruction, initialize the counter to 1.• Whenever a new pointer to the heap item is
created, increment the counter by 1• Whenever a pointer to the heap item is destroyed,
decrement the counter by 1• The heap item is “alive’ so long as the counter is >
0• When the counter decrements to 0, deallocate the
heap item
Advantages of Reference Counters
• It is easy to implement
• Dangling objects are deallocated the instant they are no longer needed
• No lengthy periodic algorithm execution
• Transparent to user
Problems with Reference Counters
• Space for to store counter variable is wasted for each heap item
• How do you know how much heap space to deallocate when reclaiming heap space?– Space wasted to store size of heap item.
• Circular references in heap can cause algorithm to fail
Destroy Stack Pointer P1 | |
Heap
1 | |
Semidynamic Memory
Static Memory
Circular
Reference
Heap space
will never be
deallocated
Mark and Sweep
• Mark and Sweep is a garbage collection algorithm
• Associate a bit called a mark bit with each heap item
• If mark bit is set to 0, heap item is assumed to be a unreferenced (i.e., a dangling object)
• If mark bit is set to 1, heap item is assumed to be referenced.
Mark and Sweep Algorithm• Mark component of algorithm
– Set all heap item mark bits to 0 (unreferenced)– Follow each pointer on the semidynamic stack to heap
item they point to and set heap item mark bit to 1– Recursively follow any pointers in heap items that are
set to 1 to other heap items and set their mark bits to 1– Finish when all possible pointer links originating from
semidynamic stack have been exhausted
• Sweep component of algorithm– Heap items with mark bit set to 0 have no references
from the semidynamic stack – they are dangling objects– Go through heap and remove heap items that still have
mark bit set to 0
Advantages of Mark and Sweep
• All dangling objects will eventually be removed
• Instead of associating a counter with each item we only need to associate a bit (mark bit) – Therefore, there could be less wasted space (but probably not)
Disadvantages of Mark and Sweep
• How do you find pointers in heap? They will be at varyingly different locations within the heap item (plain pointer, array of pointers, pointer fields in a structure)
• How do you find pointers in semidynamic stack? Same problem as in heap
• How do you know how much heap space to deallocate (same problem as was encountered in reference counters)
• You invoke algorithm only when you are running out of heap space. As you run out of heap space, performance degrades. Running the mark and sweep algorithm not only degrades performance further, but for quite a long time the CPU is dedicated exclusively to the algorithm. During this period, the screen freezes for an eternity
Stop and Copy (First Half)
• Use half of heap memory at a time• The half in use is in turn divided into two
parts– First part contains cells that have been allocated– Second part contains cells available for
allocation
• Memory is allocated sequentially from available cells until it runs out (free space is empty)
Stop and Copy (Second Half)• When memory in first half is used up, switch to
second half• Copy only accessible cells from current half to
beginning of second half• All activity now switches to second half• New memory is allocated starting from first
location after the copied space• The copying processes touches only accessible
heap items
Performance Trade-Offs• Assume
– R is the number of active heap objects– H is heap size– r is “residency” defined as the ratio of R to the heap
size– e is efficiency measured as the amount of memory
reclaimed per unit time
• e is better for Stop and Copy if r is much less than H/2
• e is better for Mark and Sweep if r is much greater than H/2
Possible Strategies
• Dozens of garbage collection algorithms are in use today
• Most are more complex than the three strategies we have discussed
• Hybrid Strategies– If R is less than H/2 select Stop and Copy– If R is greater than H/2 select Mark and Sweep
Java Strategies• In Sun’s Java system, the garbage collector runs as
a low-priority thread• The thread executes when processor demand from
other threads is low• This reduces the number of garbage collection
calls during peak demand periods• The programmer can explicitly call the garbage
collector using the system call System.gc() • This invokes garbage collection immediately,
regardless of the state of the heap