comp 250
play

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}


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

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

  3. Recall last lecture Dog myDog = new Beagle(); class Dog myDog.bark(); String serialNumber Person owner void bark() {print “woof”} : extends class Beagle void hunt () void bark() {print “ aowwwuuu ”} 3

  4. This figure shows objects in a running Java program. Object class descriptor Dog myDog = new Beagle(); myDog.bark(); getSuperClass() Dog bark() myDog Beagle class descriptor object getSuperClass() Beagle getClass() bark() class descriptor 4

  5. Suppose we are running a class TestDog, Object which has a main() method. class descriptor Dog class descriptor Beagle class descriptor Test Dog main() class descriptor

  6. Suppose we are running a class TestDog, Object which has a main() method. class descriptor Dog class descriptor Beagle There are no class descriptor TestDog.main() objects at the start of D execution. Test Dog main() Call Stack Objects class descriptor 6

  7. public static void main(){ Object Dog myDog = new Beagle(); class descriptor myDog.bark() : } Dog class descriptor Beagle class descriptor TestDog.main() Dog myDog null Test Dog Call Stack Objects class descriptor 7

  8. public static void main(){ Object Dog myDog = new Beagle(); class descriptor myDog.bark() : } Dog class descriptor Beagle() Beagle class descriptor TestDog.main() Dog myDog null Test Dog Call Stack Objects class descriptor (Beagle constructor called) 8

  9. public static void main(){ Object Dog myDog = new Beagle(); class descriptor myDog.bark() : } Dog class descriptor Beagle class descriptor Beagle TestDog.main() object Dog myDog Test Dog Call Stack Objects class descriptor 9 (after constructor is done)

  10. public static void main(){ Object Dog myDog = new Beagle(); class descriptor myDog.bark() : Dog } bark() class descriptor bark() Beagle this bark() Beagle class descriptor TestDog.main() object Dog myDog Test Dog JVM looks for the bark() method in class descriptor this.getClass() and finds it. 10

  11. public static void main(){ Object Dog myDog = new Beagle(); class descriptor myDog.bark(); myDog.getOwner(); Dog } getOwner() class descriptor getOwner() Beagle this Beagle class descriptor TestDog.main() object Dog myDog Test Dog JVM looks for the getOwner() method in class descriptor other this.getClass () and doesn’t find it. objects 11

  12. public static void main(){ Object Dog myDog = new Beagle(); class descriptor myDog.bark(); myDog.getOwner(); Dog } getOwner() JVM then looks for the getOwner() method in class descriptor this.getClass().getSuperclass() and finds it. getOwner() Beagle this Beagle class descriptor TestDog.main() object Dog myDog Test Dog JVM looks for the getOwner() method in class descriptor other this.getClass () and doesn’t find it. objects 12

  13. Class Call Stack Objects Descriptors Methods are here Local variables and method parameters Object Object instance are here fields are here class descriptor mB() Dog Beagle object mA() class descriptor Beagle TestDog.main() class descriptor other objects Test Dog class descriptor 13

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

  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

  16. Dog myDog = new Beagle (“Bob”); Terrier myDog = new Terrier(“Tim”); class descriptor Beagle class descriptor Test Beagle object class descriptor Test.main() “Bob” Dog myDog Call Stack other class descriptors 16

  17. Dog myDog = new Beagle (“Bob”); Terrier myDog = new Terrier(“Tim”); class descriptor Beagle Terrier object class descriptor “Tim” Test Beagle object class descriptor Test.main() “Bob” Dog myDog Bob is garbage. Call Stack other class descriptors 17

  18. As the program continues, more “garbage” accumulates. Terrier other objects class descriptor mC() Beagle Terrier mB() object class descriptor “Tim” mA() Test Beagle object class descriptor Test.main() “Bob” Dog myDog other class other Call Stack objects descriptors 18

  19. Objects Terrier other class descriptor objects mC() Let’s ignore Beagle Terrier mB() the call class descriptor object “Tim” descriptors mA() Test for rest of Beagle today. class descriptor object Test.main() “Bob” Dog myDog other class other Call Stack descriptors objects 19

  20. Every object has a location in memory: Object.hashCode(). Terrier object “Tim” mC() Beagle object “Bob” mB() object object mA() object main() another garbage object object Call Stack Objects 20

  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” mC() Beagle object “Bob” mB() object object mA() object main() another garbage object object Call Stack Objects 21

  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

  23. “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” mC() Beagle object “Bob” object mB() object object mA() object another garbage object main() object Call Stack Objects 23

  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

  25. Garbage collection: “Mark and Sweep” 1) Build a graph, and identify live objects (“Mark”) 2) Remove garbage (“sweep”) 25

  26. Garbage collector builds a graph that corresponds to the one here: Terrier object “Tim” Vertices mC() correspond to Beagle object “Bob” reference object mB() variables in object call stack, and object to objects. mA() object Edges correspond to main() another garbage object references. object Call Stack Objects 26

  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

  28. Phase 1: “Mark” the garbage Terrier object “Tim” mC() Beagle object “Bob” object mB() object object mA() object main() another garbage object object Call Stack Objects 28

  29. Phase 2: “Sweep” the garbage Terrier object “Tim” mC() remove node from list object mB() object object mA() object remove node main() From list object Objects Call Stack 29

  30. Use another list to keep track of free space between objects. Terrier object “Tim” mC() object mB() object object mA() object main() object Call Stack 30

  31. Terrier object “Tim” mC() new object object mB() object object mA() object main() object Call Stack 31

  32. new node in object list Terrier object “Tim” mC() new object object mB() object object mA() object main() object Call Stack 32

  33. Two lists: free space, live objects Terrier object “Tim” mC() new object object mB() object object mA() object main() object Call Stack 33

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend