10 objects variables memory
play

10 Objects, Variables, Memory Michele Van Dyne MUS 204B - PowerPoint PPT Presentation

10 Objects, Variables, Memory Michele Van Dyne MUS 204B mvandyne@mtech.edu https://katie.mtech.edu/classes/csci136 Where Java stores stuff The Heap The Stack Breaking both Java Garbage Collector (GC) Creating objects


  1. 10 – Objects, Variables, Memory Michele Van Dyne MUS 204B mvandyne@mtech.edu https://katie.mtech.edu/classes/csci136

  2.  Where Java stores stuff ◦ The Heap ◦ The Stack ◦ Breaking both ◦ Java Garbage Collector (GC)  Creating objects ◦ Copy constructors ◦ Methods creating objects  Static keyword ◦ Static methods and instance variables

  3.  Stack ◦ Where method invocations live ◦ Where local variables live ◦ A chunk of memory  Heap ◦ Where all objects live ◦ Another chunk of memory 3

  4.  Methods are “stacked” ◦ Y ou call a method → put on top of a call stack ◦ A method stays on stack until it finishes executing ◦ Each stack frame contains:  What line it is executing  Values of all the method’s local variables  This includes parameters to the method 4

  5. public void doStuff() { boolean b = true ; go(4); } public void go( int x) { int z = x + 24; crazy(); } public void crazy() { char c = 'a'; } 5

  6. public void doStuff() { boolean b = true ; go(4); } 1 2 public void go( int b) { go() doStuff() int c = b + 24; int b → 4 boolean b → true crazy(); int c → 28 } doStuff() 3 public void crazy() boolean b → true { char c = 'a'; crazy() } 4 char c → 'a' go() go() int b → 4 int b → 4 int c → 28 int c → 28 doStuff() doStuff() boolean b → true boolean b → true 6

  7. public class Factorial { public static long fact( long n) { if (n <= 1) return 1; fact(1) return n * fact (n - 1); n → 1 } public static void main(String [] args) fact(2) { n → 2 long result = fact (4); System. out .println("4! = " + result); fact(3) } n → 3 } 0! = 1 1! = 1 fact(4) n → 4 2! = 1 x 2 = 2 3! = 1 x 2 x 3 = 6 4! = 4 x 3 x 2 x 1 = 24 main() Some example factorials. 7

  8. public class Factorial { public static long fact( long n) { if (n <= 1) return 1; fact(1) return n * fact (n - 1); n → 1 } return 1 public static void main(String [] args) fact(2) { n → 2 long result = fact (4); return 2 * fact(1) System. out .println("4! = " + result); } fact(3) } n → 3 0! = 1 return 3 * fact(2) 1! = 1 fact(4) 2! = 1 x 2 = 2 n → 4 3! = 1 x 2 x 3 = 6 return 4 * fact(3) 4! = 4 x 3 x 2 x 1 = 24 main() Some example factorials. fact(4) = 24 8

  9. fact(-2) n → -2 fact(-1) public class Factorial n → -1 { Exception in thread "main" public static long fact( long n) java.lang.StackOverflowError { fact(0) at Factorial.fact(Factorial.java:6) n → 0 at Factorial.fact(Factorial.java:6) at Factorial.fact(Factorial.java:6) return n * fact (n - 1); at Factorial.fact(Factorial.java:6) fact(1) } at Factorial.fact(Factorial.java:6) n → 1 at Factorial.fact(Factorial.java:6) public static void main(String [] args) at Factorial.fact(Factorial.java:6) { at Factorial.fact(Factorial.java:6) fact(2) long result = fact (4); at Factorial.fact(Factorial.java:6) n → 2 System. out .println("4! = " + result); at Factorial.fact(Factorial.java:6) } at Factorial.fact(Factorial.java:6) } fact(3) 0! = 1 at Factorial.fact(Factorial.java:6) at Factorial.fact(Factorial.java:6) n → 3 1! = 1 at Factorial.fact(Factorial.java:6) 2! = 1 x 2 = 2 at Factorial.fact(Factorial.java:6) fact(4) 3! = 1 x 2 x 3 = 6 at Factorial.fact(Factorial.java:6) n → 4 at Factorial.fact(Factorial.java:6) 4! = 4 x 3 x 2 x 1 = 24 at Factorial.fact(Factorial.java:6) Some example factorials. at Factorial.fact(Factorial.java:6) main() ... 9

  10.  Local variables live on the stack ck ◦ Local variables in a method ◦ Parameters passed to a method ◦ Reference variables  e.g. passing in a int [] array, a Hero object, …  Only the remote control lives on the stack  Objects live on the heap ap ◦ Objects instantiated with new , live on the heap ◦ Instance variables of an object  Stored as part of the object living on the heap 10

  11.  Creating an object in 3 easy steps: 1) Declare a reference variable Heap myDuck Duck myDuck 11

  12.  Creating an object in 3 easy steps: 1) Declare a reference variable 2) Create an object Heap myDuck new Duck(); 12

  13.  Creating an object in 3 easy steps: 1) Declare a reference variable 2) Create an object 3) Link the object and the reference You have now consumed Heap memory on the heap based on how much state a Duck requires (i.e. the number and type of its instance variables). myDuck Duck myDuck = new Duck(); 13

  14.  Java tracks # of variables referring to an object ◦ As soon as nobody refers to an object, its memory can be freed ◦ Java's automatic Garbage Collector (GC) periodically handles this for you  Exactly when is up to the JVM You're so going to have to this  Enjoy it (while it lasts) yourself in data structures. 14

  15.  Object-killer #1 ◦ Reference goes out of scope permanently public class DuckKiller1 { main() public static void makeDuck() { Heap Duck d = new Duck(); } d makeDuck() public static void main(String [] args) { main() makeDuck (); while ( true ) { Heap // Do something } } main() } 15

  16.  Object-killer #1b ◦ Variable inside curly braces goes out of scope 00 public class DuckKiller1b main() 01 { 02 public static void main(String [] args) Heap 03 { line 04 04 if (args.length >= 0) line 05 d 05 { line 06 06 Duck d = new Duck(); 07 } 08 while ( true ) 09 { 10 // Do something 11 } 12 } 13 } 16

  17.  Object-killer #1b ◦ Reference goes out of scope permanently 00 public class DuckKiller1b main() 01 { 02 public static void main(String [] args) Heap 03 { line 04 04 if (args.length >= 0) line 05 05 { line 06 06 Duck d = new Duck(); 07 } line 07 08 while ( true ) line 08 09 { 10 // Do something 11 } 12 } 13 } 17

  18.  Object-killer #2 ◦ Assign the reference to another object Heap 00 public class DuckKiller2 01 { 02 public static void main(String [] args) main() 03 { 04 Duck d = new Duck(); 05 System. out .println("quack!"); line 04 06 d = new Duck(); 07 System. out .println("quack!"); d 08 d = new Duck(); 09 System. out .println("quack!"); 10 } 11 } 18

  19.  Object-killer #2 ◦ Assign the reference to another object Heap 00 public class DuckKiller2 01 { 02 public static void main(String [] args) main() 03 { 04 Duck d = new Duck(); 05 System. out .println("quack!"); line 04 06 d = new Duck(); line 05 07 System. out .println("quack!"); line 06 d 08 d = new Duck(); 09 System. out .println("quack!"); 10 } 11 } After executing line 06, no one is referring to the first Duck object anymore. It is now subject to garbage collection. 19

  20.  Object-killer #2 ◦ Assign the reference to another object Heap 00 public class DuckKiller2 01 { 02 public static void main(String [] args) main() 03 { 04 Duck d = new Duck(); 05 System. out .println("quack!"); line 04 06 d = new Duck(); line 05 07 System. out .println("quack!"); line 06 d 08 d = new Duck(); 09 System. out .println("quack!"); line 07 10 } line 08 11 } After executing line 08, both the first and second Ducks objects can be garbage collected. 20

  21.  Object-killer #3 ◦ Set the reference variable to null Heap 00 public class DuckKiller3 01 { d 02 public static void main(String [] args) main() 03 { 04 Duck d = new Duck(); 05 System. out .println("quack!"); line 04 06 d = null; 07 System. out .println("quack!"); 08 } 09 } 21

  22.  Object-killer #3 ◦ Set the reference variable to null Heap 00 public class DuckKiller3 01 { d 02 public static void main(String [] args) main() 03 { 04 Duck d = new Duck(); 05 System. out .println("quack!"); line 04 06 d = null; line 05 07 System. out .println("quack!"); line 06 08 } 09 } After executing line 06, no one is referring to the Duck object anymore. The Java garbage collector can now free up the Duck's memory. 22

  23.  Memory is not infinite ◦ Can't keep new’ing forever w/o getting rid of any ◦ You can buy yourself some time:  Increase memory with JVM flag: -Xmx<num>[k|m|g] import java.util.*; public class HeapDeath { While variable d public static void main(String [] args) scopes out every { loop, the Duck ArrayList<Duck> list = new ArrayList<Duck>(); reference persists while ( true ) inside the ArrayList. { Duck d = new Duck(); Memory is never list.add(d); freed! } } } 23

  24. Type Memory ry size (bits, 8 bi bits = byte) 8 byte 16 short 32 int 64 long 32 float 64 double http://righteousit.wordpress.com/tag /tcpip/ not precisely defined boolean 16 char NOTE: The size of a reference variable’s remote control is JVM dependent. But there is always overhead for the reference variable and for any instance variables inside the data type! 24

Recommend


More recommend