Memory Leak In java

MindfireSolutions 1,579 views 14 slides Nov 25, 2013
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Talks about topics like What is Memory Leak, Symptoms of memory leak, Detecting memory leak & Preventing memory leak etc.


Slide Content

Memory Leaks in Java
By Tanmayee Sahoo

Presentation Outline

Introduction

Symptoms of memory leak

Detecting memory leak

Preventing memory leak

Question and Answer
1

Introduction
In procedural language like C, before a developer
uses a variable he/she has to manually allocate a
region in the memory where the value will reside and
once the application finishes using that value, the
region of the memory must be manually freed by
writing the code for this. But in java when a
developer creates an object using the key word new
JVM automatically allocates the memory for that
object.
2

Introduction (cont…)
During the life of the application JVM keeps on check
which objects in memory are in use and which are not
at a regular interval. Unused objects can be freed and
memory occupied by the object can be reclaimed and
reused. This process is called garbage collection and
the corresponding piece of JVM is called garbage
collector. Memory Leak in java is a situation when
some of the objects are not used by the application
3

Introduction (cont…)
any more but GC failed to recognize them as unused
there by the unused objects remain in the memory
indefinitely and the amount of memory available to
the application is reduced.
4

Symptoms of memory leak
When an application has memory leak we will notice
the following things.
 Memory usage consistently increases during the
application life span. Sooner or later the application
will crash because of out of memory.
 Performance consistently decreases.
5

Detecting memory leak
6
Several tools are available to detect
memory leak in a java application such
as:
 Yourkit
 Jprofiler
 Visual VM
 AppPerfect
 JProbe

Preventing memory leak
While writing code if we can take care of a few
points we can avoid memory leak.
 Avoid String Concatenation. Use StringBuilder.
 Avoid unnecessary object creation.
7

Preventing memory leak (Cont…)
 Static Objects : By default live for the entire life of
the application unless explicitly set to null. So it is
better to set the references to null once the use the
static member is over.
 Avoid Using System.gc()
8

Preventing memory leak (Cont…)
Close ResultSet, Statement and Connection objects
in finally block.
 Unregister Event listener class.
Disable Jsp Page buffer if you are not using jsp
feature that needs buffering so that performance will
improve as memory will not be used in creating
buffer and output will go directly to the browser.
9

Preventing memory leak (Cont…)
11
 Set session = false in the jsp page if the page is not
accessing the data from the session.
 Avoid storing huge data in the session.

Invalidate the session when no longer used.
 Avoid duplicating libraries. For ex. If a library is
shared among multiple modules in the same
application then use WAR file manifest's
CLASSPATH to share the libraries instead of

Preventing memory leak (Cont…)
Duplicating the library in the WEB-INF/lib of
each module.
 Set session timeout appropriately.
12

Question and Answer
13
??

Thank You
14