COMP 250 Lecture 34 Polymorphism (continued.) Garbage Collection - - PowerPoint PPT Presentation

comp 250
SMART_READER_LITE
LIVE PREVIEW

COMP 250 Lecture 34 Polymorphism (continued.) Garbage Collection - - PowerPoint PPT Presentation

COMP 250 Lecture 34 Polymorphism (continued.) Garbage Collection (mark and sweep) Nov. 27, 2017 1 Recall last lecture class Dog String serialNumber Person owner void bark() {print woof}


slide-1
SLIDE 1

1

COMP 250

Lecture 34 Polymorphism (continued.) Garbage Collection

(mark and sweep)

  • Nov. 27, 2017
slide-2
SLIDE 2

Recall last lecture

2

class Dog String serialNumber Person owner void bark() {print “woof”} : class Beagle void hunt () void bark() {print “aowwwuuu”} extends

slide-3
SLIDE 3

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

Recall last lecture

3

class Dog String serialNumber Person owner void bark() {print “woof”} : class Beagle void hunt () void bark() {print “aowwwuuu”} extends

slide-4
SLIDE 4

Beagle

  • bject

Beagle bark() class descriptor Dog bark() 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.

slide-5
SLIDE 5

Beagle class descriptor Dog class descriptor Object class descriptor TestDog main() class descriptor

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

slide-6
SLIDE 6

Beagle class descriptor Dog class descriptor Object class descriptor

Call Stack Objects

TestDog main() class descriptor

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

TestDog.main()

D

6

There are no

  • bjects at the

start of execution.

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

Beagle

  • bject

Beagle class 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

slide-10
SLIDE 10

Beagle

  • bject

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

slide-11
SLIDE 11

Beagle

  • bject

Beagle class descriptor Dog getOwner() class descriptor Object class descriptor

  • ther
  • bjects

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

slide-12
SLIDE 12

Beagle

  • bject

Beagle class descriptor Dog getOwner() class descriptor Object class descriptor

  • ther
  • bjects

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

slide-13
SLIDE 13

Beagle

  • bject

Beagle class descriptor Dog class descriptor Object class descriptor

  • ther
  • bjects

TestDog.main() mA()

Call Stack Class Descriptors Objects

Local variables and method parameters are here Object instance fields are here Methods are here TestDog class descriptor

13

mB()

slide-14
SLIDE 14

14

COMP 250

Lecture 34 Polymorphism (continued.) Garbage Collection

(mark and sweep)

  • Nov. 27, 2017
slide-15
SLIDE 15

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

slide-16
SLIDE 16

Beagle

  • bject

“Bob”

Beagle class descriptor

Test.main()

Dog myDog

Call Stack

Dog myDog = new Beagle(“Bob”); myDog = new Terrier(“Tim”);

Test class descriptor

  • ther class

descriptors

16

Terrier class descriptor

slide-17
SLIDE 17

Beagle

  • bject

“Bob”

Beagle class descriptor

Test.main()

Dog myDog

Call Stack

Dog myDog = new Beagle(“Bob”); myDog = new Terrier(“Tim”);

Test class descriptor

  • ther class

descriptors

Terrier

  • bject

“Tim”

Terrier class descriptor

Bob is garbage.

17

slide-18
SLIDE 18

Beagle class descriptor mA()

Call Stack

Test class descriptor

  • ther class

descriptors

Terrier

  • bject

“Tim” Test.main()

Dog myDog mB() mC()

  • ther
  • bjects
  • ther
  • bjects

Beagle

  • bject

“Bob”

Terrier class descriptor

As the program continues, more “garbage” accumulates.

18

slide-19
SLIDE 19

mA()

Call Stack

Terrier

  • bject

“Tim” Test.main()

Dog myDog mB() mC()

  • ther
  • bjects
  • ther
  • bjects

Beagle

  • bject

“Bob”

19

Beagle class descriptor Test class descriptor

  • ther class

descriptors Terrier class descriptor

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

slide-20
SLIDE 20

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

Terrier object “Tim”

another garbage object

  • bject

Beagle object “Bob”

  • bject
  • bject
  • bject

20

mA()

Call Stack

main()

mB() mC()

Objects

slide-21
SLIDE 21

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

  • bject

Beagle object “Bob”

  • bject
  • bject
  • bject

mA()

Call Stack

main()

mB() mC()

21

Objects

slide-22
SLIDE 22

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

slide-23
SLIDE 23

“Live objects” (not garbage) are those referenced either from a call stack variable

  • r from an instance variable in a live object.

Terrier object “Tim”

another garbage object

  • bject

Beagle object “Bob”

  • bject
  • bject
  • bject

mA()

Call Stack

main()

mB() mC()

23

Objects

  • bject
slide-24
SLIDE 24

Object A Object B

Q: If these objects are only referenced by each other, then are they garbage ? A: Yes, because they will never be used by the program.

24

slide-25
SLIDE 25

Garbage collection: “Mark and Sweep”

1) Build a graph, and identify live objects (“Mark”) 2) Remove garbage (“sweep”)

25

slide-26
SLIDE 26

Terrier object “Tim”

another garbage object

  • bject

Beagle object “Bob”

  • bject
  • bject
  • bject

mA()

Call Stack

main()

mB() mC()

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

26

Objects

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

  • bject
slide-27
SLIDE 27

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

slide-28
SLIDE 28

Terrier object “Tim”

another garbage object

  • bject

Beagle object “Bob”

  • bject
  • bject

mA()

Call Stack Objects

main()

mB() mC()

Phase 1: “Mark” the garbage

28

  • bject
  • bject
slide-29
SLIDE 29

Terrier object “Tim”

  • bject
  • bject
  • bject

mA()

main()

mB() mC()

Phase 2: “Sweep” the garbage

29

remove node from list remove node From list

Call Stack Objects

  • bject
  • bject
slide-30
SLIDE 30
  • bject
  • bject
  • bject

mA()

Call Stack

main()

mB() mC()

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

Terrier object “Tim”

30

  • bject
  • bject
slide-31
SLIDE 31
  • bject
  • bject

mA()

Call Stack

main()

mB() mC()

31

Terrier object “Tim” new object

  • bject
  • bject
  • bject
slide-32
SLIDE 32

Terrier object “Tim”

  • bject
  • bject
  • bject

mA()

Call Stack

main()

mB() mC()

32

new object new node in

  • bject list
  • bject
  • bject
slide-33
SLIDE 33
  • bject
  • bject
  • bject

mA()

Call Stack

main()

mB() mC()

33

Terrier object “Tim” new object

Two lists: free space, live objects

  • bject
  • bject
slide-34
SLIDE 34

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 time

applications.

34