Java Memory Management

 
Java Memory
Java Memory
Management
Management
 
8-2
 
Objectives
 
Understand how the memory of a
computer is used when executing a
program
Understand where objects, code, and
execution stack are stored in memory.
 
1-3
 
Memory Allocation in Java
 
When a program is being executed,
separate areas of memory are
allocated for
code (classes)
objects
execution stack
 
1-4
 
E
x
e
c
u
t
i
o
n
 
s
t
a
c
k
 
 
(
a
l
s
o
 
c
a
l
l
e
d
 
r
u
n
t
i
m
e
 
s
t
a
c
k
 
o
r
 
c
a
l
l
s
t
a
c
k
)
Used to store 
method
 information needed 
while the
method is being executed, 
like
Local variables
Formal parameters
Return value
Return address
H
e
a
p
Used to store
Code
Objects
 
Memory Areas
 
1-5
 
 
Execution
stack
 
code and
static objects
 
objects
 
Memory Allocated to a Program
 
Activation
record
 
Heap
 
public static void main (…) {
}
 
public void setNext(…) {
}
 
1-6
 
W
h
a
t
 
h
a
p
p
e
n
s
 
w
h
e
n
 
a
n
 
o
b
j
e
c
t
 
i
s
c
r
e
a
t
e
d
 
b
y
 
n
e
w
,
 
a
s
 
i
n
 
 
 
P
e
r
s
o
n
 
f
r
i
e
n
d
 
=
 
n
e
w
 
P
e
r
s
o
n
(
)
;
T
h
e
 
r
e
f
e
r
e
n
c
e
 
v
a
r
i
a
b
l
e
 
f
r
i
e
n
d
 
h
a
s
m
e
m
o
r
y
 
a
l
l
o
c
a
t
e
d
 
t
o
 
i
t
 
i
n
 
t
h
e
e
x
e
c
u
t
i
o
n
 
s
t
a
c
k
T
h
e
 
o
b
j
e
c
t
 
i
s
 
c
r
e
a
t
e
d
 
u
s
i
n
g
 
m
e
m
o
r
y
i
n
 
t
h
e
 
h
e
a
p
 
Memory Allocation in Java
 
1-7
 
Execution Stack
 
E
x
e
c
u
t
i
o
n
 
s
t
a
c
k
 
(
r
u
n
t
i
m
e
 
s
t
a
c
k
)
 
i
s
 
t
h
e
m
e
m
o
r
y
 
s
p
a
c
e
 
u
s
e
d
 
t
o
 
s
t
o
r
e
 
t
h
e
 
i
n
f
o
r
m
a
t
i
o
n
n
e
e
d
e
d
 
b
y
 
a
 
m
e
t
h
o
d
,
 
w
h
i
l
e
 
t
h
e
 
m
e
t
h
o
d
 
t
h
e
 
i
s
b
e
i
n
g
 
e
x
e
c
u
t
e
d
W
h
e
n
 
a
 
m
e
t
h
o
d
 
i
s
 
i
n
v
o
k
e
d
,
 
a
n
 
a
c
t
i
v
a
t
i
o
n
r
e
c
o
r
d
 
(
o
r
 
c
a
l
l
 
f
r
a
m
e
)
 
f
o
r
 
t
h
a
t
 
m
e
t
h
o
d
 
i
s
c
r
e
a
t
e
d
 
a
n
d
 
p
u
s
h
e
d
 
o
n
t
o
 
t
h
e
 
e
x
e
c
u
t
i
o
n
 
s
t
a
c
k
All the information needed during the
execution of the method is stored in an
activation record
 
1-8
 
Activation Record
for a Method
R
e
t
u
r
n
 
v
a
l
u
e
 
L
o
c
a
l
 
v
a
r
i
a
b
l
e
s
 
F
o
r
m
a
l
 
P
a
r
a
m
e
t
e
r
s
 
R
e
t
u
r
n
 
a
d
d
r
e
s
s
 
1-9
 
Activation Record
 
A
n
 
a
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
 
c
o
n
t
a
i
n
s
:
Address to return to after method ends
Method’s formal parameter variables
Method’s local variables
Return value (if any)
 
N
o
t
e
 
t
h
a
t
 
t
h
e
 
v
a
l
u
e
s
 
i
n
 
a
n
 
a
c
t
i
v
a
t
i
o
n
r
e
c
o
r
d
 
a
r
e
 
a
c
c
e
s
s
i
b
l
e
 
o
n
l
y
 
w
h
i
l
e
 
t
h
e
c
o
r
r
e
s
p
o
n
d
i
n
g
 
m
e
t
h
o
d
 
i
s
 
b
e
i
n
g
e
x
e
c
u
t
e
d
!
 
1-10
public class CallStackDemo  {
 
public static void 
m2
( ) {
  
System.out.println("Starting m2");
  
System.out.println("m2 calling m3");
  
m3();
  
System.out.println("m2 calling m4");
  
m4();
  
System.out.println("Leaving m2");
  
return;
 
}
 
public static void 
m3
( ) {
  
System.out.println("Starting m3");
  
System.out.println("Leaving m3");
  
return;
 
}
 
1-11
 
public static void 
m4
( ) {
  
System.out.println("Starting m4");
  
System.out.println("Leaving m4");
  
return;
 
}
 
public static void 
main
(String args[ ]) {
  
System.out.println("Starting main");
  
System.out.println("main calling m2");
  
m2( );
  
System.out.println("Leaving main");
 
}
}
 
1-12
 
 
Execution Stack for
 
a Typical Calling Sequence
 
Activation
record for
main
 
Activation
record for
main
 
Activation
record for
m2
 
Activation
record for
main
 
Activation
record for
m2
 
Activation
record for
m3
 
m
a
i
n
 
c
a
l
l
s
 
m
2
 
m
2
 
c
a
l
l
s
 
m
3
 
1-13
 
 
Execution Stack for
 
a Typical Calling Sequence
 
Activation
record for
main
 
Activation
record for
main
 
Activation
record for
m2
 
Activation
record for
main
 
Activation
record for
m2
 
Activation
record for
m4
 
Activation
record for
m2
 
R
e
t
u
r
n
 
f
r
o
m
 
m
3
 
m
2
 
c
a
l
l
s
 
m
4
 
R
e
t
u
r
n
 
f
r
o
m
 
m
4
 
1-14
 
W
h
e
n
 
t
h
e
 
m
a
i
n
 
m
e
t
h
o
d
 
i
s
 
i
n
v
o
k
e
d
:
An 
activation record 
for
 main
 is created
and pushed onto the execution stack
W
h
e
n
 
m
a
i
n
 
c
a
l
l
s
 
t
h
e
 
m
e
t
h
o
d
 
m
2
:
An 
activation record for m2
 is created and
pushed onto the execution stack
W
h
e
n
 
m
2
 
c
a
l
l
s
 
m
3
:
An 
activation record for m3
 is created and
pushed onto the execution stack
W
h
e
n
 
m
3
 
t
e
r
m
i
n
a
t
e
s
,
 
i
t
s
 
a
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
 
i
s
p
o
p
p
e
d
 
o
f
f
 
a
n
d
 
c
o
n
t
r
o
l
 
r
e
t
u
r
n
s
 
t
o
 
m
2
 
Execution Stack for a Typical Calling Sequence
 
1-15
 
W
h
e
n
 
m
2
 
n
e
x
t
 
c
a
l
l
s
 
m
4
:
What happens next?
W
h
a
t
 
h
a
p
p
e
n
s
 
w
h
e
n
 
m
4
 
t
e
r
m
i
n
a
t
e
s
?
W
h
a
t
 
h
a
p
p
e
n
s
 
w
h
e
n
 
m
2
 
t
e
r
m
i
n
a
t
e
s
?
W
h
a
t
 
h
a
p
p
e
n
s
 
w
h
e
n
 
m
a
i
n
 
t
e
r
m
i
n
a
t
e
s
?
I
t
s
 
a
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
 
i
s
 
p
o
p
p
e
d
 
o
f
f
 
a
n
d
c
o
n
t
r
o
l
 
r
e
t
u
r
n
s
 
t
o
 
t
h
e
 
o
p
e
r
a
t
i
n
g
 
s
y
s
t
e
m
 
Execution Stack for a Typical Calling Sequence
 
1-16
 
Activation Records
 
We will now look at some examples of
what is in the activation record for a
method
First for simple variables
Then for reference variables
 
1-17
 
Example: Activation Records- Simple Variables
public class CallFrameDemo1 {
 
public static double 
square
(double n){
  
double temp;
  
temp = n * n;
  
return temp;
 
}
 
 
public static void 
main
(String args[ ]) 
 
{
  
double x = 4.5;
  
double y;
  
y = square(x);
  
System.out.println("Square of " + x + " is " + y);
 
}
}
 
1-18
 
D
r
a
w
 
a
 
p
i
c
t
u
r
e
 
o
f
 
t
h
e
 
a
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
s
 
o
n
 
t
h
e
e
x
e
c
u
t
i
o
n
 
s
t
a
c
k
:
W
h
a
t
 
w
i
l
l
 
b
e
 
i
n
 
t
h
e
 
a
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
 
f
o
r
 
t
h
e
 
m
a
i
n
m
e
t
h
o
d
?
Address to return to in operating system
V
a
r
i
a
b
l
e
 
a
r
g
s
V
a
r
i
a
b
l
e
 
x
V
a
r
i
a
b
l
e
 
y
What will be in the activation record for the method
square
?
Address to return to in 
main
V
a
r
i
a
b
l
e
 
n
V
a
r
i
a
b
l
e
 
t
e
m
p
Return value
 
Activation Records – Example 1
 
1-19
 
T
h
e
r
e
 
w
i
l
l
 
b
e
 
a
n
 
a
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
 
o
n
 
t
h
e
e
x
e
c
u
t
i
o
n
 
s
t
a
c
k
 
f
o
r
 
e
a
c
h
 
m
e
t
h
o
d
 
c
a
l
l
e
d
.
 
S
o
w
h
a
t
 
o
t
h
e
r
 
a
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
(
s
)
 
w
i
l
l
 
b
e
 
p
u
s
h
e
d
o
n
t
o
 
t
h
e
 
e
x
e
c
u
t
i
o
n
 
s
t
a
c
k
 
f
o
r
 
o
u
r
 
e
x
a
m
p
l
e
?
Which activation records will be on the
execution stack at the same time?
 
Discussion
 
1-20
 
Heap
 
S
t
a
t
i
c
 
s
p
a
c
e
:
c
o
n
t
a
i
n
s
 
o
n
e
 
c
o
p
y
 
o
f
 
t
h
e
 
c
o
d
e
 
o
f
 
e
a
c
h
c
l
a
s
s
 
u
s
e
d
 
i
n
 
t
h
e
 
p
r
o
g
r
a
m
also contains static objects
D
y
n
a
m
i
c
 
o
r
 
O
b
j
e
c
t
 
s
p
a
c
e
:
I
n
f
o
r
m
a
t
i
o
n
 
t
h
a
t
 
i
s
 
s
t
o
r
e
d
 
f
o
r
 
e
a
c
h
 
o
b
j
e
c
t
:
values of its instance variables
reference to its code
 
Execution Stack,
Call Stack, or
Runtime Stack
 
H
e
a
p
 
S
t
a
t
i
c
 
h
e
a
p
 
D
y
n
a
m
i
c
 
h
e
a
p
 
C
o
d
e
 
 
S
t
a
t
i
c
o
b
j
e
c
t
s
 
O
b
j
e
c
t
s
 
A
c
t
i
v
a
t
i
o
n
R
e
c
o
r
d
s
 
1-22
 
Object Creation
 
M
e
m
o
r
y
 
i
s
 
a
l
l
o
c
a
t
e
d
 
i
n
 
t
h
e
 
h
e
a
p
 
a
r
e
a
w
h
e
n
 
a
n
 
o
b
j
e
c
t
 
i
s
 
c
r
e
a
t
e
d
 
u
s
i
n
g
 
t
h
e
o
p
e
r
a
t
o
r
 
n
e
w
Reference variables are allocated memory
in the 
activation records
 in the 
execution
stack
The objects are allocated memory in the
heap
 
1-23
public class CallFrameDemo2 {
 
 
private static void 
printAll
(String s1, String s2, String s3){
  
System.out.println(s1.toString( ));
  
System.out.println(s2.toString( ));
  
System.out.println(s3.toString( ));
 
 }
 
public static void 
main
(String args[ ]) {
  
String str1, str2, str3;
 
  
str1 = new String(“ string 1 ”);
  
str2 = new String(“ string 2 ”);
  
str3 = new String(“ string 3 ”);
 
  
printAll(str1, str2, str3);
 
 }
}
 
1-24
 
Draw a picture of the execution stack and of the heap as
the above program executes:
Activation record for 
main
Activation record for 
String constructor
 for 
str1
 – then
popped off
Activation record for 
String constructor
 for 
str2
 – then
popped off
Activation record for 
String constructor
 for 
str3
 – then
popped off
A
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
 
f
o
r
 
p
r
i
n
t
A
l
l
Activation record for 
toString 
for 
str1
 – then popped off
A
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
 
f
o
r
 
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
 
 
 
t
h
e
n
 
p
o
p
p
e
d
o
f
f
etc.
 
Activation Records– Example 2
 
1-25
 
W
h
a
t
 
w
i
l
l
 
b
e
 
s
t
o
r
e
d
 
i
n
 
t
h
e
 
a
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
 
f
o
r
 
m
a
i
n
?
Address to return to in operating system
V
a
r
i
a
b
l
e
 
a
r
g
s
V
a
r
i
a
b
l
e
 
s
t
r
1
Initial value?
Value after return from 
String constructor
?
V
a
r
i
a
b
l
e
 
s
t
r
2
V
a
r
i
a
b
l
e
 
s
t
r
3
W
h
a
t
 
w
i
l
l
 
b
e
 
i
n
 
t
h
e
 
a
c
t
i
v
a
t
i
o
n
 
r
e
c
o
r
d
 
f
o
r
 
p
r
i
n
t
A
l
l
?
 
Activation Records– Example 2
 
1-26
 
Memory Deallocation
 
What happens when a method returns?
On the 
execution stack
:
The activation record is popped off
when the method returns
S
o
,
 
t
h
a
t
 
m
e
m
o
r
y
 
i
s
 
d
e
a
l
l
o
c
a
t
e
d
 
1-27
 
Memory Deallocation
 
What happens to 
objects
 on the heap?
An object stays in the heap even if there is
no longer a variable referencing it!
S
o
,
 
J
a
v
a
 
h
a
s
 
a
u
t
o
m
a
t
i
c
 
g
a
r
b
a
g
e
c
o
l
l
e
c
t
i
o
n
I
t
 
r
e
g
u
l
a
r
l
y
 
i
d
e
n
t
i
f
i
e
s
 
o
b
j
e
c
t
s
 
w
h
i
c
h
 
n
o
l
o
n
g
e
r
 
h
a
v
e
 
a
 
 
v
a
r
i
a
b
l
e
 
r
e
f
e
r
e
n
c
i
n
g
t
h
e
m
,
 
a
n
d
 
d
e
a
l
l
o
c
a
t
e
s
 
t
h
a
t
 
m
e
m
o
r
y
Slide Note
Embed
Share

Explore how memory is managed in Java programs, including memory allocation for code, objects, and execution stack. Learn about memory areas like the execution stack and heap, and understand the concept of activation records in method execution.

  • Java
  • Memory Management
  • Execution Stack
  • Heap
  • Activation Records

Uploaded on Sep 14, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Java Memory Management

  2. Objectives Understand how the memory of a computer is used when executing a program Understand where objects, code, and execution stack are stored in memory. 8-2

  3. Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for code (classes) objects execution stack 1-3

  4. Memory Areas Execution stack (also called runtime stack or call stack) Used to store method information needed while the method is being executed, like Local variables Formal parameters Return value Return address Heap Used to store Code Objects 1-4

  5. Memory Allocated to a Program Execution stack Heap Activation record public static void main ( ) { } public void setNext( ) { } code and static objects objects 1-5

  6. Memory Allocation in Java What happens when an object is created by new, as in Person friend = new Person( ); The reference variable friend has memory allocated to it in the execution stack The object is created using memory in the heap 1-6

  7. Execution Stack Execution stack (runtime stack) is the memory space used to store the information needed by a method, while the method the is being executed When a method is invoked, an activation record (or callframe) for that methodis created and pushed onto the execution stack All the information needed during the execution of the method is stored in an activation record 1-7

  8. Activation Record for a Method Return value Local variables Formal Parameters Return address 1-8

  9. Activation Record An activation record contains: Address to return to after method ends Method s formal parameter variables Method s local variables Return value (if any) Note that the values in an activation record are accessible only while the corresponding method is being executed! 1-9

  10. public class CallStackDemo { public static void m2( ) { System.out.println("Starting m2"); System.out.println("m2 calling m3"); m3(); System.out.println("m2 calling m4"); m4(); System.out.println("Leaving m2"); return; } public static void m3( ) { System.out.println("Starting m3"); System.out.println("Leaving m3"); return; } 1-10

  11. public static void m4( ) { System.out.println("Starting m4"); System.out.println("Leaving m4"); return; } public static void main(String args[ ]) { System.out.println("Starting main"); System.out.println("main calling m2"); m2( ); System.out.println("Leaving main"); } } 1-11

  12. Execution Stack fora Typical Calling Sequence Activation record for m3 Activation record for m2 Activation record for m2 Activation record for main Activation record for main Activation record for main 1-12 main calls m2 m2 calls m3

  13. Execution Stack fora Typical Calling Sequence Activation record for m4 Activation record for m2 Activation record for m2 Activation record for m2 Activation record for main Activation record for main Activation record for main 1-13 Return from m3 m2 calls m4 Return from m4

  14. Execution Stack for a Typical Calling Sequence When the main method is invoked: An activation record for main is created and pushed onto the execution stack When main calls the method m2: An activation record for m2 is created and pushed onto the execution stack When m2 calls m3: An activation record for m3 is created and pushed onto the execution stack When m3 terminates, its activation record is popped off and control returns to m2 1-14

  15. Execution Stack for a Typical Calling Sequence When m2 next calls m4: What happens next? What happens when m4 terminates? What happens when m2 terminates? What happens when main terminates? Its activation record is popped off and control returns to the operating system 1-15

  16. Activation Records We will now look at some examples of what is in the activation record for a method First for simple variables Then for reference variables 1-16

  17. Example: Activation Records- Simple Variables public class CallFrameDemo1 { public static double square(double n){ double temp; temp = n * n; return temp; } } public static void main(String args[ ]) { double x = 4.5; double y; y = square(x); System.out.println("Square of " + x + " is " + y); } 1-17

  18. Activation Records Example 1 Draw a picture of the activation records on the execution stack: What will be in the activation record for the main method? Address to return to in operating system Variable args Variable x Variable y What will be in the activation record for the method square? Address to return to in main Variable n Variable temp Return value 1-18

  19. Discussion There will be an activation record on the execution stack foreach method called. So what other activation record(s) will be pushed onto the execution stack for our example? Which activation records will be on the execution stack at the same time? 1-19

  20. Heap Static space: contains one copy of the code of each class used in the program also contains static objects Dynamic or Object space: Information that is stored for each object: values of its instance variables reference to its code 1-20

  21. Static heap Dynamic heap Code Objects Activation Records Static objects Execution Stack, Call Stack, or Runtime Stack Heap

  22. Object Creation Memory is allocated in the heap area when an object is created using the operator new Reference variables are allocated memory in the activation records in the execution stack The objects are allocated memory in the heap 1-22

  23. public class CallFrameDemo2 { private static void printAll(String s1, String s2, String s3){ System.out.println(s1.toString( )); System.out.println(s2.toString( )); System.out.println(s3.toString( )); } public static void main(String args[ ]) { String str1, str2, str3; str1 = new String( string 1 ); str2 = new String( string 2 ); str3 = new String( string 3 ); } } printAll(str1, str2, str3); 1-23

  24. Activation Records Example 2 Draw a picture of the execution stack and of the heap as the above program executes: Activation record for main Activation record for String constructor for str1 then popped off Activation record for String constructor for str2 then popped off Activation record for String constructor for str3 then popped off Activation record for printAll Activation record for toString for str1 then popped off Activation record for System.out.println then popped off etc. 1-24

  25. Activation Records Example 2 What will be stored in the activation record for main? Address to return to in operating system Variable args Variable str1 Initial value? Value after return from String constructor? Variable str2 Variable str3 What will be in the activation record for printAll? 1-25

  26. Memory Deallocation What happens when a method returns? On the execution stack: The activation record is popped off when the method returns So, that memory is deallocated 1-26

  27. Memory Deallocation What happens to objects on the heap? An object stays in the heap even if there is no longer a variable referencing it! So, Java has automatic garbage collection It regularly identifies objects which no longer have a variable referencing them, and deallocates that memory 1-27

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#