Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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

3 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

4 Create Reference to Heap from Stack Heap 1 | | Pointer P Semidynamic Memory Static Memory

5 Reference Heap 2 from Heap 1 1 | | Heap 1 | | Pointer P Semidynamic Memory Static Memory

6 Reference Heap 1 from Heap 2 1 | | Heap 2 | | Pointer P Semidynamic Memory Static Memory

7 Destroy Stack Pointer P 1 | | Heap 1 | | Pointer P Semidynamic Memory Static Memory X

8 Destroy Stack Pointer P 1 | | Heap 1 | | Semidynamic Memory Static Memory Circular Reference Heap space will never be deallocated

9 Reference Counts (Example 2) 1 23 000null… Reference Counter (RC) p q free list garbage 0

10 If p->next = null is Executed 1 12null 000 … Reference Counter (RC) p q free list garbage

11 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.

12 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

13 Set All Mark Bits to 0 0 00 0 0 0null… Mark Bit (MB) p q free listnull

14 Set Accessible Mark Bits to 1 0 01 1 0 1null… Mark Bit (MB) p q free listnull

15 Sweep (Reclaim Space) 0 01 1 0 1null… Mark Bit (MB) p q free list

16 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)

17 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

18 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)

19 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

20 Stop and Copy (First Half) p q null … free list To Space From Space

21 Stop and Copy (Second Half) p q null … free list To Space From Space …

22 Other Names Stop and Copy is also referred to as: –Semi-Space Method –Copy Collection

23 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

24 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

25 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


Download ppt "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."

Similar presentations


Ads by Google