SlideShare a Scribd company logo
1 of 50
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 from
the OS and size of this space can not be changed during the
program 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 per
thread
Permanent Generation
1. Per-class data
2. Interned Strings
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çı
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çı
STACK AREA
JAVATHLON.COM BY TALHA OCAKÇI
MOTIVATION
javathlon.com by Talha Ocakçı
We will try to
swap the
customers and
see if they were
really swapped.
WHAT IS STACK?
Element 2
Element 1
Element 1
Element 2
Element 1
Element 1
JAVATHLON.COM BY TALHA OCAKÇI
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
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 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.
STACK FRAME
Stored
information
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 run
this code?
A) Prints 5 infinitely
B) Exhausts all the memory of
JVM
C) Throws an exception
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.
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.
EXAMPLE
javathlon.com by Talha Ocakçı
We will try to
swap the
customers and
See if they were
really swapped.
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;
}
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
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
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
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
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
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.
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
1
Talha
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
1
Talha
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
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
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
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 special
case of the String class and string interning
idea.
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 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çı
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 == s2
6000 == 6000
true
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

More Related Content

What's hot

JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and ArraysWebStackAcademy
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsAnton Keks
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
SQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONSQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONArun Sial
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.David Gómez García
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
exception handling in java.ppt
exception handling in java.pptexception handling in java.ppt
exception handling in java.pptVarshini62
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
JAVA Programming notes.ppt
JAVA Programming notes.pptJAVA Programming notes.ppt
JAVA Programming notes.pptAravindSiva19
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-PresentationChuck Walker
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design PatternShahriar Hyder
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data TypesTareq Hasan
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
How Hashmap works internally in java
How Hashmap works internally  in javaHow Hashmap works internally  in java
How Hashmap works internally in javaRamakrishna Joshi
 

What's hot (20)

JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Java constructors
Java constructorsJava constructors
Java constructors
 
SQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONSQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTION
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
exception handling in java.ppt
exception handling in java.pptexception handling in java.ppt
exception handling in java.ppt
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
JAVA Programming notes.ppt
JAVA Programming notes.pptJAVA Programming notes.ppt
JAVA Programming notes.ppt
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
How Hashmap works internally in java
How Hashmap works internally  in javaHow Hashmap works internally  in java
How Hashmap works internally in java
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 

Viewers also liked

Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesDoris Chen
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)Talha Ocakçı
 
Linked list (java platform se 8 )
Linked list (java platform se 8 )Linked list (java platform se 8 )
Linked list (java platform se 8 )charan kumar
 
Java Stack Traces
Java Stack TracesJava Stack Traces
Java Stack Tracesdbanttari
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part IIIHari Christian
 
Java Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video TutorialJava Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video TutorialMarcus Biel
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)Prof. Erwin Globio
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8Talha Ocakçı
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectOUM SAOKOSAL
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 

Viewers also liked (20)

Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
 
Java
JavaJava
Java
 
Linked list (java platform se 8 )
Linked list (java platform se 8 )Linked list (java platform se 8 )
Linked list (java platform se 8 )
 
Java Stack Traces
Java Stack TracesJava Stack Traces
Java Stack Traces
 
Java Stack (Pilha)
Java Stack (Pilha)Java Stack (Pilha)
Java Stack (Pilha)
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Introduction to java and oop
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part III
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
01 introduction to oop and java
01 introduction to oop and java01 introduction to oop and java
01 introduction to oop and java
 
Java Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video TutorialJava Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video Tutorial
 
Stack, queue and hashing
Stack, queue and hashingStack, queue and hashing
Stack, queue and hashing
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Oop java
Oop javaOop java
Oop java
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
 
OOP java
OOP javaOOP java
OOP java
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 

Similar to Heap and stack space in java

Introduction to apache_cassandra_for_develope
Introduction to apache_cassandra_for_developeIntroduction to apache_cassandra_for_develope
Introduction to apache_cassandra_for_developezznate
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performanceRoger Xia
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in JavaMudit Gupta
 
How to debug slow lambda response times
How to debug slow lambda response timesHow to debug slow lambda response times
How to debug slow lambda response timesYan Cui
 
Java interview-questions-and-answers
Java interview-questions-and-answersJava interview-questions-and-answers
Java interview-questions-and-answersbestonlinetrainers
 
weblogic perfomence tuning
weblogic perfomence tuningweblogic perfomence tuning
weblogic perfomence tuningprathap kumar
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 
Владимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpotВладимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpotVolha Banadyseva
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunningguest1f2740
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance TunningTerry Cho
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2Sherihan Anver
 
Java 8 lambdas expressions
Java 8 lambdas expressionsJava 8 lambdas expressions
Java 8 lambdas expressionsLars Lemos
 

Similar to Heap and stack space in java (20)

Java 8
Java 8Java 8
Java 8
 
Introduction to apache_cassandra_for_develope
Introduction to apache_cassandra_for_developeIntroduction to apache_cassandra_for_develope
Introduction to apache_cassandra_for_develope
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java 7 & 8 - A&BP CC
Java 7 & 8 - A&BP CCJava 7 & 8 - A&BP CC
Java 7 & 8 - A&BP CC
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performance
 
OpenMP And C++
OpenMP And C++OpenMP And C++
OpenMP And C++
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
How to debug slow lambda response times
How to debug slow lambda response timesHow to debug slow lambda response times
How to debug slow lambda response times
 
Java String
Java String Java String
Java String
 
Java interview-questions-and-answers
Java interview-questions-and-answersJava interview-questions-and-answers
Java interview-questions-and-answers
 
weblogic perfomence tuning
weblogic perfomence tuningweblogic perfomence tuning
weblogic perfomence tuning
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Владимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpotВладимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpot
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunning
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunning
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
Md04 flow control
Md04 flow controlMd04 flow control
Md04 flow control
 
Java 8 lambdas expressions
Java 8 lambdas expressionsJava 8 lambdas expressions
Java 8 lambdas expressions
 

Recently uploaded

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 

Recently uploaded (20)

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 

Heap and stack space in java

  • 1. MEMORY MANAGEMENT in JAVA STACK SPACE, HEAP SPACE AND STRING POOLS JAVATHLON.COM BY TALHA OCAKÇI
  • 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çı
  • 9. MOTIVATION javathlon.com by Talha Ocakçı We will try to swap the customers and see if they were really swapped.
  • 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.
  • 15. STACK FRAME Stored information Value Arguments String Local object (customer) 0x65899 Local primitive (age) 30 Return value True … JAVATHLON.COM BY TALHA OCAKÇI
  • 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.
  • 20. EXAMPLE javathlon.com by Talha Ocakçı We will try to swap the customers and See if they were really swapped.
  • 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.
  • 41. Second example javathlon.com by Talha Ocakçı What is the output of this code snippet?
  • 42. Second example javathlon.com by Talha Ocakçı Answer is «false»
  • 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