SlideShare a Scribd company logo
1 of 40
Download to read offline
Memory Leaks in Java
By:- Ashish Bhasin
ashbhasin@yahoo.com
Agenda
• Introduction
• Resolving Memory Leaks
Then why are we
here???
Memory Management
is done by Java
Introduction
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.
Memory Manager – Garbage collector (GC)
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
7
What is memory leak in java?
(Memory Map)
Java Memory
leak
Allocated
Reachable
Live
Handled by
JVM
8
Why Memory Leak is BAD…
• Memory-related issues affect
execution speed and reliability of
application.
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.
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
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.
• 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)
We will see how
to catch Memory
leaks
Oops, Java has
memory leaks
Resolving Memory
Leaks
Resolving Memory Leaks
• What to look in your program
– Is My Program actually leaking Memory
– Monitoring Process
– Dump the heap
– Analyze the heap
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?
Monitoring Memory Leaks
- Verbose GC log
Monitoring using GC which shows that we might
have leak in the program
Monitoring Java Process
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.
Tools to Analyze Heap Dump
Fast leak
Slow leak
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
Slow and Small Memory Leaks
Profiling
Profiling
• What is Profiling?
• What Profiling Tells You
• What Profiling Is Not
• Manual Profiling
• Profiling Techniques Overview
– Insertion
– Sampling
– Instrumented VM
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
What Profiling Tells You
• Basic information:
– How much time is spent in each
method? (“flat” profiling)
– How many objects of each type are
allocated?
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?
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
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
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());
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
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
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
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
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…
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
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
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
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
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
Demo Time

More Related Content

Similar to Resolve Java Memory Leaks

Webinar: Zing Vision: Answering your toughest production Java performance que...
Webinar: Zing Vision: Answering your toughest production Java performance que...Webinar: Zing Vision: Answering your toughest production Java performance que...
Webinar: Zing Vision: Answering your toughest production Java performance que...Azul Systems Inc.
 
A Taste of Monitoring and Post Mortem Debugging with Node
A Taste of Monitoring and Post Mortem Debugging with Node A Taste of Monitoring and Post Mortem Debugging with Node
A Taste of Monitoring and Post Mortem Debugging with Node ibmwebspheresoftware
 
.Net Architecture and Performance Tuning
.Net Architecture and Performance Tuning.Net Architecture and Performance Tuning
.Net Architecture and Performance TuningGauranG Bajpai
 
Vulnerability, exploit to metasploit
Vulnerability, exploit to metasploitVulnerability, exploit to metasploit
Vulnerability, exploit to metasploitTiago Henriques
 
Lec 1-of-oop2
Lec 1-of-oop2Lec 1-of-oop2
Lec 1-of-oop2SM Rasel
 
Performance tuning Grails Applications GR8Conf US 2014
Performance tuning Grails Applications GR8Conf US 2014Performance tuning Grails Applications GR8Conf US 2014
Performance tuning Grails Applications GR8Conf US 2014Lari Hotari
 
Java Performance & Profiling
Java Performance & ProfilingJava Performance & Profiling
Java Performance & ProfilingIsuru Perera
 
19-reliabilitytesting.ppt
19-reliabilitytesting.ppt19-reliabilitytesting.ppt
19-reliabilitytesting.pptAnilteaser
 
The Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance TuningThe Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance TuningjClarity
 
Memory Profiling
Memory ProfilingMemory Profiling
Memory Profilingnanthaaru
 
Java performance tuning
Java performance tuningJava performance tuning
Java performance tuningJerry Kurian
 
Application Profiling for Memory and Performance
Application Profiling for Memory and PerformanceApplication Profiling for Memory and Performance
Application Profiling for Memory and PerformanceWSO2
 
Web Sphere Problem Determination Ext
Web Sphere Problem Determination ExtWeb Sphere Problem Determination Ext
Web Sphere Problem Determination ExtRohit Kelapure
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidStanojko Markovik
 
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK toolsHaribabu Nandyal Padmanaban
 
Analytics tools and Instruments
Analytics tools and InstrumentsAnalytics tools and Instruments
Analytics tools and InstrumentsKrunal Soni
 

Similar to Resolve Java Memory Leaks (20)

Webinar: Zing Vision: Answering your toughest production Java performance que...
Webinar: Zing Vision: Answering your toughest production Java performance que...Webinar: Zing Vision: Answering your toughest production Java performance que...
Webinar: Zing Vision: Answering your toughest production Java performance que...
 
A Taste of Monitoring and Post Mortem Debugging with Node
A Taste of Monitoring and Post Mortem Debugging with Node A Taste of Monitoring and Post Mortem Debugging with Node
A Taste of Monitoring and Post Mortem Debugging with Node
 
.Net Architecture and Performance Tuning
.Net Architecture and Performance Tuning.Net Architecture and Performance Tuning
.Net Architecture and Performance Tuning
 
Vulnerability, exploit to metasploit
Vulnerability, exploit to metasploitVulnerability, exploit to metasploit
Vulnerability, exploit to metasploit
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Lec 1-of-oop2
Lec 1-of-oop2Lec 1-of-oop2
Lec 1-of-oop2
 
Performance tuning Grails Applications GR8Conf US 2014
Performance tuning Grails Applications GR8Conf US 2014Performance tuning Grails Applications GR8Conf US 2014
Performance tuning Grails Applications GR8Conf US 2014
 
Java Performance & Profiling
Java Performance & ProfilingJava Performance & Profiling
Java Performance & Profiling
 
PHP - Introduction to PHP Bugs - Debugging
PHP -  Introduction to  PHP Bugs - DebuggingPHP -  Introduction to  PHP Bugs - Debugging
PHP - Introduction to PHP Bugs - Debugging
 
19-reliabilitytesting.ppt
19-reliabilitytesting.ppt19-reliabilitytesting.ppt
19-reliabilitytesting.ppt
 
The Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance TuningThe Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance Tuning
 
Memory Profiling
Memory ProfilingMemory Profiling
Memory Profiling
 
Java performance tuning
Java performance tuningJava performance tuning
Java performance tuning
 
Visual Studio Profiler
Visual Studio ProfilerVisual Studio Profiler
Visual Studio Profiler
 
Application Profiling for Memory and Performance
Application Profiling for Memory and PerformanceApplication Profiling for Memory and Performance
Application Profiling for Memory and Performance
 
SE Unit-1.pptx
SE Unit-1.pptxSE Unit-1.pptx
SE Unit-1.pptx
 
Web Sphere Problem Determination Ext
Web Sphere Problem Determination ExtWeb Sphere Problem Determination Ext
Web Sphere Problem Determination Ext
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with android
 
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
 
Analytics tools and Instruments
Analytics tools and InstrumentsAnalytics tools and Instruments
Analytics tools and Instruments
 

More from Ashish Bhasin

Digital Strategy for future business
Digital Strategy for future businessDigital Strategy for future business
Digital Strategy for future businessAshish Bhasin
 
Digital strategy overview
Digital strategy overviewDigital strategy overview
Digital strategy overviewAshish Bhasin
 
Strategy- Mission to Win
Strategy- Mission to WinStrategy- Mission to Win
Strategy- Mission to WinAshish Bhasin
 
Io t presentation_v01
Io t presentation_v01Io t presentation_v01
Io t presentation_v01Ashish Bhasin
 
Sales Training - Welcome to Organized Jungle
Sales Training - Welcome to Organized JungleSales Training - Welcome to Organized Jungle
Sales Training - Welcome to Organized JungleAshish Bhasin
 
Connected Car by Ashish Bhasin
Connected Car by Ashish BhasinConnected Car by Ashish Bhasin
Connected Car by Ashish BhasinAshish Bhasin
 

More from Ashish Bhasin (9)

Digital Strategy for future business
Digital Strategy for future businessDigital Strategy for future business
Digital Strategy for future business
 
Digital strategy overview
Digital strategy overviewDigital strategy overview
Digital strategy overview
 
Story of 8_frogs
Story of 8_frogsStory of 8_frogs
Story of 8_frogs
 
Strategy- Mission to Win
Strategy- Mission to WinStrategy- Mission to Win
Strategy- Mission to Win
 
Io t presentation_v01
Io t presentation_v01Io t presentation_v01
Io t presentation_v01
 
Alien ceo ver03
Alien ceo ver03Alien ceo ver03
Alien ceo ver03
 
Intro to analytics
Intro to analyticsIntro to analytics
Intro to analytics
 
Sales Training - Welcome to Organized Jungle
Sales Training - Welcome to Organized JungleSales Training - Welcome to Organized Jungle
Sales Training - Welcome to Organized Jungle
 
Connected Car by Ashish Bhasin
Connected Car by Ashish BhasinConnected Car by Ashish Bhasin
Connected Car by Ashish Bhasin
 

Recently uploaded

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Recently uploaded (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Resolve Java Memory Leaks

  • 1. Memory Leaks in Java By:- Ashish Bhasin ashbhasin@yahoo.com
  • 3. Then why are we here??? Memory Management is done by Java Introduction
  • 4. 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.
  • 5. Memory Manager – Garbage collector (GC)
  • 6. 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
  • 7. 7 What is memory leak in java? (Memory Map) Java Memory leak Allocated Reachable Live Handled by JVM
  • 8. 8 Why Memory Leak is BAD… • Memory-related issues affect execution speed and reliability of application.
  • 9. 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.
  • 10. 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
  • 11. 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.
  • 12. • 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)
  • 13. We will see how to catch Memory leaks Oops, Java has memory leaks Resolving Memory Leaks
  • 14. Resolving Memory Leaks • What to look in your program – Is My Program actually leaking Memory – Monitoring Process – Dump the heap – Analyze the heap
  • 15. 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?
  • 16. Monitoring Memory Leaks - Verbose GC log Monitoring using GC which shows that we might have leak in the program
  • 18. 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.
  • 19. Tools to Analyze Heap Dump Fast leak Slow leak
  • 20. 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
  • 21. Slow and Small Memory Leaks
  • 23. Profiling • What is Profiling? • What Profiling Tells You • What Profiling Is Not • Manual Profiling • Profiling Techniques Overview – Insertion – Sampling – Instrumented VM
  • 24. 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
  • 25. What Profiling Tells You • Basic information: – How much time is spent in each method? (“flat” profiling) – How many objects of each type are allocated?
  • 26. 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?
  • 27. 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
  • 28. 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
  • 29. 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());
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. 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
  • 34. 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…
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 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
  • 39. 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