10-ObjectsAndMemory
10-ObjectsAndMemory
Java
An Introduction
ERIC S. ROBERTS to Computer Science
CHAPTER 7
. .
.
• In Java, it is impossible to determine the address of .
FFD0
FFF4
an object. Memory addresses used in the examples FFF5
FFD4
static
are never created or destroyed as the program runs, data
such as named constants and other class variables.
This information is called static data.
• Whenever you create a new object, Java allocates heap
it allocates heap space for the new Rational object. For this
example, imagine that the object is created at address 1000.
• The local variable r1 is allocated in the current stack frame
and is assigned the value 1000, which identifies the object.
heap stack
1000
num 1 1004
den 2 1008 r1 1000 FFFC
heap
heap stack
stack TestRational
TestRational
1000
1000
1/2 ++ 1/3
1/2 1/3 ++ 1/6
1/6 == 11
num
num 11 1004
1004
den
den 22 1008
1008 All objects are created
100C
100C in the heap.
num
num 11 1010
1010
den
den 33 1014
1014
1018
1018 This object is a temporary value
num
num 11 101C
101C used only during
r the100C
calculation.
1018 FFE0 This stack frame is created
den
den 66 1020
1020 this 1000
1024 FFE4 for the add method.
1024
1024 FFE8
num
num 55 1028
1028 sum
sum 1030
1030 FFEC
FFEC
den
den 66 102C
102C cc 1018
1018 FFF0
FFF0 This stack frame is created
1030
1030 bb 100C
100C FFF4
FFF4 for the main method.
num
num 11 1034
1034 aa 1000
1000 FFF8
FFF8
den
den 11 1038
1038 FFFC
FFFC skip simulation
The Pointer Model
• The heap-stack diagram at the lower left shows the state of
memory at the end of the run method from TestRational.
• The diagram at the lower right shows exactly the same state
using arrows instead of numeric addresses. This style of
diagram is said to use the pointer model.
heap stack heap stack
1000
num 1 1004 num 1
den 2 1008 den 2
100C
num 1 1010 num 1
den 3 1014 den 3
1018
num 1 101C num 1
den 6 1020 den 6
1024
num 5 1028 sum 1030 FFEC num 5 sum
den 6 102C c 1018 FFF0 den 6 c
1030 b 100C FFF4 b
num 1 1034 a 1000 FFF8 num 1 a
den 1 1038 FFFC den 1
Addresses vs. Pointers
• The two heap-stack diagram formats—the address model and
the pointer model—describe exactly the same memory state.
The models, however, emphasize different things:
– The address model makes it clear that references have numeric values.
– The pointer model emphasizes the relationship between the reference
and the object and makes the diagram easier to follow.
heap stack heap stack
1000
num 1 1004 num 1
den 2 1008 den 2
100C
num 1 1010 num 1
den 3 1014 den 3
1018
num 1 101C num 1
den 6 1020 den 6
1024
num 5 1028 sum 1030 FFEC num 5 sum
den 6 102C c 1018 FFF0 den 6 c
1030 b 100C FFF4 b
num 1 1034 a 1000 FFF8 num 1 a
den 1 1038 FFFC den 1
Garbage Collection
• One fact that the pointer model makes clear in this diagram is
that there are no longer any references to the Rational value
5/6. That value has now become garbage.
• From time to time, Java runs through the heap and reclaims
any garbage. This process is called garbage collection.
heap stack
num 1
den 2
num 1
den 3
num 1
This object was used to hold a temporary
den 6
result and is no longer accessible.
num 5 sum
den 6 c
b
num 1 a
den 1
Exercise: Stack-Heap Diagrams
Suppose that the classes Point and Line are defined as follows:
public class Point { public class Line {
public Point(int x, int y) { public Line(Point p1,
cx = x; Point p2) {
cy = y; start = p1;
} finish = p2;
}
. . . other methods appear here . . .
. . . other methods appear here . . .
private int cx;
private int cy; private Point start;
} private Point finish;
}
OUTPUT:
Linking Objects Together
• Although most examples of this technique are beyond the
scope of a first course, references are particularly important in
computer science because they make it possible to represent
the relationship among objects by linking them together in
various ways.
• One common example is called a linked list, in which each
object in a sequence contains a reference to the one that
follows it:
• Java marks the end of linked list using the constant null,
which signifies a reference that does not actually point to an
actual object. The value null has several other uses, as you
will discover in the chapters that follow.
The Beacons of Gondor
For answer Gandalf cried aloud to his horse. “On, Shadowfax!
We must hasten. Time is short. See! The beacons of Gondor are
alight, calling for aid. War is kindled. See, there is the fire on
Amon Dîn, and flame on Eilenach; and there they go speeding
west: Nardol, Erelas, Min-Rimmon, Calenhad, and the Halifirien
on the borders of Rohan.”
—J. R. R. Tolkien, The Return of the King, 1955
Minas Tirith Amon Dîn Eilenach Nardol Erelas Min-Rimmon Calenhad Halifirien Rohan
Message Passing in Linked Structures
To represent this message-passing public class SignalTower {
image, you might use a definition /* Constructs a new signal tower */
such as the one shown on the right. public SignalTower(String name,
SignalTower link) {
You can then initialize a chain of towerName = name;
nextTower = link;
SignalTower objects, like this: }
/*
Minas Tirith Min-Rimmon * Signals this tower and passes the
* message along to the next one.
*/
Amon Dîn Calenhad
public void signal() {
lightCurrentTower();
if (nextTower != null) {
nextTower.signal();
Eilenach Halifirien }
}
Nardol Rohan
/* Marks this tower as lit */
public void lightCurrentTower() {
null
. . . code to draw a fire on this tower . . .
}
Erelas
/* Private instance variables */
private String towerName;
Calling signal on the first tower private SignalTower nextTower;
}
sends a message down the chain.
public static void main(String[] args) {
SignalTower rohan = new SignalTower("Rohan", null);
SignalTower halifirien = new SignalTower("Halifirien", rohan);
SignalTower calenhad = new SignalTower("Calenhad", halifirien);
SignalTower minRimmon = new SignalTower("Min-Rimmon", calenhad);
SignalTower erelas = new SignalTower("Erelas", minRimmon);
SignalTower nardol = new SignalTower("Nardol", erelas);
SignalTower eilenach = new SignalTower("Eilenach", nardol);
SignalTower amonDin = new SignalTower("Amin Din", eilenach);
SignalTower minasTirith = new SignalTower("Minas Tirith", amonDin);
minasTirith.signal();
}