1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall...

26
1 Object Lifetime and Garbage Object Lifetime and Garbage Collection Collection (Section 10.3 & 7.7) (Section 10.3 & 7.7) CSCI 431 Programming Languages CSCI 431 Programming Languages Fall 2003 Fall 2003 A modification of slides A modification of slides developed by Felix Hernandez- developed by Felix Hernandez- Campos and Campos and Mircea Nicolescu Mircea Nicolescu

Transcript of 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall...

Page 1: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

11

Object Lifetime and Garbage Object Lifetime and Garbage CollectionCollection

(Section 10.3 & 7.7)(Section 10.3 & 7.7)

CSCI 431 Programming LanguagesCSCI 431 Programming Languages

Fall 2003Fall 2003

A modification of slides developed by Felix A modification of slides developed by Felix Hernandez-Campos and Hernandez-Campos and Mircea NicolescuMircea Nicolescu

Page 2: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

22

Fundamental Concepts in OOPFundamental Concepts in OOP

• EncapsulationEncapsulation– Data AbstractionData Abstraction– Information hidingInformation hiding– The notion of class and objectThe notion of class and object

• InheritanceInheritance– Code reusabilityCode reusability– Is-a vs. has-a relationshipsIs-a vs. has-a relationships

• PolymorphismPolymorphism– Dynamic method bindingDynamic method binding

Page 3: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

33

Object Lifetime: ConstructorsObject Lifetime: Constructors

• ContructorsContructors are methods used to are methods used to initializeinitialize the the content of an objectcontent of an object

– They do not allocate spaceThey do not allocate space

• Most languages allow multiple constructorsMost languages allow multiple constructors– They are distinguished using different names or different They are distinguished using different names or different

parameters (type and/or number)parameters (type and/or number)– Java and C++ overload the constructor name, so the Java and C++ overload the constructor name, so the

appropriate methods is selected using the number and the appropriate methods is selected using the number and the type of the argumentstype of the arguments» Rectangle r;Rectangle r;» Invokes the parameterless constructorInvokes the parameterless constructor

– Smalltalk and Eiffel support different constructor namesSmalltalk and Eiffel support different constructor names

Page 4: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

44

Constructors in EiffelConstructors in Eiffel

Complex Complex Numbers Numbers

ClassClass

Constructors (!! equals Constructors (!! equals new)new)

Explicit Constructor DeclarationExplicit Constructor Declaration

Page 5: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

55

References and ValuesReferences and Values

• Some OO languages use the reference modelSome OO languages use the reference model– More elegantMore elegant– Extra level of indirection on every accessExtra level of indirection on every access– E.g.E.g. Java, Simula, Smalltalk Java, Simula, Smalltalk

• Other languages use the value modelOther languages use the value model– More efficientMore efficient– More difficult to control initializationMore difficult to control initialization

» E.g. uninitialized objects, mutual referencesE.g. uninitialized objects, mutual references

– E.g.E.g. C++, Ada 95 C++, Ada 95

Page 6: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

66

Constructors in C++Constructors in C++

InitializationInitialization((notnot assignment) assignment)

Copy ConstructorCopy Constructoroperator=operator=

Page 7: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

77

Execution OrderExecution Order

• How is an object of class B derived from class A How is an object of class B derived from class A initialized?initialized?

• In C++ and Java, the constructor of A is invoked In C++ and Java, the constructor of A is invoked before the constructor of Bbefore the constructor of B

– Why?Why?» So the B constructor never sees uninitialized attributesSo the B constructor never sees uninitialized attributes

– What are the arguments of the A constructor?What are the arguments of the A constructor?» In C++, they are explicitly definedIn C++, they are explicitly definedB::B (B_params) : A (A_args) { … }B::B (B_params) : A (A_args) { … }» Futhermore, constructors for object arguments can also be Futhermore, constructors for object arguments can also be

initializedinitializedlist_node() : prev(this), next(this), list_node() : prev(this), next(this), head_node(this), val(0) { }head_node(this), val(0) { }

Page 8: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

88

Object Lifetime: DestructorsObject Lifetime: Destructors

• DestructorsDestructors are methods used to are methods used to finalizefinalize the content the content of an objectof an object

– They do not deallocate spaceThey do not deallocate space– Language implementations that support garbage collection Language implementations that support garbage collection

greatly reduce the need for destructorsgreatly reduce the need for destructors» Most C++ compiler do not support GCMost C++ compiler do not support GC

Page 9: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

99

C++ ExampleC++ Example

• In general, C++ destructors are used for manual In general, C++ destructors are used for manual storage reclamationstorage reclamation

DestructorDestructor

Page 10: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

1010

Dangling ReferencesDangling References

• How are objects deallocated?How are objects deallocated?

• Storage classes:Storage classes:– static - never deallocated, same lifetime as the programstatic - never deallocated, same lifetime as the program– stack - deallocated automatically on subroutine returnstack - deallocated automatically on subroutine return– heap - explicit or implicit deallocationheap - explicit or implicit deallocation

• Explicit deallocation in Pascal:Explicit deallocation in Pascal:dispose (my_ptr);dispose (my_ptr);

• In C:In C:free (my_ptr);free (my_ptr);

• In C++:In C++:delete my_ptr;delete my_ptr; ////before deallocation, also calls the destructor for the before deallocation, also calls the destructor for the

objectobject• Implicit deallocationImplicit deallocation

– garbage collectiongarbage collection

Page 11: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

1111

Dangling ReferencesDangling References

• Dangling referenceDangling reference - a pointer that no longer points to a live - a pointer that no longer points to a live objectobject

• Produced in the context of heap allocation:Produced in the context of heap allocation:

p = new int ;p = new int ;r = p ;r = p ;delete r ;delete r ;*p = 3;*p = 3; // crash!!// crash!!

• Produced in the context of stack allocation (not in Pascal - has Produced in the context of stack allocation (not in Pascal - has only pointers to heap objects):only pointers to heap objects):

int* f ()int* f (){{

int x;int x;return &x;return &x;

}}

void main ()void main (){{

int *p;int *p;p = f();p = f();*p = 3;*p = 3; // // crash!!crash!!

}}

Page 12: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

1212

Dangling ReferencesDangling References

• Dangling references can only be produced in languages with Dangling references can only be produced in languages with explicit deallocation. Why?explicit deallocation. Why?

– Programmer may deallocate an object that is still referred by live pointersProgrammer may deallocate an object that is still referred by live pointers– Garbage collection will check if an object still has references to it before Garbage collection will check if an object still has references to it before

deleting itdeleting it

• Why do we need to detect dangling references?Why do we need to detect dangling references?– Need to warn the programmer - generate an error, not silently retrieve some Need to warn the programmer - generate an error, not silently retrieve some

garbagegarbage

• Mechanisms to detect dangling referencesMechanisms to detect dangling references– TombstonesTombstones– Locks and keysLocks and keys

Page 13: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

1313

TombstonesTombstones

• Extra level of indirectionExtra level of indirection– the pointer points to the tombstonethe pointer points to the tombstone– the tombstone points to the objectthe tombstone points to the object

• Deallocate an object - put a special value in the tombstoneDeallocate an object - put a special value in the tombstone

• For each object allocated For each object allocated dynamically - also allocate a dynamically - also allocate a tombstonetombstone

Page 14: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

1414

TombstonesTombstones

• PropertiesProperties– catch all dangling referencescatch all dangling references– handle both heap and stack objects handle both heap and stack objects – make storage compaction easier – why?make storage compaction easier – why?

− when moving blocks, need to change only addresses in tombstones, not in the pointerswhen moving blocks, need to change only addresses in tombstones, not in the pointers

• Time complexity - overhead:Time complexity - overhead:– creation of tombstones when allocating objectscreation of tombstones when allocating objects– checking validity on every accesschecking validity on every access– double indirectiondouble indirection

• Space complexity - need extra space for tombstonesSpace complexity - need extra space for tombstones– when are they deallocated?when are they deallocated?

– two approaches:two approaches:» never deallocate themnever deallocate them» add a reference count to each tombstone - deallocate when count is zeroadd a reference count to each tombstone - deallocate when count is zero

Page 15: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

1515

Locks and KeysLocks and Keys

• Allocation - generate a number, store Allocation - generate a number, store it in the object (it in the object (locklock) and in the pointer ) and in the pointer ((keykey))

• Access - check if the key matches the Access - check if the key matches the locklock

• Deallocation - put a special value in Deallocation - put a special value in the lockthe lock

Page 16: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

1616

Locks and KeysLocks and Keys

• PropertiesProperties– do not guarantee to catch all dangling references - why?do not guarantee to catch all dangling references - why?

» a reused block may get the same lock number - however, it is a low probabilitya reused block may get the same lock number - however, it is a low probability

– used to handle only heap objects – why?used to handle only heap objects – why?» to catch stack objects - would need to have locks on every local variable and to catch stack objects - would need to have locks on every local variable and

argumentargument

• Time complexity - overhead:Time complexity - overhead:– comparing locks and keys on every accesscomparing locks and keys on every access– however, no double indirectionhowever, no double indirection

• Space complexitySpace complexity– extra space for locks in every heap objectextra space for locks in every heap object– extra space for keys in every pointerextra space for keys in every pointer– however, no additional entities (tombstones) to be deallocatedhowever, no additional entities (tombstones) to be deallocated

Page 17: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

1717

Garbage CollectionGarbage Collection

– Advantages: implementation simplicity, speedAdvantages: implementation simplicity, speed– Disadvantages: burden on programmer, may produce garbage (memory Disadvantages: burden on programmer, may produce garbage (memory

leaks) or dangling referencesleaks) or dangling references

• Explicit deallocation of heap objectsExplicit deallocation of heap objects

• Automatic deallocation of heap objects (Automatic deallocation of heap objects (garbage collectiongarbage collection))– Advantages: convenience for programmer, safety (no memory leaks or Advantages: convenience for programmer, safety (no memory leaks or

dangling references)dangling references)– Disadvantages: complex implementation, run-time overheadDisadvantages: complex implementation, run-time overhead

• Garbage collectionGarbage collection– essential for functional languages - frequent construction and return of essential for functional languages - frequent construction and return of

objects from functionsobjects from functions– increasingly used in imperative languages (Clu, Ada, Java)increasingly used in imperative languages (Clu, Ada, Java)– mechanisms:mechanisms:

» reference countsreference counts» mark-and-sweepmark-and-sweep

Page 18: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

1818

Reference CountsReference Counts

• How do we know when a heap object is no longer useful?How do we know when a heap object is no longer useful?– when there are no pointers to itwhen there are no pointers to it

• SolutionSolution– in each object keep a in each object keep a reference counterreference counter = the number of pointers referring = the number of pointers referring

the objectthe object– when allocating the object:when allocating the object:

p = new int;p = new int; // // refcntrefcnt of new object of new object ←← 1 1

– when assigning pointers:when assigning pointers:p = r;p = r; // // refcntrefcnt of object referred by p -- of object referred by p --

// // refcntrefcnt of object referred by r ++ of object referred by r ++

– when the reference count is zero when the reference count is zero →→ can destroy it can destroy it

Page 19: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

1919

Reference CountsReference Counts

• Problems:Problems:– Objects that contain pointers:Objects that contain pointers:

p = new chr_tree;p = new chr_tree;......p = r; //p = r; // if the object referred by p can be deleted (if the object referred by p can be deleted (refcntrefcnt = 0), need to = 0), need to

recursively follow pointers within it to decrement recursively follow pointers within it to decrement refcntrefcnt for for those objects, and delete them as well if their those objects, and delete them as well if their refcntrefcnt is zero is zero

– Pointers declared as local variables:Pointers declared as local variables:void f ( int * x )void f ( int * x ){{

int i;int i;int * p = x;int * p = x; // // refcntrefcnt of object referred by x ++ of object referred by x ++return;return; ////

}}all local variables will die here - must find all those which are all local variables will die here - must find all those which are pointers (like p) and decrement pointers (like p) and decrement refcntrefcntss

– Solution - use Solution - use type descriptorstype descriptors» at the beginning of each record - specify which fields are pointersat the beginning of each record - specify which fields are pointers» in each stack frame - specify which local variables are pointersin each stack frame - specify which local variables are pointers

Page 20: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

2020

Reference CountsReference Counts

• Problem - circular lists:Problem - circular lists:

• The objects in the list are not reachable, but their reference counts The objects in the list are not reachable, but their reference counts are non-zeroare non-zero

Page 21: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

2121

Mark-and-Sweep CollectionMark-and-Sweep Collection

• When the free space becomes low:When the free space becomes low:1.1. walk through the heap, and mark each block (tentatively) as "useless"walk through the heap, and mark each block (tentatively) as "useless"2.2. recursively explore all pointers in the program, and mark each recursively explore all pointers in the program, and mark each

encountered block as "useful"encountered block as "useful"3.3. walk again through the heap, and delete every "useless" block (could not walk again through the heap, and delete every "useless" block (could not

be reached)be reached)

• To find all pointers - To find all pointers - use type descriptorsuse type descriptors

• To explore recursively - To explore recursively - need a stack (to be able to return)need a stack (to be able to return)– maximum length of stack = the longest chain of pointersmaximum length of stack = the longest chain of pointers– problem - maybe not enough space for a stack (the free space is already problem - maybe not enough space for a stack (the free space is already

low)low)– solution - exploration via pointer reversalsolution - exploration via pointer reversal

Page 22: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

2222

Mark-and-Sweep CollectionMark-and-Sweep Collection

• Exploration via pointer reversalExploration via pointer reversal::

• Before moving from Before moving from currentcurrent to to nextnext block, reverse the pointer block, reverse the pointer that is followed, to refer back to that is followed, to refer back to previousprevious block block

• When returning, restore the When returning, restore the pointerpointer

• During exploration, the currently During exploration, the currently explored path will have all explored path will have all pointers reversed, to trace the pointers reversed, to trace the way backway back

Page 23: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

2323

Heap-based AllocationHeap-based Allocation

• The The heapheap is a region of storage in which subblock is a region of storage in which subblock can be allocated and deallocatedcan be allocated and deallocated

– This This notnot the the heap data structureheap data structure

Page 24: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

2424

Storage CompactionStorage Compaction

• Storage compaction - reduce external fragmentationStorage compaction - reduce external fragmentation– elegant solution – stop-and-copyelegant solution – stop-and-copy

• Stop-and-copyStop-and-copy– achieve compaction and garbage collection and simultaneously eliminate achieve compaction and garbage collection and simultaneously eliminate

steps 1 and 3 from steps 1 and 3 from mark-and-sweepmark-and-sweep– divide the heap in two halvesdivide the heap in two halves– all allocations happen in first halfall allocations happen in first half– when memory is lowwhen memory is low

» recursively explore all pointers in the program, move each encountered block recursively explore all pointers in the program, move each encountered block to the second half, and change the pointer to it accordinglyto the second half, and change the pointer to it accordingly

» swap the notion of first and second halfswap the notion of first and second half

Page 25: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

2525

Garbage CollectionGarbage Collection

• Stop-and-copyStop-and-copy – advantage over standard – advantage over standard mark-and-sweepmark-and-sweep

• Significant difference between Significant difference between reference countsreference counts strategies and strategies and mark-and-sweepmark-and-sweep strategies strategies

– no more walks ("sweeps") through the entire heapno more walks ("sweeps") through the entire heap– overhead proportional to the number of used blocks, instead of the total overhead proportional to the number of used blocks, instead of the total

number of blocksnumber of blocks

– reference countsreference counts - when an object is no longer needed, it is immediately - when an object is no longer needed, it is immediately reclaimedreclaimed

– mark-and-sweepmark-and-sweep - "stop-the-world" effect: pause program execution to - "stop-the-world" effect: pause program execution to reclaim all the garbagereclaim all the garbage

Page 26: 1 Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.

2626

DestructorsDestructors

nono

• With garbage collection, do we still need destructors?With garbage collection, do we still need destructors?– if the only finalization to do is deallocation –if the only finalization to do is deallocation –

– if other operations are needed (close a file, print a message) –if other operations are needed (close a file, print a message) – yesyes

– problem –problem – no control over when the garbage collector deallocates an no control over when the garbage collector deallocates an objectobject