2. MOTIVATION
javathlon.com by Talha Ocakçı
What happens when you run this code:
A) Operating system fails. Because all memory is exhausted by our program.
B) All running programs lose data since all memory is exhausted by our program.
C) Operating system does not allow program execution by detecting the memory consumption.
D) Program halts and throw an error.
3. JVM MEMORY
javathlon.com by Talha Ocakçı
OPERATING
SYSTEM
MEMORY
JVM MEMORY
(CONSTANT)
When main method is executed, JVM asks for some memory from
the OS and size of this space can not be changed during the
program execution.
4. JVM MEMORY
Every object is stored in JVM memory and can be
accessed by its address value.
A Java application can not interfere with a
memory block outside the JVM.
JAVATHLON.COM BY TALHA OCAKÇI
5. Memory Management
javathlon.com by Talha Ocakçı
HEAP AREA STACK AREA NON-HEAP AREA
Class instances and arrays Stacks for executing methods per
thread
Permanent Generation
1. Per-class data
2. Interned Strings
6. HEAP AREA
▪ Objects (excluding interned strings) created in any
thread are stored in this area. So this is a common
place for all threads.
▪ Interned strings stored in non-heap area
separately.
javathlon.com by Talha Ocakçı
7. HEAP AREA
▪ If you create an object with new() keyword, it will
be stored in heap area.
▪If no reference left for a given object, it will be a
candidate for garbage collection.
javathlon.com by Talha Ocakçı
10. WHAT IS STACK?
Element 2
Element 1
Element 1
Element 2
Element 1
Element 1
JAVATHLON.COM BY TALHA OCAKÇI
11. HOW IS METHOD ORDER
PRESERVED?
When a method
is called,
method-specific
data is collected,
tied together and
put into stack
space. It is
removed when
the execution is
done.
JAVATHLON.COM BY TALHA OCAKÇI
13. STACK SPACE
JVM uses this space for controlling the execution of
methods.
Stores local values of ongoing methods.
Stores signatures of ongoing methods.
JAVATHLON.COM BY TALHA OCAKÇI
14. STACK AREA
javathlon.com by Talha Ocakçı
Stack for
Thread 1
Stack
for
method
A
Stack for
Thread 2
Stack frame per method
Stack frame per method
Stack frame per method
Stack frame per method
Stack area
For each thread, a stack is stored.
Each stack stores a stack frame per
method call. Stack frame stores return
type, arguments and address values of
local objects and values of local
primitive values.
16. STACK FRAMES
Located in a stack allocated to a thread in stack area. Stores
1. Address values of local objects
2. Values of local primitives
3. Return value of the method
4. Current class’s definition’s address value
5. Operands stack
javathlon.com by Talha Ocakçı
17. MOTIVATION
javathlon.com by Talha Ocakçı
What happens when you run
this code?
A) Prints 5 infinitely
B) Exhausts all the memory of
JVM
C) Throws an exception
18. MOTIVATION
javathlon.com by Talha Ocakçı
Stack space is limited. Here,
without popping a stack frame from
the stack, a new one is pushed. So
allocated memory for method
information grows rapidly.
JVM throws a
StackOverflowException and halts
the execution.
19. PASSING OBJECTS in JAVA
javathlon.com by Talha Ocakçı
In Java, objects are passed between
methods by their values.
1. Objects are passed by values of their
address on heap space
2. Primitives are passed by values
3. Strings are also passed by values of
their value. This value is checked against
the interned strings in non-heap area.
21. javathlon.com by Talha Ocakçı
...
HEAP SPACE
1
Talha
Turkey
2
John
US
customer1 1000
customer2 2000
1000
2000
Stack frame for main method
public void swapCustomers(Customer cust1, Customer cust2) {
Customer temp = cust2;
cust2 = cust1;
cust1 = temp;
}
22. javathlon.com by Talha Ocakçı
...
HEAP SPACE
1
Talha
Turkey
2
John
US
customer1 1000
customer2 2000
1000
2000
Stack frame for main method
public void swapCustomers(Customer cust1, Customer cust2) {
Customer temp = cust2;
cust2 = cust1;
cust1 = temp;
}
cust1 1000
cust2 2000
Stack frame for swapCustomers method
23. javathlon.com by Talha Ocakçı
...
HEAP SPACE
1
Talha
Turkey
2
John
US
customer1 1000
customer2 2000
1000
2000
Stack frame for main method
public void swapCustomers(Customer cust1, Customer cust2) {
Customer temp = cust2;
cust2 = cust1;
cust1 = temp;
}
temp 2000
cust1 1000
cust2 2000
Stack frame for swapCustomers method
24. javathlon.com by Talha Ocakçı
...
HEAP SPACE
1
Talha
Turkey
2
John
US
customer1 1000
customer2 2000
1000
2000
Stack frame for main method
public void swapCustomers(Customer cust1, Customer cust2) {
Customer temp = cust2;
cust2 = cust1;
cust1 = temp;
}
temp 2000
cust1 1000
cust2 1000
Stack frame for swapCustomers method
25. javathlon.com by Talha Ocakçı
...
HEAP
SPACE
1
Talha
Turkey
2
John
US
customer1 1000
customer2 2000
1000
2000
Stack frame for main method
public void swapCustomers(Customer cust1, Customer cust2) {
Customer temp = cust2;
cust2 = cust1;
cust1 = temp;
}
temp 2000
cust1 2000
cust2 1000
Stack frame for swapCustomers method
26. javathlon.com by Talha Ocakçı
...
HEAP
SPACE
1
Talha
Turkey
2
John
US
customer1 1000
customer2 2000
1000
2000
Stack frame for main method
public void swapCustomers(Customer cust1, Customer cust2) {
Customer temp = cust2;
cust2 = cust1;
cust1 = temp;
}
temp 2000
cust1 2000
cust2 1000
Stack frame for swapCustomers method
27. javathlon.com by Talha Ocakçı
...
HEAP SPACE
1
Talha
Turkey
2
John
US
customer1 1000
customer2 2000
1000
2000
Stack frame for main method
public void swapCustomers(Customer cust1, Customer cust2) {
Customer temp = cust2;
cust2 = cust1;
cust1 = temp;
}
So nothing has been changed on
Customer1 and customer2 references.
28. SECOND EXAMPLE
javathlon.com by Talha Ocakçı
We will pass the
object to a method,
change some
attributes of it and
check whether the
original object has
been changed.
29. javathlon.com by Talha Ocakçı
...
HEAP
SPACE
1
Talha
Turkey
customer1 1000
1000
Stack frame for main method
public void changeCountry(Customer cust) {
cust.address = "new zealand";
}
30. javathlon.com by Talha Ocakçı
...
HEAP
SPACE
1
Talha
Turkey
customer1 1000
1000
Stack frame for main method
public void changeCountry(Customer cust) {
cust.address = "new zealand";
}
cust 1000
31. javathlon.com by Talha Ocakçı
...
HEAP
SPACE
1
Talha
new zealand
customer1 1000
1000
Stack frame for main method
public void changeCountry(Customer cust) {
cust.address = "new zealand";
}
cust 1000
Stack frame for changeCountry method
32. javathlon.com by Talha Ocakçı
...
HEAP
SPACE
1
Talha
new zealand
customer1 1000
1000
Stack frame for main method
public void changeCountry(Customer cust) {
cust.address = "new zealand";
}
So, the original object is really
modified on heap space
33. EXAMPLE 3
javathlon.com by Talha Ocakçı
We will send a
primitive to a
method,
multiply the
argument value and
will check
Whether te original
value has been
changed.
34. javathlon.com by Talha Ocakçı
...
HEAP
SPACE
x 10
Stack frame for main method
public void doubleTheValue(int value)
{
value = value *2;
}
Since there is no object defined,
no object exists for this program
value 10
Stack frame for doubleTheValue method
35. javathlon.com by Talha Ocakçı
...
HEAP
SPACE
x 10
Stack frame for main method
public void doubleTheValue(int value)
{
value = value *2;
}
Since there is no object defined,
no object exists for this program
value 20
Stack frame for doubleTheValue method
36. javathlon.com by Talha Ocakçı
...
HEAP
SPACE
x 10
Stack frame for main method
public void doubleTheValue(int value)
{
value = value *2;
}
Since there is no object defined,
no object exists for this program
37. Example 4
javathlon.com by Talha Ocakçı
Now we will try the same thing with
a String. We will demonstrate the special
case of the String class and string interning
idea.
38. Example 4
javathlon.com by Talha Ocakçı
This code will output «current».
Because updateValue method could not
change the value of String string variable
The reason is string interning also
known as «string pooling».
39. Lets start with an example
javathlon.com by Talha Ocakçı
What is the output of this code snippet?
40. Lets start with an example
javathlon.com by Talha Ocakçı
If you answered this as «true», you
probably know what is going on.
43. Third example
javathlon.com by Talha Ocakçı
Now, let’s try it with string interning.
Output is true.
Now lets inspect what is going on.
44. String interning
One of the most used method in an ordinary program is string comparison. And in an average
program, there are so many strings which has an average length between 10-20.
So, to compare two strings, JVM must compare that much character everytime causing a real
performance problem. That’s why, JVM puts a used String to a String pool and uses the
internal HashMap for comparing the string variables.
javathlon.com by Talha Ocakçı
45. javathlon.com by Talha Ocakçı
...
STRING POOL
s1 6000
s2 6000
Stack frame for main method
String s1 = "talha_ocakci";
talha_ocakci
6000
String s2 = "talha_ocakci";
46. javathlon.com by Talha Ocakçı
...
STRING POOL
s1 6000
s2 6000
Stack frame for main method
String s1 = "talha_ocakci";
talha_ocakci
6000
String s2 = "talha_ocakci";
s1 == s2
6000 == 6000
true
47. javathlon.com by Talha Ocakçı
STRING POOL
s3 9500
s4 9600
Stack frame for main method
String s3 = new String("talha_ocakci");
String s4 = new String("talha_ocakci");
...
talha_ocakci
6000
HEAP
...
talha_ocakci
9500
talha_ocakci
9600
48. javathlon.com by Talha Ocakçı
STRING POOL
s3 9500
s4 9600
Stack frame for main method
String s3 = new String("talha_ocakci");
s3 = s3.intern();
String s4 = new String("talha_ocakci");
s4= s4.intern();
...
talha_ocakci
6000
HEAP
...
talha_ocakci
9500
talha_ocakci
9600
49. javathlon.com by Talha Ocakçı
STRING POOL
s3 6000
s4 6000
Stack frame for main method
String s3 = new String("talha_ocakci");
s3 = s3.intern();
String s4 = new String("talha_ocakci");
s4= s4.intern();
...
talha_ocakci
6000
HEAP
...
talha_ocakci
9500
talha_ocakci
9600