Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implications of Substitution Fall 2005 OOPD John Anthony.

Similar presentations


Presentation on theme: "Implications of Substitution Fall 2005 OOPD John Anthony."— Presentation transcript:

1 Implications of Substitution Fall 2005 OOPD John Anthony

2 Topics Memory Allocation Strategies Memory Allocation Strategies The Java Memory Allocation Model The Java Memory Allocation Model A Java Stack and Heap Example A Java Stack and Heap Example Memory Allocation and Inheritance Memory Allocation and Inheritance An Example in C++ Using the Stack An Example in C++ Using the Stack Reference Assignment Strategies Reference Assignment Strategies

3 Memory Allocation Generally broken down into two strategies (although variance does exist).

4 Static (Stack-based) Allocation Static allocation means allocation of memory before the program starts and retention until the end. Static allocation means allocation of memory before the program starts and retention until the end.allocation Compiler determines the exact size of the data structure based on the software text. Compiler determines the exact size of the data structure based on the software text. Memory allocation and release is determined at procedure entry and exit. Memory allocation and release is determined at procedure entry and exit. This means that the size of the program and its “data” are determined before execution. This means that the size of the program and its “data” are determined before execution.

5 Dynamic (Heap-based) Allocation Dynamic allocation means allocation of memory during program execution. Dynamic allocation means allocation of memory during program execution.allocation Compiler cannot determine the exact size of the data structure based on the software text. Compiler cannot determine the exact size of the data structure based on the software text. Memory allocation and release is not determined at procedure entry and exit. Memory allocation and release is not determined at procedure entry and exit. Memory release either handled by programmer or system (garbage collection). Memory release either handled by programmer or system (garbage collection).

6 Java (Thread) Execution State Execution state of a Java thread has several runtime data areas: Java stack succession of frames representing method calls Object heap Objects used by a thread Method area Classes used by a thread

7 The Java Stack A new frame for each method execution A new frame for each method execution A frame includes local variables of the associated method and operand stack that contains partial results (operands) A frame includes local variables of the associated method and operand stack that contains partial results (operands) A frame also contains registers such as the program counter A frame also contains registers such as the program counter

8 Stack, Heap, and Method Area The heap associated with a thread contains all the objects used by the thread (objects accessible from the thread’s Java stack) The heap associated with a thread contains all the objects used by the thread (objects accessible from the thread’s Java stack) The Class Pool associated with a thread contains the classes used by the thread. The Class Pool associated with a thread contains the classes used by the thread. Class Pool Stack Object heap Class Reference Object Reference

9 Stack – A Closer Look Java Code Stack > int x; > x = 5; > double min = 0.5; > boolean done = false; Fernando Pereira University of Pennsylvania

10 Stack & Heap – A Closer Look Java Code Stack and Heap > int x = 99; > Counter c1; > c1 null > c1 = new Counter(); > c1 Counter@2f996f > c1.incrementCount(); > Counter c2 = new Counter(); > c2 Counter@4a0ac5 Fernando Pereira University of Pennsylvania

11 Value of a Reference Variable The value of are reference variable is either null or a “heap address” The value of are reference variable is either null or a “heap address” Example: Example: > Counter c1; > Counter c1; > c1 > c1 null null > c1 = new Counter(); > c1 = new Counter(); > c1 > c1 Counter@e05ad6 Counter@e05ad6 Counter@e05ad6 e05ad6 is a hexadecimal (base 16) number e05ad6 is a hexadecimal (base 16) number We don’t have to (and can’t) deal with these hex numbers directly We don’t have to (and can’t) deal with these hex numbers directly Fernando Pereira University of Pennsylvania

12 “Has a” Relationship We’ll look at an example where an object A has an instance variable which is an object whose type is B. (A “has a” B.) We’ll look at an example where an object A has an instance variable which is an object whose type is B. (A “has a” B.) We will create a DormRoom object, and a Freshman object whose room is that DormRoom We will create a DormRoom object, and a Freshman object whose room is that DormRoom What code should we write? What will the stack and the heap will look like?

13 public class DormRoom{ private int num; private String bldgName; public DormRoom(int n, String b){ num = n; bldgName = b; } public String getLocation(){ return num + " " + bldgName; } } > DormRoom room = new DormRoom(208, "Hill"); > room.getLocation() "208 Hill" DormRoom Code and UML

14 A DormRoom on the Heap > DormRoom room = new DormRoom(208, "Hill"); > room.getLocation() "208 Hill"

15 public class Freshman{ private String name; private DormRoom room; public Freshman(String n, DormRoom r){ name = n; room = r; } public String getName(){ return name;} public DormRoom getRoom(){ return room;} } > DormRoom room = new DormRoom(208, "Hill"); > Freshman f = new Freshman("jo", room); > f.getName() "jo" > f.getRoom().getLocation() "208 Hill" Freshman Code and UML

16 A Freshman on the Heap :) > DormRoom room = new DormRoom(208, "Hill"); > Freshman f = new Freshman("jo", room); > f.getName() "jo" > f.getRoom().getLocation() "208 Hill"

17 Memory Allocation and Inheritance If Stack Allocation is a more efficient memory allocation and reclamation strategy, then why use the heap? If Stack Allocation is a more efficient memory allocation and reclamation strategy, then why use the heap? Let’s look at one challenge as seen through the eyes of C++ and polymorphism. Let’s look at one challenge as seen through the eyes of C++ and polymorphism.

18 Consider the following…. class Window { public: virtual void oops(); private: int height; int width; }; class TextWindow : public Window { public: virtual void oops(); private: char * contents; int cursorLocation; }; If we create a new Window (Window win;), how much space on the Stack should be allocated?

19 Three Choices Allocate enough memory for the base class only. (C++) Allocate enough memory for the base class only. (C++) Allocate the maximum memory needed for any legal value (base or subclass). Allocate the maximum memory needed for any legal value (base or subclass). Allocate enough space to only hold a pointer. (Smalltalk, Java) Allocate enough space to only hold a pointer. (Smalltalk, Java)

20 Minimum Static Space Allocation C++ uses this approach. C++ uses this approach. What happens when we execute the following code: What happens when we execute the following code: Window win; Window *tWinPtr;... tWinPtr = new TextWindow... Win = *tWinPtr

21 Slicing height width … height width … contents cursorLocation … void Window::oops() { cout << “Window oops”” << endl; } Void Window::oops() { cout << “TextWindow oops”” << cursorLocation << endl; }

22 Rules for Member Function Binding in C++ Variables declared as references or pointers, the binding of the function name to the function body is based on the dynamic class (as you’d perhaps expect). Variables declared as references or pointers, the binding of the function name to the function body is based on the dynamic class (as you’d perhaps expect). With variables that are not pointers (i.e. declared on the stack), the binding of the function name to the function body is based on the static class. With variables that are not pointers (i.e. declared on the stack), the binding of the function name to the function body is based on the static class.

23 Example Window win; TextWindow *tWinPtr, *tWin;... tWinPtr= new Textwindow; win = * tWinPtr; tWin = tWinPtr; Win.oops();tWin.oops();

24 Maximum Static space Allocation Eliminates slicing problem by allocating the maximum amount of memory necessary. Eliminates slicing problem by allocating the maximum amount of memory necessary. In order to do this, the entire program needs to be scanned. In order to do this, the entire program needs to be scanned. What about dynamic class loading….? What about dynamic class loading….? No major OO language takes this approach due to restriction of “program scan”. No major OO language takes this approach due to restriction of “program scan”.

25 Dynamic Memory Allocation The value is stored in a separate data area called the Heap. The value is stored in a separate data area called the Heap. Fixed size pointers are stored on the Stack and point to the heap. Fixed size pointers are stored on the Stack and point to the heap. Space on the heap is allocated when the object is created via the “new” keyword (for Java). Space on the heap is allocated when the object is created via the “new” keyword (for Java). What happens when after executing an assignment statement? What happens when after executing an assignment statement?

26 Pointer Assignment Counter c1 = new Counter(); Counter c2 = new Counter(); c2 = c1; c1 c2 0 0 Stack Heap Counter

27 Copy Assignment Counter c1 = new Counter(); Counter c2 = new Counter(); c2 = c1; c1 c2 0 0 Stack Heap Counter 0 Copied as a result of assignment

28 A Hybrid Approach c1 c2 0 0 Stack Heap Counter 0 Copied as a result of modification (not assignment) Counter c1 = new Counter(); Counter c2 = new Counter(); c2 = c1;


Download ppt "Implications of Substitution Fall 2005 OOPD John Anthony."

Similar presentations


Ads by Google