Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model
©Duane Szafron About This Lecture In this lecture we will learn how memory is structured and used in Java programs.
©Duane Szafron Outline Memory References The stack and the heap Parameter passing Objects on the stack Assignment
©Duane Szafron 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.
©Duane Szafron 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.
©Duane Szafron 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 Registers CPU fetch 0001 store
©Duane Szafron 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.
©Duane Szafron 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.
©Duane Szafron 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.
©Duane Szafron 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); }
©Duane Szafron 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 stack frame for alpha { {
©Duane Szafron 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; this child index anInt newPerson stack frame for alpha 30
©Duane Szafron 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); this child index anInt newPerson stack frame for alpha object ref
©Duane Szafron 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 a Person object }
©Duane Szafron 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 a Person object object ref 30
©Duane Szafron Object References map to Addresses An object reference maps to an address in the heap called a pointer. newPerson = new Person(“Fred”, anInt); this child index anInt newPerson name age value offset count stack frame for alpha a Person object a String object length data 4 ‘F’ an Array ‘r’ ‘e’‘d’
©Duane Szafron 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’
©Duane Szafron 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.
©Duane Szafron 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); ??? this aPerson daughter childIndex stack frame for caller name age a Person object name age a Person object this child index anInt newPerson stack frame for alpha 1
©Duane Szafron 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
©Duane Szafron 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 a Person object 4 ‘F’‘r’ ‘e’‘d’ 28
©Duane Szafron 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 ‘F’‘r’ ‘e’‘d’ stack frame
©Duane Szafron 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.
©Duane Szafron 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’
©Duane Szafron 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.
©Duane Szafron 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
©Duane Szafron 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.
©Duane Szafron 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 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
©Duane Szafron 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