heap and stack space in java

50
MEMORY MANAGEMENT in JAVA STACK SPACE, HEAP SPACE AND STRING POOLS JAVATHLON.COM BY TALHA OCAKÇI

Upload: talha-ocakci

Post on 16-Jul-2015

167 views

Category:

Engineering


6 download

TRANSCRIPT

Page 1: Heap and stack space in java

MEMORY MANAGEMENT in JAVA

STACK SPACE, HEAP SPACE AND STRING POOLS

JAVATHLON.COM BY TALHA OCAKÇI

Page 2: Heap and stack space in java

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.

Page 3: Heap and stack space in java

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.

Page 4: Heap and stack space in java

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

Page 5: Heap and stack space in java

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

Page 6: Heap and stack space in java

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çı

Page 7: Heap and stack space in java

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çı

Page 8: Heap and stack space in java

STACK AREA

JAVATHLON.COM BY TALHA OCAKÇI

Page 9: Heap and stack space in java

MOTIVATION

javathlon.com by Talha Ocakçı

We will try toswap thecustomers andsee if they werereally swapped.

Page 10: Heap and stack space in java

WHAT IS STACK?

Element 2

Element 1Element 1

Element 2

Element 1

Element 1

JAVATHLON.COM BY TALHA OCAKÇI

Page 11: Heap and stack space in java

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

Page 12: Heap and stack space in java

secondMethod

firstMethod

Element 1

firstMethod

firstMethod

secondMethod

secondMethod

firstMethod

thirdMethod

secondMethod

firstMethod

firstMethod

thirdMethod

Stack frame

JAVATHLON.COM BY TALHA OCAKÇI

Page 13: Heap and stack space in java

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

Page 14: Heap and stack space in java

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.

Page 15: Heap and stack space in java

STACK FRAME

Storedinformation

Value

Arguments String

Local object(customer)

0x65899

Local primitive(age)

30

Return value True

JAVATHLON.COM BY TALHA OCAKÇI

Page 16: Heap and stack space in java

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çı

Page 17: Heap and stack space in java

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

Page 18: Heap and stack space in java

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.

Page 19: Heap and stack space in java

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.

Page 20: Heap and stack space in java

EXAMPLE

javathlon.com by Talha Ocakçı

We will try toswap thecustomers andSee if they werereally swapped.

Page 21: Heap and stack space in java

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;

}

Page 22: Heap and stack space in java

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

Page 23: Heap and stack space in java

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

Page 24: Heap and stack space in java

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

Page 25: Heap and stack space in java

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

Page 26: Heap and stack space in java

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

Page 27: Heap and stack space in java

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.

Page 28: Heap and stack space in java

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.

Page 29: Heap and stack space in java

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";

}

Page 30: Heap and stack space in java

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

Page 31: Heap and stack space in java

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

Page 32: Heap and stack space in java

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

Page 33: Heap and stack space in java

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.

Page 34: Heap and stack space in java

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

Page 35: Heap and stack space in java

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

Page 36: Heap and stack space in java

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

Page 37: Heap and stack space in java

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.

Page 38: Heap and stack space in java

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».

Page 39: Heap and stack space in java

Lets start with an example

javathlon.com by Talha Ocakçı

What is the output of this code snippet?

Page 40: Heap and stack space in java

Lets start with an example

javathlon.com by Talha Ocakçı

If you answered this as «true», you probably know what is going on.

Page 41: Heap and stack space in java

Second example

javathlon.com by Talha Ocakçı

What is the output of this code snippet?

Page 42: Heap and stack space in java

Second example

javathlon.com by Talha Ocakçı

Answer is «false»

Page 43: Heap and stack space in java

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.

Page 44: Heap and stack space in java

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çı

Page 45: Heap and stack space in java

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";

Page 46: Heap and stack space in java

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

Page 47: Heap and stack space in java

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

Page 48: Heap and stack space in java

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

Page 49: Heap and stack space in java

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

Page 50: Heap and stack space in java

www.javathlon.com

By Talha OCAKÇI

JAVATHLON.COM BY TALHA OCAKÇI