Navigation

Wednesday, 12 February 2014

Example of Garbage collection .

This post is dedicated to explain how Garbage collection works and what can cause memory leaks.
Here we will manually run a small program and will check how memory is allocated to objects and how they it
is collected back when they needed no more.

Lets create a Valueobject which we will access from a class .
below is a Employee Vo which contains name and id of a employees

Lets create a Test class to accesses this Vo .

The above class contains a method m1 and a main method.

When JVM starts it creates to portions to store data
1) heap to keep objects 
2) stack to keep primitive data types and references to heap.Each method will have there own heap

So when we will run the above Test class the memory snap at line number 4 will be

Heap
===================
Empty


Stack
=============
main method
---------------
args:null


so initially heap will be empty and stack will contain the args passed to main method.
Every method has its own stack.
At line number 5 it will store the value of val in stack . it will be in stack because val is of primitive data type.
now memory snap will.

Heap
===================



Stack
=============
main method
---------------
args:null
val:10


On next line i.e. line number 6 it will create a object of Employee and 
will store in heap and will maintain its reference in stack
So it will look like 

Heap
===================
1000X:EmployeeObj name=2000X , id=2
2000X:StringObj "Ravi"


Stack
=============
main method
---------------
args : null
val : 10
e : 1000X

Here 1000X and 2000X are memory addresses and since "Ravi"
is a object of String class it will be stored at different address


On next line number 7 it is calling a method m1 so a new stack will be created in a
stack and will hold the passed parameter i.e. object e

Heap
===================
1000X:EmployeeObj name=2000X , id=2
2000X:StringObj "Ravi"


Stack
=============

main method
---------------
args : null
val : 10
e : 1000X

m1 method
--------------------
e : 1000X----passed as reference 


Now in next line 12 it has changed the value of id from 2 to 5 
so it will be like

Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"


Stack
=============
main method
---------------
args : null
val : 10
e : 1000X

m1 method
--------------------
e : 1000X


The reference of e will be the same 
At next line number 13 its creating a new object.
Remember When we use New key word always a new object is created
So the heap stack position will be like.


Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
3000X:EmployeeObj name=4000X , id=7
4000X:StringObj "Gaurav"


Stack
=============

main method
---------------
args : null
val : 10
e : 1000X

m1 method
--------------------
e : 3000X


Now at line number 14 its id value will be changed to 9
so memory snap will

Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
3000X:EmployeeObj name=4000X , id=9
4000X:StringObj "Gaurav"


Stack
=============

main method
---------------
args : null
val : 10
e : 1000X

m1 method
--------------------
e : 3000X


At line 15 method will be finished and its stack will be removed

Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
3000X:EmployeeObj name=4000X , id=9
4000X:StringObj "Gaurav"


Stack
=============

main method
---------------
args : null
val : 10
e : 1000X

So finaly the value of the id at line number (8) will 5
Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
3000X:EmployeeObj name=4000X , id=9
4000X:StringObj "Gaurav"

Stack
=============
main method
---------------
args : null
val : 10
e : 1000X


Now let at this point garbage collector runs
So first it will mark all the live object which can be referred from stack.
and will remove all other objects from memory

Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
3000X:EmployeeObj name=4000X , id=9
4000X:StringObj "Gaurav"

Stack
=============
main method
---------------
args : null
val : 10
e : 1000X


After running GC

Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
4000X:StringObj "Gaurav" not be collected 

Stack
=============
main method
---------------
args : null
val : 10
e : 1000X


String objects will not be removed since they are sorted in separate memory called String pool.

You may also like:-

Using JConsole in java And creating memory dump by it

Heap analysis by Memory Analyzer (MAT) - Eclipse java

Main causes of Out-Of-Memory Errors in java

Five main types of Memory leaks in java

How to Reduce Garbage-Collection Pause Time?

4 Mains causes of Long Garbage Collection Pause?


How Application Performance get impacted by Garbage Collection?


Java Performance Tuning options Java heap


Why Memory leak in Java ?


What is a Memory Leak in java?


What is Garbage collector in java and how it works?


What is Garbage Collector Compaction in Java?


What are Java heap Young, Old and Permanent Generations?


What is difference between Web Server and Application Server ?


What is the maximum ram for 32 bits and 64 bits computer?


Some Amazing fact about memory (petabyte,exabyte,zettabyte) And How much data exists on the Web?