lecture 34 polymorphism (continued.) garbage...

34
1 COMP 250 Lecture 34 Polymorphism (continued.) Garbage Collection (mark and sweep) Nov. 27, 2017

Upload: others

Post on 19-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

1

COMP 250

Lecture 34

Polymorphism (continued.)

Garbage Collection (mark and sweep)

Nov. 27, 2017

Page 2: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Recall last lecture

2

class DogString serialNumberPerson owner

void bark() {print “woof”}

:

class Beaglevoid hunt ()void bark()

{print “aowwwuuu”}

extends

Page 3: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Dog myDog = new Beagle(); myDog.bark();

Recall last lecture

3

class DogString serialNumberPerson owner

void bark() {print “woof”}

:

class Beaglevoid hunt ()void bark()

{print “aowwwuuu”}

extends

Page 4: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle object

Beagle bark()

class descriptor

Dogbark()

class descriptor

getSuperClass()

getClass()

Object

class descriptor

getSuperClass()

myDog

4

Dog myDog = new Beagle(); myDog.bark();

This figure shows objects in a running Java program.

Page 5: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle

class descriptor

Dog

class descriptor

Object

class descriptor

TestDogmain()

class descriptor

Suppose we are running a class TestDog, which has a main() method.

Page 6: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle

class descriptor

Dog

class descriptor

Object

class descriptor

Call Stack Objects

TestDogmain()

class descriptor

Suppose we are running a class TestDog, which has a main() method.

TestDog.main()

D

6

There are no objects at the start of execution.

Page 7: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle

class descriptor

Dog

class descriptor

Object

class descriptor

TestDog.main()

Dog myDog

public static void main(){Dog myDog = new Beagle();

myDog.bark():

}

TestDog

class descriptor

null

7

Call Stack Objects

Page 8: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle

class descriptor

Dog

class descriptor

Object

class descriptor

TestDog.main()

Dog myDog

Beagle()

(Beagle constructor called)

public static void main(){Dog myDog = new Beagle();

myDog.bark():

}

TestDog

class descriptor

null

8

Call Stack Objects

Page 9: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle object

Beagleclass descriptor

Dog

class descriptor

Object

class descriptor

TestDog.main()

Dog myDog

TestDog

class descriptor

(after constructor is done)

public static void main(){Dog myDog = new Beagle();

myDog.bark():

}

9

Call Stack Objects

Page 10: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle object

Beagle

bark()

class descriptor

Dog

bark()

class descriptor

Object

class descriptor

TestDog.main()

Dog myDog

bark()this

TestDog

class descriptor

public static void main(){Dog myDog = new Beagle();

myDog.bark():

}

JVM looks for the bark() method in this.getClass() and finds it.

10

Page 11: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle object

Beagle

class descriptor

Dog

getOwner()

class descriptor

Object

class descriptor

other objects

TestDog.main()

Dog myDog

getOwner()this

TestDog

class descriptor

public static void main(){Dog myDog = new Beagle();

myDog.bark();myDog.getOwner();

}

JVM looks for the getOwner() method in this.getClass() and doesn’t find it.

11

Page 12: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle object

Beagle

class descriptor

DoggetOwner()

class descriptor

Object

class descriptor

other objects

TestDog.main()

Dog myDog

getOwner()this

JVM then looks for the getOwner() method in this.getClass().getSuperclass() and finds it.

TestDog

class descriptor

public static void main(){Dog myDog = new Beagle();

myDog.bark();myDog.getOwner();

}

JVM looks for the getOwner() method in this.getClass() and doesn’t find it.

12

Page 13: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle object

Beagle

class descriptor

Dog

class descriptor

Object

class descriptor

other objects

TestDog.main()

mA()

Call StackClass

DescriptorsObjects

Local variables and method parameters are here Object instance

fields are here

Methods are here

TestDog

class descriptor13

mB()

Page 14: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

14

COMP 250

Lecture 34

Polymorphism (continued.)

Garbage Collection (mark and sweep)

Nov. 27, 2017

Page 15: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Garbage Collection

Dog myDog = new Beagle(“Bob”);

myDog = new Terrier(“Tim”);

Nothing references the Bob the Beagle.

Bob is wasting memory. Bob has become garbage.

15

Page 16: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle object“Bob”

Beagle

class descriptor

Test.main()

Dog myDog

Call Stack

Dog myDog = new Beagle(“Bob”);

myDog = new Terrier(“Tim”);

Test

class descriptor

other class descriptors

16

Terrier

class descriptor

Page 17: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle object“Bob”

Beagle

class descriptor

Test.main()

Dog myDog

Call Stack

Dog myDog = new Beagle(“Bob”);

myDog = new Terrier(“Tim”);

Test

class descriptor

other class descriptors

Terrier object“Tim”

Terrier

class descriptor

Bob is garbage.

17

Page 18: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Beagle

class descriptor

mA()

Call Stack

Test

class descriptor

other class descriptors

Terrier object“Tim”

Test.main()

Dog myDog

mB()

mC()

other objects

other objects

Beagle object“Bob”

Terrier

class descriptor

As the program continues, more “garbage” accumulates.

18

Page 19: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

mA()

Call Stack

Terrier object“Tim”

Test.main()

Dog myDog

mB()

mC()

other objects

other objects

Beagle object“Bob”

19

Beagle

class descriptor

Test

class descriptor

other class descriptors

Terrier

class descriptor

Let’s ignore the call descriptors for rest of today.

Objects

Page 20: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Every object has a location in memory: Object.hashCode().

Terrier object “Tim”

another garbage object

object

Beagle object “Bob”

object

object

object

20

mA()

Call Stack

main()

mB()

mC()

Objects

Page 21: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

The Java Virtual Machine (JVM) maintains a linked list of all objects. i.e. The list stores the Object.hashCode() of each object.

Terrier object “Tim”

another garbage object

object

Beagle object “Bob”

object

object

object

mA()

Call Stack

main()

mB()

mC()

21Objects

Page 22: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Q: What to do when object space fills up?

A: Let the program crash.

A: Reuse the space we don’t need.(Garbage collection)

22

Page 23: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

“Live objects” (not garbage) are those referenced either from a call stack variable or from an instance variable in a live object.

Terrier object “Tim”

another garbage object

object

Beagle object “Bob”

object

object

object

mA()

Call Stack

main()

mB()

mC()

23Objects

object

Page 24: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Object A

Object B

Q: If these objects are only referencedby each other, then are they garbage ?

A: Yes, because they will never be used by the program.24

Page 25: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Garbage collection: “Mark and Sweep”

1) Build a graph, and identify live objects (“Mark”)

2) Remove garbage (“sweep”)

25

Page 26: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Terrier object “Tim”

another garbage object

object

Beagle object “Bob”

object

object

object

mA()

Call Stack

main()

mB()

mC()

Garbage collector builds a graph that corresponds to the one here:

26Objects

Vertices correspond to reference variables in call stack, and to objects.

Edges correspond to references.

object

Page 27: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

For each vertex that corresponds to a reference variable on the call stack:

traverse the graph.

Visiting a node means mark it as live.

27

Page 28: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Terrier object “Tim”

another garbage object

object

Beagle object “Bob”

object

object

mA()

Call Stack Objects

main()

mB()

mC()

Phase 1: “Mark” the garbage

28

object

object

Page 29: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Terrier object “Tim”

object

object

object

mA()

main()

mB()

mC()

Phase 2: “Sweep” the garbage

29

remove node from list

remove nodeFrom list

Call Stack Objects

object

object

Page 30: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

object

object

object

mA()

Call Stack

main()

mB()

mC()

Use another list to keep track of free space between objects.

Terrier object “Tim”

30

object

object

Page 31: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

object

object

mA()

Call Stack

main()

mB()

mC()

31

Terrier object “Tim”

new object

object

object

object

Page 32: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

Terrier object “Tim”

object

object

object

mA()

Call Stack

main()

mB()

mC()

32

new object

new node in object list

object

object

Page 33: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

object

object

object

mA()

Call Stack

main()

mB()

mC()

33

Terrier object “Tim”

new object

Two lists: free space, live objects

object

object

Page 34: Lecture 34 Polymorphism (continued.) Garbage Collectionlanger/250/34-garbagecollection-slides.pdf · After garbage collection, continue execution.. •New objects can be added, where

After garbage collection, continue execution..

• New objects can be added, where there is a big enough gap in free space.

• Garbage collection is needed again when there is no gap big enough for the new object.

• Program needs to stop (temporarily) to do garbage collection. This is not good for real time timeapplications.

34