1 / 26

Objects and memory

Objects and memory. The stack The heap Variable life and death null Clones Serializing objects. Memory: A programmer’s prospective. Recall: Memory can be viewed as a sequence of numbered ‘storage boxes’ Essentially memory is an array

tameka
Download Presentation

Objects and memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects and memory The stack The heap Variable life and death null Clones Serializing objects

  2. Memory: A programmer’s prospective • Recall: Memory can be viewed as a sequence of numbered ‘storage boxes’ • Essentially memory is an array • The compiler decides (on our behalf) where variables will live in memory (their address) x0000 x0001 0 x0002 ‘a’ x0003 140 x0004 000000000 x0005 public static void main (String[] args) { int x = 0; char c = ‘a’; double dx = 140000000000; … } // end method main x0006 x0007 x0008 . . . xFFFD xFFFE Symbol Table xFFFF

  3. Memory: A programmer’s prospective • Areas of memory are allocated for the different types of things that need to be stored • Operating System routines • The executable code • Static/global variables • Dynamically created objects • Instance variables • Memory needs grow/shrink during execution of the program. • The compiler has no idea how much space you might need (may depend on input, etc) • Local variables (a.k.a. stack variables) • Memory requirements change as variables local to the method come into and out of scope • The amount of available memory is system dependent OS code globals heap stack

  4. Types of variables public class Main { public static int number; public static void main (String[] args) { System.out.println(number); } end method main } // end class main OS code Static/global variable: number public class Point { int x; int y; } // end class Point globals heap Instance variables: x, y public int foo (int bar){ int temp = bar + 1; return temp; } // end class Point stack Local/stack variables: bar, temp

  5. Memory in action public class Point { int x; int y; Point (int xValue, int yValue) { x = xValue; y = yValue; } // end constructor Point makePoint (int x, int y) { Point p1 = new Point(x,y); return p1; } // end method Point static void main (String[] args) { Point p1 = new Point(1,2); Point p2 = makePoint(3,4); } end method main } // end class Point … globals heap stack main(): p2 main(): p1 main(): args Before first line of main executes

  6. Memory in action public class Point { int x; int y; Point (int xValue, int yValue) { x = xValue; y = yValue; } // end constructor Point makePoint (int x, int y) { Point p1 = new Point(x,y); return p1; } // end method Point static void main (String[] args) { Point p1 = new Point(1,2); Point p2 = makePoint(3,4); } end method main } // end class Point … globals Point: x heap Point: y stack main(): p2 main(): p1 main(): args After “new” but before constructor is executed

  7. Memory in action public class Point { int x; int y; Point (int xValue, int yValue) { x = xValue; y = yValue; } // end constructor Point makePoint (int x, int y) { Point p1 = new Point(x,y); return p1; } // end method Point static void main (String[] args) { Point p1 = new Point(1,2); Point p2 = makePoint(3,4); } end method main } // end class Point … globals Point: x heap Point: y Point(): xValue 1 Point(): yValue 2 stack main(): p2 main(): p1 main:() args constructor is about to begin

  8. Memory in action public class Point { int x; int y; Point (int xValue, int yValue) { x = xValue; y = yValue; } // end constructor Point makePoint (int x, int y) { Point p1 = new Point(x,y); return p1; } // end method Point static void main (String[] args) { Point p1 = new Point(1,2); Point p2 = makePoint(3,4); } end method main } // end class Point … globals Point: x 1 heap Point: y 2 Point(): xValue 1 Point(): yValue 2 stack main(): p2 main(): p1 main(): args constructor is about to finish

  9. Memory in action public class Point { int x; int y; Point (int xValue, int yValue) { x = xValue; y = yValue; } // end constructor Point makePoint (int x, int y) { Point p1 = new Point(x,y); return p1; } // end method Point static void main (String[] args) { Point p1 = new Point(1,2); Point p2 = makePoint(3,4); } end method main } // end class Point … globals Point: x 1 heap Point: y 2 stack main(): p2 main(): p1 main(): args Execution of the first line of main complete

  10. Memory in action public class Point { int x; int y; Point (int xValue, int yValue) { x = xValue; y = yValue; } // end constructor Point makePoint (int x, int y) { Point p1 = new Point(x,y); return p1; } // end method Point static void main (String[] args) { Point p1 = new Point(1,2); Point p2 = makePoint(3,4); } end method main } // end class Point … globals Point: x 1 heap Point: y 2 makePoint(): y 3 makePoint(): x 4 stack main(): p2 main(): p1 main(): args Preparing to execute makePoint

  11. Memory in action public class Point { int x; int y; Point (int xValue, int yValue) { x = xValue; y = yValue; } // end constructor Point makePoint (int x, int y) { Point p1 = new Point(x,y); return p1; } // end method Point static void main (String[] args) { Point p1 = new Point(1,2); Point p2 = makePoint(3,4); } end method main } // end class Point … globals Point: x 1 heap Point: y 2 Point: x Point: y makePoint(): p1 makePoint(): y 3 makePoint(): x 4 stack main(): p2 main(): p1 main(): args makePoint, after new, before constructor called

  12. Memory in action public class Point { int x; int y; Point (int xValue, int yValue) { x = xValue; y = yValue; } // end constructor Point makePoint (int x, int y) { Point p1 = new Point(x,y); return p1; } // end method Point static void main (String[] args) { Point p1 = new Point(1,2); Point p2 = makePoint(3,4); } end method main } // end class Point … globals Point: x 1 heap Point: y 2 Point: x 3 Point: y 4 Point(): xValue 3 Point(): yValue 4 makePoint(): p1 makePoint(): y 3 makePoint(): x 4 stack main(): p2 main(): p1 main(): args makePoint near end of constructor call

  13. Memory in action public class Point { int x; int y; Point (int xValue, int yValue) { x = xValue; y = yValue; } // end constructor Point makePoint (int x, int y) { Point p1 = new Point(x,y); return p1; } // end method Point static void main (String[] args) { Point p1 = new Point(1,2); Point p2 = makePoint(3,4); } //end method main } // end class Point … globals Point: x 1 heap Point: y 2 Point: x 3 Point: y 4 makePoint(): p1 makePoint(): y 3 makePoint(): x 4 stack main(): p2 main(): p1 main(): args makePoint near end of constructor call

  14. Memory in action public class Point { int x; int y; Point (int xValue, int yValue) { x = xValue; y = yValue; } // end constructor Point makePoint (int x, int y) { Point p1 = new Point(x,y); return p1; } // end method Point static void main (String[] args) { Point p1 = new Point(1,2); Point p2 = makePoint(3,4); } end method main } // end class Point … globals Point: x 1 heap Point: y 2 Point: x 3 Point: y 4 stack main(): p2 main(): p1 main(): args end of main

  15. Methods are stacked • When you call a method, its frame is pushed on the call stack • Frame: The ordered set of local variables that belong to a method • Stack: A list in which a data structure can be removed only if all objects added more recently than it are first removed. FILO. • A method stays on the stack until it ends (hits it closing curly brace). • If the foo() method calls the bar() method then the frame for bar() is stacked on top of foo(). When bar completes, it (and all its variables) are removed (poof!) and foo() continues. • All local variables exist in/on the stack, in the stack frame for the method call. • If the variable exists, it is alive • If another frame is on top of it, however, it is out of scope • If the method ends then the variable no longer exists in memory • In Java, local variables get a default initialization of 0. Not true in C++!

  16. Stack Trace Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 3 at java.lang.String.charAt(Unknown Source) at Coordinate.getStatus (Coordinate: 67) at Ship.getLocation (Ship: 15) at GameBoard.checkTarget (GameBoard.java: 108) at GameBoard.play (GameBoard.java: 54) at Main.main (Main.java: 23) • We have caught an exception due to trying to reference a character beyond the end of a string, and printed its stack trace. • The stack trace shows us the flow of method calls from the instantiation of the program until the error occurred, and shows us the order of methods present on the stack.

  17. Objects are allocated • When you create an object instance memory is allocated on the heap. • There is NO WAY to create an object other than someone, somewhere using new on the class type • An object instance needs a place to store its instance variables. • The instance variables determine the size of the data structure. • heap: Essentially, a list in which data structures can be added or removed in any order and at any time. • Objects can be allocated anywhere in the heap where there is space (not necessarily adjacent to each other) • Any number of instances can be created, each starting at a unique address/location in the heap. • Object reference variables (which could be static, local, or instance) hold the reference/location of the object on the heap • no live variable with reference <==> object is inaccessible garbage

  18. Static variables have a permanent home • When you create a static variable, it is assigned a permanent location in memory • A static variable is always live • (from the time the class is loaded at start until the program ends) • Static variables don’t exist in the object’s data structure in the heap • This is why a static variables has the same value for all instances of a class. They all refer to the same memory location • You should always assign an initial value to static variables • In Java and C++, static variables are assigned a default value of 0

  19. null • What value should an object reference variable have BEFORE an object is created and stores its reference in the variable? • null • is a keyword • can be stored in a reference variable • Indicates that the variable current refers to no existing object • The concept of null is particularly important in languages without garbage collection, as references may be stored to objects that have been destroyed • In general, methods have a precondition that the parameters contain valid object references. • Best practices imply that one should always test for null references before using an object reference passed as a parameter • if( x == null) …

  20. Example: Memory public class PointList extends Point { PointList nextPoint = new PointList (); static PointList firstPoint; PointList () { super(0,0); } // end constructor public static void main (String[] args) { PointList list = new PointList(); list = null; } // end method main } // end class Point • What is the likely intention of this class? • What are the static, instance, and local variables? • This code compiles without error. When executed, you get a runtime exception: StackOverflowError. Why and how?

  21. Aggregate object • Are cards private or not? String “Heart” Card suit: rank: Card suit: rank: Card suit: rank: Deck cardsInDeck: ArrayList String “Two” … String “Club” String “Seven” … … public class Deck { private ArrayList<Card> cardsInDeck; … public Card topCard() { return cardsInDeck.get(0); } // end method topCard } // end class Deck String “Spade” String “Queen”

  22. Memory and encapsulation String “Heart” Card suit: rank: Card suit: rank: Card suit: rank: Deck cardsInDeck: ArrayList String “Two” … String “Club” … cheat(): topCard String “Seven” … … … public class Poker { … public void cheat () { Card topCard = topCard(); topCard.setRank(“Ace”); } // end method cheat } // end class Poker String “Spade” String “Queen”

  23. Security and encapsulation • When an accessor method exists to provide access to a private object: • Do not return the reference to the actual field object • This would allow the external program to access/change the object without going through the appropriate mutator method! • Instead, return a copy of the object. • This provides all the same information • But changes to the copy do not affect the original! • You have to make a new object • Wouldn’t it be great if we had a constructor that made this easy?

  24. Copy constructors • Copy constructor: A constructor that accepts an object of the same class as an argument and makes an “identical” copy. public class Deck { private ArrayList<Card> cardsInDeck; … public Card topCard() { return new Card(cardsInDeck.get(0)); } // end method topCard } // end class Deck public class Card { public Card (Card originalCard) { this.setSuit(originalCard.getSuit()); this.setRank(originalCard.getRank()); } // end copy constructor … } // end class Card Why are Strings Immutable in Java?

  25. Shallow Copy public class Deck { private ArrayList<Card> cardsInDeck; … public Deck (Deck originalDeck) { this.cardsInDeck = new ArrayList<Card>(); for (Card card : cardsInDeck) { this.cardsInDeck.add(card); } } // end (SHALLOW) copy constructor } // end class Deck Deck ArrayList Cards …

  26. Deep Copy • To create a secure copy, you cannot forward any reference to any object (except immutable objects) in your aggregation • Style: well designed objects should have a copy constructor that uses the copy constructors of the objects that it holds to ensure a deep copy public class Deck { private ArrayList<Card> cardsInDeck; … public Deck (Deck originalDeck) { this.cardsInDeck = new ArrayList<Card>(); for (Card card : cardsInDeck) { this.cardsInDeck.add(new Card(card)); } } // end (SHALLOW) copy constructor } // end class Deck Deck ArrayList Cards …

More Related