memory leak in java

14
Memory Leaks in Java By Tanmayee Sahoo

Upload: mindfire-solutions

Post on 05-Dec-2014

568 views

Category:

Technology


3 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Memory Leak In java

Memory Leaks in JavaBy Tanmayee Sahoo

Page 2: Memory Leak In java

Presentation Outline

Introduction Symptoms of memory leak Detecting memory leak Preventing memory leak Question and Answer

1

Page 3: Memory Leak In java

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

Page 4: Memory Leak In java

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

Page 5: Memory Leak In java

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

Page 6: Memory Leak In java

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

Page 7: Memory Leak In java

Detecting memory leak

6

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

Page 8: Memory Leak In java

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

Page 9: Memory Leak In java

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

Page 10: Memory Leak In java

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

Page 11: Memory Leak In java

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

Page 12: Memory Leak In java

Preventing memory leak (Cont…)

Duplicating the library in the WEB-INF/lib of each module. Set session timeout appropriately.

12

Page 13: Memory Leak In java

Question and Answer

13

??

Page 14: Memory Leak In java

Thank You

14