View
213
Download
0
Embed Size (px)
Citation preview
Cmput 115 - Lecture 8
Department of Computing Science
University of Alberta
©Duane Szafron 2000
Revised 1/26/00
The Java Memory ModelThe Java Memory Model
©Duane Szafron 1999
2
About This LectureAbout This Lecture
In this lecture we will learn how memory is structured and used in Java programs.
©Duane Szafron 1999
3
OutlineOutline
Memory References The stack and the heap Parameter passing Objects on the stack Assignment
©Duane Szafron 1999
4
Why Study a Memory Model?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 1999
5
MemoryMemory
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 1999
6
Accessing MemoryAccessing 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
00100110Registers
CPU 01101011101101011111111
000100020003
fetch 0001
store 0003
01101011
0010011000100110
01101011
©Duane Szafron 1999
7
ReferencesReferences
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 1999
8
Direct References in MethodsDirect 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 1999
9
Method ActivationsMethod 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 1999
10
Example MethodExample 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 1999
11
Local Variables map to AddressesLocal 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.
thischildindexanIntnewPerson
012300012304012308012312012316
stack frame for alpha
{
{
©Duane Szafron 1999
12
Assignment of ValuesAssignment 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;
012300012304012308012312012316
thischildindexanIntnewPerson
stack frame for alpha
30
©Duane Szafron 1999
13
Assignment of ObjectsAssignment 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
012300012304012308012312012316
thischildindexanIntnewPerson
stack frame for alpha
object ref
©Duane Szafron 1999
14
Object MemoryObject 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);
nameage
100234100238
a Person object
}
©Duane Szafron 1999
15
Object Memory OrganizationObject 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);
nameage
100234100238
a Person objectobject ref
30
©Duane Szafron 1999
16
Object References map to Object References map to AddressesAddresses
An object reference maps to an address in the heap called a pointer.
newPerson = new Person(“Fred”, anInt);
30
012300012304012308012312012316 100234
thischildindexanIntnewPerson
10050030
nameage
100234100238
100500100504100508
valueoffsetcount
10098004
stack frame for alpha
a Person object
a String object
100980101984101988
lengthdata
4‘F’
an Array
‘r’‘e’ ‘d’
©Duane Szafron 1999
17
Object References as ArrowsObject References as Arrows
When we draw pictures of memory, we often omit the addresses and represent object references as arrows.
30
thischildindexanIntnewPerson
30nameage
04
stack frame for alpha
a Person object
a String objectvalueoffsetcount
lengthdata
4‘F’
an Array
‘r’‘e’ ‘d’
©Duane Szafron 1999
18
Parameter Passing in JavaParameter 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 1999
19
Example Parameter PassingExample 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);
???100620100844
012004012008012012012016
thisaPersondaughterchildIndex
stack frame for caller100200
28nameage
a Person object100620100621
1003345
nameage
a Person object100844100848
100620100844
1
012300012304012308012312012316
thischildindexanIntnewPerson
stack frame for alpha1
©Duane Szafron 1999
20
Example Parameter Passing - Example Parameter Passing - ArrowsArrows
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
thisaPersondaughterchildIndex
stack frame for caller
28nameage
a Person object
5nameage
a Person object1
thischildindexanIntnewPerson
stack frame for alpha
©Duane Szafron 1999
21
Objects in ObjectsObjects 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
100234100238100242100246
a Person object
4‘F’ ‘r’‘e’ ‘d’
28
©Duane Szafron 1999
22
Objects on the StackObjects 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
100234100238100242100246100250
4‘F’ ‘r’‘e’ ‘d’
2810
stack frame
©Duane Szafron 1999
23
The Assignment Operation The Assignment Operation RevisitedRevisited
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 1999
24
Second Java Assignment Second Java Assignment ExampleExample
Person person1;
Person person2;int int1; int int2;person1 = new Person(“Fred”, 30);int1 = 7;person2 = person1;int2 = int1;
77
person1person2int1int2
stack frame
30nameage
04
a String objectvalueoffsetcount
lengthdata
4‘F’
an Array
‘r’‘e’ ‘d’
©Duane Szafron 1999
25
Stack Object or Structure Stack Object or Structure AssignmentAssignment
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 1999
26
Stack Assignment ExampleStack Assignment Example
Person person1;
Person person2;int int1;int int2;person1 = new Person(“Fred”, 30);int1 = 7;person2 = person1;int2 = int1; person1
person2
int1int2
stack frame
4‘F’ ‘r’‘e’ ‘d’
4‘F’ ‘r’‘e’ ‘d’
3030
©Duane Szafron 1999
27
The Memory for CollectionsThe 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 1999
28
Java Collection Memory Java Collection Memory ExampleExample
Person people[ ];
people = new Person[4];
for (index = 0; index < 4, index++)
people[index] = new Person(“name” + index, index);
people
stack frame
4lengthdata
an Array
nameage 0
05
a String object
lengthdata
5‘n’
an Array
‘a’‘m’ ‘e’‘0’
1nameage
valueoffsetcount
2nameage
3nameage
©Duane Szafron 1999
29
CollectionCollection Stack Memory Stack Memory ExampleExample
stack framepeople 5
‘n’ ‘a’‘m’ ‘e’‘0’
05
‘n’ ‘a’‘m’ ‘e’‘1’
1
5‘n’ ‘a’‘m’ ‘e’‘2’
25
‘n’ ‘a’‘m’ ‘e’‘3’
3