RCGC can be more attractive Reference counting example Root set Heap - - PowerPoint PPT Presentation

rcgc can be more attractive reference counting example
SMART_READER_LITE
LIVE PREVIEW

RCGC can be more attractive Reference counting example Root set Heap - - PowerPoint PPT Presentation

RCGC can be more attractive Reference counting example Root set Heap space 1 1 2 1 1 1 1 2 1 2 Reference counting example Root set Heap space 1 1 2 1 0 1 1 2 1 3 Reference counting example Root set Heap space 1 1 2 1 0 1


slide-1
SLIDE 1

RCGC can be more attractive

slide-2
SLIDE 2

Reference counting example

2

Root set

Heap space

1 1 1 1 1 1 2 2 1

slide-3
SLIDE 3

Reference counting example

3

Root set

Heap space

1 1 1 1 1 2 2 1

slide-4
SLIDE 4

Reference counting example

4

Root set

Heap space

1 1 1 1 2 1 1

slide-5
SLIDE 5

Reference counting example

5

Root set

Heap space

1 1 1 1 2 1 1

slide-6
SLIDE 6

Reference counting example

6

Root set

Heap space

1 1 1 1 1 1 1

slide-7
SLIDE 7

Reference counting example

7

Root set

Heap space

1 1 1 1 1 1 1

Memory leak

slide-8
SLIDE 8

Deficiencies of RCGC

Cost of removing last pointer unbounded Total overhead of adjusting RCs significantly greater

than that of tracing collectors

Substantial space overhead Inability to reclaim cyclic data structures

8

slide-9
SLIDE 9

How do we overcome shortcomings?

Problem

Inability to reclaim cyclic data structures

RC of objects in cycle never get to zero Cyclic data structures are common

At application level

Back pointers (e.g., doubly linked list) Back edge in the link to a back table chain

At system level

Functional languages use cycles to express recursion

Memory leak

Solution

Cyclic reference counting

9

slide-10
SLIDE 10

Functional programming languages

Cycles created in well defined manner

Treat specially

Created only by recursive definitions References to such structures must follow these restrictions:

Circular structure created all at once Use of proper subset that does not include root is copied as

independent structure, not shared

Cycle‐closing pointers to head of cycles are tagged

Ensure cycle is treated as single entity Access to cycle is only through pointer to its root

10

slide-11
SLIDE 11

Bobrow’s technique

Distinguish between pointers internal to the cycle

from external references.

External pointers counted as pointers to structure as a

whole

Internal pointers not counted Idea:

Collect groups of objects Programmer assign objects to groups Each group is reference counted Group membership determined by object’s address

11

slide-12
SLIDE 12

Bobrow’s technique

update(R, S){ T = *R gr = group_no(R) if gr != group_no(S) // external reference increment_groupRC(S) if gr != group_no(T) // external reference decrement_groupRC(T) if groupRC(T) == 0 reclaim_group(T) *R = S }

12

slide-13
SLIDE 13

Weak pointer Algorithms

Distinguishing cycle closing pointers (weak pointers)

from other references (strong pointers)

Basis: Two invariants:

Each live object must be reachable from a root by a chain

  • f strong pointers (strongly reachable)

Strong pointers must never be allowed to form cycles Objects have 2 RC

Strong RC (SRC)

Pointers to new objects

Weak RC (WRC)

Closing link on pointer copy

13

slide-14
SLIDE 14

Weak‐pointer algorithms

// Brownbridge’s new new() { if freeList == empty abort “Memory exhausted” newCell = allocate() SRC(newCell) = 1 return strong(newCell) }

14

//Salkild’s update update(R, S){ WRC(S) = WRC(S) + 1 delete(*R) *R = S weaken(*R) }

slide-15
SLIDE 15

Disadvantages of weak pointer alg.

Cyclic structures can be incorrectly discarded Algorithm fails to terminate in some cases

A suicide pass searches for and breaks strong cycles

Pathological case can lead to exponential time

complexity in the worst case

Space overhead is high: 2 RC fields

15

slide-16
SLIDE 16

Hybrid algorithms

Most objects freed by RC

Ideal candidates are uniquely referenced

Cyclic structures freed by mark‐sweep collector

Shared objects are cycle candidates

Lin’s algorithm (Lazy tracing of graphs)

Do not trace sub‐graph every time shared pointer is

deleted

Save values of deleted pointers in control set

Traps pointer writes Uses extra field to colors objects At suitable time search control set for garbage

16

slide-17
SLIDE 17

Lin’s Algorithm

Uses for colors for objects Black:

  • Active objects are painted black; including new objects

White:

  • Garbage and free cells are painted white

Gray:

  • Cells visited in marking phase are painted grey, have to be visited again

Purple:

  • Cells that may be part of isolated cycles, have to be traversed by collector

17

slide-18
SLIDE 18

Lin’s algorithm

When pointer to shared object deleted, object painted

purple

Address put in control set Avoids duplicate in control set

New objects are allocated black Both arguments to update() must be removed from

control set to prevent them from being mark‐swept

They must be active Painted black Control set used to identify potential free space

Mark‐sweep is used if picking object from it is still purple

18

slide-19
SLIDE 19

Details of Lin’s algorithm

// New objects allocated black delete(T) { RC(T) = RC(T) – 1 if RC(T) == 0 color(T) = black for U in children(T) delete(*U) free(T) else if color(T) != purple if control_set is full gc_control_set() color(T) = purple push(T, control_set) }

19

update(R, S){ RC(S) = RC(S) + 1 color(R) = black color(S) = black delete(*R) *R = S }

slide-20
SLIDE 20

Details of Lin’s algorithm

Discussion of mark‐sweep algorithm in text Example helps explain algorithm Will differ discussion until we explore mark‐sweep

20