Skip to main content

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?

Popular posts from this blog

What are Java heap Young, Old and Permanent Generations?

The Java heap is dived into two parts i.e. Young generation and old generation. Young generationYoung generation  memory is 40% of Maximum Java Heap. It consists of two parts, Eden space and Survivor Space. Eden SpaceInitially objects are created in this  Part of Java Heap , here most objects die and quickly are cleaned up by the minor Garbage Collectors (It only runs in young generation and its frequency is high in compared to major garbage collector). Usually any new objects created inside a Java Method go into Eden space and the objects space is reclaimed once the method execution completes. Whereas the Instance Variables of a Class usually lives longer until the Object based on that class gets destroyed. When Eden fills up it causes a minor collection, in which some surviving objects are moved to Survivor Space or an older generation. Survivor Space  The pool containing objects that have survived the garbage collection of the Eden space The parameter Survivor Ratio can be used to tune…

Compression of HttpServletRequest and HttpServletResponse by gzip encoding

Description 
This filter is on HTTP headers in a HttpServletRequest, compress data written to the HttpServletResponse, or decompress data read from the request. When supported by the client browser, this can potentially greatly reduce the number of bytes written across the network from and to the client. As a Filter, this class can also be easily added to any J2EE 1.3+ web application.





Installation

1).Add the pjl-comp-filter-XX.jar file containing CompressingFilter to your web application's WEB-INF/lib directory.

2).Add the following entries to your web.xml deployment descriptor:

 <filter>
  <filter-name>CompressingFilter</filter-name>
  <filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>CompressingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

The below data is of my application where i tested this compression technique…

4 mains causes of Long Garbage Collection Pause?

So a long GC Pause can have a direct impact on response time and throughput. But there are some causes for long GC pause which you can check and avoid to increase performance. They are as follows:-


1)Insufficient heap size:- If heap size is small then the size required then the allocation requests for new objects  fail and the JVM needs to invoke garbage collections in an attempt to reclaim space for the allocations. So these frequent Full GCs cause long pauses in the application run. To avoid long GC pause by this identify the average footprint of the application and then specify the heap size accordingly.
2)Fragmentation in the heap:-GCs can occur more frequently due to fragmentation in the Java Heap  and also sometimes causing long pauses in the GCs. This is more probable in the case of Concurrent Mark Sweep collector, also known as CMS, where the tenured generation space is not compacted with the concurrent collections. For more please go through the previous post about heap  compact…