MEMORY MANAGEMENT in JAVA
STACK SPACE, HEAP SPACE AND STRING POOLS
JAVATHLON.COM BY TALHA OCAKÇI
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.
JVM MEMORY
javathlon.com by Talha Ocakçı
OPERATING
SYSTEM
MEMORY
JVM MEMORY
(CONSTANT)
When main method is executed, JVM asks for some memory fromthe OS and size of this space can not be changed during theprogram execution.
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
Memory Management
javathlon.com by Talha Ocakçı
HEAP AREA STACK AREA NON-HEAP AREA
Class instances and arrays Stacks for executing methods perthread
Permanent Generation1. Per-class data 2. Interned Strings
HEAP AREA
▪ Objects (excluding interned strings) created in anythread are stored in this area. So this is a commonplace for all threads.
▪ Interned strings stored in non-heap areaseparately.
javathlon.com by Talha Ocakçı
HEAP AREA
▪ If you create an object with new() keyword, it willbe 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çı
STACK AREA
JAVATHLON.COM BY TALHA OCAKÇI
MOTIVATION
javathlon.com by Talha Ocakçı
We will try toswap thecustomers andsee if they werereally swapped.
WHAT IS STACK?
Element 2
Element 1Element 1
Element 2
Element 1
Element 1
JAVATHLON.COM BY TALHA OCAKÇI
HOW IS METHOD ORDER PRESERVED?
When a methodis called, method-specificdata is collected, tied together andput into stackspace. It is removed whenthe execution is done.
JAVATHLON.COM BY TALHA OCAKÇI
secondMethod
firstMethod
Element 1
firstMethod
firstMethod
secondMethod
secondMethod
firstMethod
thirdMethod
secondMethod
firstMethod
firstMethod
thirdMethod
Stack frame
JAVATHLON.COM BY TALHA OCAKÇI
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
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 areaFor each thread, a stack is stored.
Each stack stores a stack frame permethod call. Stack frame stores returntype, arguments and address values of local objects and values of localprimitive values.
STACK FRAME
Storedinformation
Value
Arguments String
Local object(customer)
0x65899
Local primitive(age)
30
Return value True
…
JAVATHLON.COM BY TALHA OCAKÇI
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çı
MOTIVATION
javathlon.com by Talha Ocakçı
What happens when you runthis code?A) Prints 5 infinitelyB) Exhausts all the memory of JVMC) Throws an exception
MOTIVATION
javathlon.com by Talha Ocakçı
Stack space is limited. Here, without popping a stack frame fromthe stack, a new one is pushed. Soallocated memory for methodinformation grows rapidly.
JVM throws a StackOverflowException and haltsthe execution.
PASSING OBJECTS in JAVA
javathlon.com by Talha Ocakçı
In Java, objects are passed betweenmethods by their values.
1. Objects are passed by values of theiraddress on heap space
2. Primitives are passed by values
3. Strings are also passed by values of their value. This value is checked againstthe interned strings in non-heap area.
EXAMPLE
javathlon.com by Talha Ocakçı
We will try toswap thecustomers andSee if they werereally swapped.
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
Turkey
2JohnUS
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;
}
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
Turkey
2JohnUS
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
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
Turkey
2JohnUS
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
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
Turkey
2JohnUS
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
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
Turkey
2JohnUS
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
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
Turkey
2JohnUS
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
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
Turkey
2JohnUS
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 onCustomer1 and customer2 references.
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.
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
Turkey
customer1 1000
1000
Stack frame for main method
public void changeCountry(Customer cust) {cust.address = "new zealand";
}
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
Turkey
customer1 1000
1000
Stack frame for main method
public void changeCountry(Customer cust) {cust.address = "new zealand";
}
cust 1000
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
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
javathlon.com by Talha Ocakçı
...
HEAP SPACE
1Talha
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
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.
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
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
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
Example 4
javathlon.com by Talha Ocakçı
Now we will try the same thing with a String. We will demonstrate the specialcase of the String class and string interningidea.
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».
Lets start with an example
javathlon.com by Talha Ocakçı
What is the output of this code snippet?
Lets start with an example
javathlon.com by Talha Ocakçı
If you answered this as «true», you probably know what is going on.
Second example
javathlon.com by Talha Ocakçı
What is the output of this code snippet?
Second example
javathlon.com by Talha Ocakçı
Answer is «false»
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.
String interningOne 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çı
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";
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 == s26000 == 6000true
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
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
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
www.javathlon.com
By Talha OCAKÇI
JAVATHLON.COM BY TALHA OCAKÇI