Top Banner
Memory Leaks in Java By:- Ashish Bhasin [email protected]
40

TechGIG_Memory leaks in_java_webnair_26th_july_2012

Sep 11, 2014

Download

Technology

This presentation was presented on techGig 26th July 2012. Talks how we can catch Memory leaks
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Memory Leaks in Java

By:- Ashish Bhasin

[email protected]

Page 2: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Agenda

• Introduction

• Resolving Memory Leaks

Page 3: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Then why are we

here??? Memory Management

is done by Java

Introduction

Page 4: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Memory Manager – Garbage collector (GC)

• A key feature of memory management in

Java is its garbage-collected heap

– Garbage collector (GC) that comes with

Java is a tracing collector, which determines

which objects should be preserved in

memory by tracing all objects reachable

from a set of roots.

– These reachable objects survive collections

because they may be used in the course of

program execution.

Page 5: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Memory Manager – Garbage collector (GC)

Page 6: TechGIG_Memory leaks in_java_webnair_26th_july_2012

What is Memory Leak in Java ?

Note :-

When no more memory is remaining, an OutOfMemoryError alert will be

thrown and generate an exception like this:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

at

Page 7: TechGIG_Memory leaks in_java_webnair_26th_july_2012

7

What is memory leak in java?

(Memory Map)

Java Memory

leak

Allocated

Reachable

Live

Handled by

JVM

Page 8: TechGIG_Memory leaks in_java_webnair_26th_july_2012

8

Why Memory Leak is BAD…

• Memory-related issues affect

execution speed and reliability of

application.

Page 9: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Causes of memory leaks in Java • The four typical causes of memory leaks in a Java

program are: – Unknown or unwanted object references: These objects are no longer

needed, but the garbage collector can not reclaim the memory

because another object still refers to it.

– Long-living (static) objects: These objects stay in the memory for the

application's full lifetime. Objects tagged to the session may also have

the same lifetime as the session, which is created per user and

remains until the user logs out of the application.

– Failure to clean up or free native system resources: Native system

resources are resources allocated by a function external to Java,

typically native code written in C or C++. Java Native Interface (JNI)

APIs are used to embed native libraries/code into Java code.

– Bugs in the JDK or third-party libraries: Bugs in various versions of the

JDK or in the Abstract Window Toolkit and Swing packages can

cause memory leaks.

Page 10: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Types of Memory Leaks in Objects

Lapsed Listener Object added to collection, not removed

e.g. event listener, observer

collection size can grow without bound

iteration over “dead” objects degrades

performance

Lingerer Reference used transiently by

long-term object

Reference always reset on next use

e.g. associations with menu items

e.g. global action or service

Limbo Reference pinned by long-running thread

references on stack

GC doesn’t do local liveness analysis

Page 11: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Lapsed Listener – An Example (1/2)

• Listeners are commonly used to specify event

handlers for events that occur to the object.

Some commonly used event types are:

– GUI events: listener classes implementing

java.util.EventListener interface for GUI events in

AWT, Swing, and other GUI libraries.

– Database connection events; listeners implement

ConnectionEventListener interface in javax.sql.

Note :-

• The listener pattern, a specific class of the observer pattern, consists of a subject and an observer.

• The listener pattern is widespread, it is error-prone and may cause memory leaks if not used properly.

Page 12: TechGIG_Memory leaks in_java_webnair_26th_july_2012

• Listener registration:

object.addMyListener(n

ewMyListener(...));

• unregistering the

listener and allowing

object to be garbage-

collected.object.remov

eMyListener(l);

A typical large-scale application with a GUI and a database back end may use

dozens of different types of listeners, all of which have to be unregistered to avoid

memory leaks.

Lapsed Listener – An Example (2/2)

Page 13: TechGIG_Memory leaks in_java_webnair_26th_july_2012

We will see how

to catch Memory

leaks

Oops, Java has

memory leaks

Resolving Memory

Leaks

Page 14: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Resolving Memory Leaks

• What to look in your program

– Is My Program actually leaking Memory

– Monitoring Process

– Dump the heap

– Analyze the heap

Page 15: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Note

• FIG 1 :- The graph below shows the memory usage in a healthy Java application that does not suffer from memory leaks, with the peak load occurring around 10 AM and application usage drastically decreasing at 5 PM. Naturally, the peak load on business applications often correlates with normal business hours.

• FIG 2 :- let's suppose that we have a memory leak in the application. The primary characteristic of memory leaks is that memory requirements increase as a function of time, not as a function of the load. Let's see how the application would look after running for a few days with a memory leak and the same peak user loads reached around 10 AM every day:

• There is one special case that should be noted here: a program that needs to be restarted periodically in order to prevent it from crashing with an OutOfMemoryError alert.

Is My Program Leaking Memory?

Page 16: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Monitoring Memory Leaks

- Verbose GC log

Monitoring using GC which shows that we might

have leak in the program

Page 17: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Monitoring Java Process

Page 18: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Dump the Heap

• Note

– Before you dump heap, be sure to keep the following issues in

mind: • Programs in the JVM should be paused for the duration of the heap dump,

• Heap dumps are saved on disk, and the files might be fairly large.

Page 19: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Tools to Analyze Heap Dump

Fast leak

Slow leak

Page 20: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Analyzing the Heap Dump for Fast Memory

Leaks

• Analyzing the heap dump is done in order to:

– Find objects that are "leaking"

– Find the root cause of the memory leak

Page 21: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Slow and Small Memory Leaks

Page 22: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Profiling

Page 23: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Profiling

• What is Profiling?

• What Profiling Tells You

• What Profiling Is Not

• Manual Profiling

• Profiling Techniques Overview

– Insertion

– Sampling

– Instrumented VM

Page 24: TechGIG_Memory leaks in_java_webnair_26th_july_2012

What is Profiling?

• Profiling is measuring an application,

specifically:

– where is the time being spent?

• This is “classical” profiling

• which method takes the most time?

• which method is called the most?

– how is memory being used?

• what kind of objects are being created?

• this in especially applicable in OO, GC’ed

environments

Page 25: TechGIG_Memory leaks in_java_webnair_26th_july_2012

What Profiling Tells You

• Basic information:

– How much time is spent in each

method? (“flat” profiling)

– How many objects of each type are

allocated?

Page 26: TechGIG_Memory leaks in_java_webnair_26th_july_2012

What Profiling Tells You

• Beyond the basics:

– Program flow (“hierarchical” profiling)

• do calls to method A cause method B to take

too much time?

– Per-line information

• which line(s) in a given method are the most

expensive?

• Which methods created which objects?

– Visualization aspects

• Is it easy to use the profiler to get to the

information you’re interested in?

Page 27: TechGIG_Memory leaks in_java_webnair_26th_july_2012

What Profiling Is Not • Profiling is measuring performance

statistics on your particular

application

– regardless of the underlying platform

• Benchmarking is measuring the

performance of a particular platform

– not a specific application

• Optimization is an automatic

technique that applies generic

enhancements to speed up code

– regardless of the application or platform

Page 28: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Optimization • Even though optimization isn’t always

enough, it’s a good place to start

• Java is designed to be optimized by

the JVM, not by the compiler

– even though there are optimizing

compilers

• JIT compilers help

• Sun’s next-generation JVM, HotSpot,

offers:

– generational garbage collection

– improved, profiling native compiler

Page 29: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Manual Profiling

• You don’t absolutely need to have a

profiling tool to profile your code… you

could

– build your own profiling tool

• see Dr. Dobb’s Journal, March 1998

• Or Dr. Dobbs, Sept. 1999

– use very simple code-insertion techniques

• System.out.println(System.getCurrentTimeMillis());

Page 30: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Profiling Techniques Overview

• Commercial profilers use one of three

techniques for profiling programs:

– insertion

– sampling

– instrumented virtual machine

• Why is it important to understand how a

profiler works?

– each different technique has its own pros &

cons

– different profilers may give different results

Page 31: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Insertion

• Multiple flavours:

– Source code insertion

• profiling code goes in with the source

• easy to do

– Object code insertion

• profiling code goes into the .o (C++) or

.class (Java) files

• can be done statically or dynamically

• hard to do

– modified class loader

Page 32: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Insertion Pros & Cons

• Insertion Pros:

– can be used across a variety of

platforms

– accurate (in some ways)

• can’t easily do memory profiling

• Insertion Cons:

– requires recompilation or relinking of the

app

– profiling code may affect performance

• difficult to calculate exact impact

Page 33: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Sampling

• In sampling, the processor or VM is

monitored and at regular intervals an

interrupt executes and saves a

“snapshot” of the processor state

• This data is then compared with the

program’s layout in memory to get an

idea of where the program was at

each sample

Page 34: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Sampling Pros & Cons

• Sampling Pros:

– no modification of app is necessary

• Sampling Cons:

– a definite time/accuracy trade-off

• a high sample rate is accurate, but takes a

lot of time

– more…

Page 35: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Sampling Pros & Cons

• Sampling Cons:

– very small methods will almost always

be missed

• if a small method is called frequently and

you have are unlucky, small but expensive

methods may never show up

– sampling cannot easily monitor memory

usage

Page 36: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Instrumented VM

• Another way to collect information is

to instrument the Java VM

• Using this technique each and every

VM instruction can be monitored

– highly accurate

Page 37: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Instrumented VM Pros&Cons

• Pros:

– The most accurate technique

– Can monitor memory usage data as well

as time data

– Can easily be extended to allow remote

profiling

• Cons:

– The instrumented VM is platform-

specific

Page 38: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Instrumented VMs

• Java 2 (JDK 1.2 and higher)

– information can be accessed through

JVMPI

– the Java Virtual Machine Profiling

Interface • http://java.sun.com/products/jdk/1.3/docs/guide/jvmpi

/

• Microsoft JVM

– uses a COM-based interface to provide

profiling information

• JProbe Profiler

Page 39: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Common Bottlenecks • Although profiling can yield some

surprises, often the bottlenecks are

fairly obvious once the profiler points

them out

– excessive heap expansion

– not using System.arraycopy()

– using the “+” operator with String

objects

– using unbuffered I/O streams

– excessive memory allocations

– excessive calls to slow library methods

Page 40: TechGIG_Memory leaks in_java_webnair_26th_july_2012

Demo Time