Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cmput 115 - Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.

Similar presentations


Presentation on theme: "Cmput 115 - Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model."— Presentation transcript:

1 Cmput 115 - Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model

2 ©Duane Szafron 1999 2 About This Lecture In this lecture we will learn how memory is structured and used in Java programs.

3 ©Duane Szafron 1999 3 Outline Memory References The stack and the heap Parameter passing Objects on the stack Assignment

4 ©Duane Szafron 1999 4 Why Study a Memory Model? In this course we are studying data structures and algorithms. To compare the space and time requirements of these data structures and algorithms we need to know how the data is actually stored in memory and how it is modified. The way that data is stored and accessed is called a memory model.

5 ©Duane Szafron 1999 5 Memory When a program is running, the data it needs is stored in memory in a binary format. In a simple memory model, there are two kinds of memory –a small set of memory that can be manipulated directly by the CPU is called register memory. –a larger set of memory that cannot be manipulated directly by the CPU is called main memory.

6 ©Duane Szafron 1999 6 Accessing Memory Every byte (8 binary digits) of main memory or word (1, 2, 4 or 8 bytes) is assigned a numerical address. A fetch instruction is used to copy the contents of one word of main memory at a particular address to register memory. A store instruction is used to copy the contents of a register to an address in main memory. 00000000 00100110 Registers CPU 01101011 10110101 1111111 0001 0002 0003 fetch 0001 store 0003 01101011 00100110 00100110 01101011

7 ©Duane Szafron 1999 7 References Application programmers use symbolic program references to refer to objects and values that are stored in memory, instead of numerical addresses. The compiler translates each symbolic program reference into an expression that evaluates to an address in main memory or a register. To understand how memory is used, we must look at how it is organized.

8 ©Duane Szafron 1999 8 Direct References in Methods When a method is executing it can access some objects and some values. The receiver object can be referenced directly using the pseudo-variable this. Other objects and values can be referenced directly using method parameters and local variables. Still other objects and values can only be accessed indirectly by sending messages that return references to them.

9 ©Duane Szafron 1999 9 Method Activations A method can only access objects while it is executing or active. The collection of all direct references in a method is called the frame or stack frame of a method. The frame is created when the method is invoked, and destroyed when the method finishes. The stack frames are organized into a region of memory called the run-time stack.

10 ©Duane Szafron 1999 10 Example Method Consider a method in the class Person, where each person has two instance variables, name which is bound to a String, and age which is bound to an int: public void alpha(Person child, int index) { int anInt; Person newPerson; anInt = 30; newPerson = new Person(“Fred”, anInt); }

11 ©Duane Szafron 1999 11 Local Variables map to Addresses The stack frames are stored in main memory where the receiver reference (this), the parameters and the local variables are mapped to consecutive memory locations that are part of the run-time stack. this child index anInt newPerson 012300 012304 012308 012312 012316 stack frame for alpha { {

12 ©Duane Szafron 1999 12 Assignment of Values When a value is assigned to a local variable, the value is put into the appropriate memory location in the stack frame. For example consider the statement: anInt = 30; 012300 012304 012308 012312 012316 this child index anInt newPerson stack frame for alpha 30

13 ©Duane Szafron 1999 13 Assignment of Objects When an object is assigned to a local variable, a reference to that object is put into the appropriate memory location in the stack frame. For example, consider the statement: newPerson = new Person(“Fred”, anInt); 30 012300 012304 012308 012312 012316 this child index anInt newPerson stack frame for alpha object ref

14 ©Duane Szafron 1999 14 Object Memory To understand the nature of the object reference we must consider the memory for the object itself. When an object is created, memory for that object is allocated in a region of memory called the heap where space is allocated for each instance variable of the object. For example: new Person(“Fred”, anInt); name age 100234 100238 a Person object }

15 ©Duane Szafron 1999 15 Object Memory Organization Each instance variable in an object is bound to a value or another object. If an instance variable is bound to a value, the value is stored directly. If an instance variable is bound to an object, a reference to that object is stored. A constructor initializes this memory. new Person(“Fred”, anInt); name age 100234 100238 a Person object object ref 30

16 ©Duane Szafron 1999 16 Object References map to Addresses An object reference maps to an address in the heap called a pointer. newPerson = new Person(“Fred”, anInt); 30 012300 012304 012308 012312 012316 100234 this child index anInt newPerson 100500 30 name age 100234 100238 100500 100504 100508 value offset count 100980 0 4 stack frame for alpha a Person object a String object 100980 101984 101988 length data 4 ‘F’ an Array ‘r’ ‘e’‘d’

17 ©Duane Szafron 1999 17 Object References as Arrows When we draw pictures of memory, we often omit the addresses and represent object references as arrows. 30 this child index anInt newPerson 30 name age 0 4 stack frame for alpha a Person object a String object value offset count length data 4 ‘F’ an Array ‘r’ ‘e’‘d’

18 ©Duane Szafron 1999 18 Parameter Passing in Java When a method call is made, data from a calling method must be transferred to the method that is called. How is the argument data passed to the method? Java uses call by value for each value parameter and call by reference for each object parameter. In call by value, a copy of the data value is put into the stack frame for the corresponding parameter. In call by reference, a reference to an object is put into the stack frame.

19 ©Duane Szafron 1999 19 Example Parameter Passing Consider some code that calls the method alpha: aPerson = new Person(“Wilma”, 28); daughter = new Person(“Pebbles”, 5); childIndex = 1; aPerson.alpha(daughter, childIndex); ??? 100620 100844 012004 012008 012012 012016 this aPerson daughter childIndex stack frame for caller 100200 28 name age a Person object 100620 100621 100334 5 name age a Person object 100844 100848 100620 100844 1 012300 012304 012308 012312 012316 this child index anInt newPerson stack frame for alpha 1

20 ©Duane Szafron 1999 20 Example Parameter Passing - Arrows Consider some code that calls the method alpha: aPerson = new Person(“Wilma”, 28); daughter = new Person(“Pebbles”, 5); childIndex = 1; aPerson.alpha(daughter, childIndex); 1 this aPerson daughter childIndex stack frame for caller 28 name age a Person object 5 name age a Person object 1 this child index anInt newPerson stack frame for alpha

21 ©Duane Szafron 1999 21 Objects in Objects Some languages (C++) allow objects to contain other objects rather than just references to other objects. Some languages (C, Pascal) use structures or records for composite data instead of objects. In this case the layout of an object is “flattened”: name age 100234 100238 100242 100246 a Person object 4 ‘F’‘r’ ‘e’‘d’ 28

22 ©Duane Szafron 1999 22 Objects on the Stack Some languages (C++, C, Pascal) allow objects and structures to be allocated directly in a stack frame instead of the heap. Person aPerson; int anInt; aPerson anInt 100234 100238 100242 100246 100250 4 ‘F’‘r’ ‘e’‘d’ 28 10 stack frame

23 ©Duane Szafron 1999 23 The Assignment Operation Revisited Assigning a value to a value variable, copies the value. Assigning an object to an object variable, copies only the object reference, not the entire object.

24 ©Duane Szafron 1999 24 Second Java Assignment Example Person person1; Person person2; int int1; int int2; person1 = new Person(“Fred”, 30); int1 = 7; person2 = person1; int2 = int1; 7 7 person1 person2 int1 int2 stack frame 30 name age 0 4 a String object value offset count length data 4 ‘F’ an Array ‘r’ ‘e’‘d’

25 ©Duane Szafron 1999 25 Stack Object or Structure Assignment If an object or a structure is on the stack (not possible in Java), then assigning it to a variable, copies the entire object or structure.

26 ©Duane Szafron 1999 26 Stack Assignment Example Person person1; Person person2; int int1; int int2; person1 = new Person(“Fred”, 30); int1 = 7; person2 = person1; int2 = int1; person1 person2 int1 int2 stack frame 4 ‘F’‘r’ ‘e’‘d’ 4 ‘F’‘r’ ‘e’‘d’ 30

27 ©Duane Szafron 1999 27 The Memory for Collections In Java, an array of Objects or a Vector of objects, actually contains references to objects. The memory for the collection itself is one four byte object reference per element, regardless of the size of the element object itself. In other languages all of the memory for the collection may be in the collection itself and this may even be in the stack.

28 ©Duane Szafron 1999 28 Java Collection Memory Example Person people[ ]; people = new Person[4]; for (index = 0; index < 4, index++) people[index] = new Person(“name” + index, index); people stack frame 4 length data an Array name age 0 0 5 a String object length data 5 ‘n’ an Array ‘a’ ‘m’‘e’ ‘0’ 1 name age value offset count 2 name age 3 name age

29 ©Duane Szafron 1999 29 Collection Stack Memory Example stack frame people 5 ‘n’‘a’ ‘m’‘e’ ‘0’ 0 5 ‘n’‘a’ ‘m’‘e’ ‘1’ 1 5 ‘n’‘a’ ‘m’‘e’ ‘2’ 2 5 ‘n’‘a’ ‘m’‘e’ ‘3’ 3


Download ppt "Cmput 115 - Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model."

Similar presentations


Ads by Google