How To Detect Java Memory Leaks
How To Detect Java Memory Leaks
HowtoDetectJavaMemoryLeaks|Toptal
HIREADEVELOPER
APPLYASADEVELOPER
LOGIN
SUBSCRIBE
GetExclusiveUpdates
No spam. Just great engineering
posts.
Vote
Like
171
Share
22
88
Share
Tweet
TRENDING ARTICLES
79
Programming Editors:
A Never Ending Battle
With No Clear Winner
Inexperienced programmers
often think that Javas automatic
garbage collection completely
frees them from worrying about
memory management. This is a
common misperception: while
the garbage collector does its
best, its entirely possible for
even the best programmer to
fall prey to crippling memory
leaks. Let me explain.
http://www.toptal.com/java/huntingmemoryleaksinjava
7 days ago
Rapid Application
Development with
AllcountJS
2 days ago
1/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
The Roadmap to
Roadmaps: A Survey
of the Best Online
Mapping Tools
10 days ago
Are We Creating An
Insecure Internet of
Things?
3 days ago
RELEVANT TECHNOLOGIES
Java EE
Back-end
Java
TOPTAL AUTHORS
Raoni Boaventura
Front-end Software Engi
Pavel Tiunov
Michael Houghton
SOFTWARE ENGINEER
Performance: usually
associated with excessive
object creation and deletion,
long delays in garbage
collection, excessive
http://www.toptal.com/java/huntingmemoryleaksinjava
Balint Erdi
Software engineer
2/33
07/03/2015
Brian VanLoo
Software Engineer
allocations.
In this post, Ill focus on Java
heaps leaks and outline an
3/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
necessarily manifest
themselves as OOMs, especially
in the case of desktop
applications or client
applications (which arent run for
very long without restarts).
Think of memory
leakage as a disease
and the
OutOfMemoryError as
a symptom. But not all
OutOfMemoryErrors
http://www.toptal.com/java/huntingmemoryleaksinjava
4/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
OutOfMemoryErrors
imply memory leaks,
and not all memory
leaks manifest
themselves as
OutOfMemoryErrors.
Deciphering the
OutOfMemoryError
As mentioned above, the OOM
is a common indication of a
memory leak. Essentially, the
error is thrown when theres
insufficient space to allocate a
new object. Try as it might, the
garbage collector cant find the
necessary space, and the heap
cant be expanded any further.
Thus, an error emerges, along
with a stack trace.
http://www.toptal.com/java/huntingmemoryleaksinjava
5/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
http://www.toptal.com/java/huntingmemoryleaksinjava
6/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
. After some
investigation, I figured out that
OutOfMemoryError
http://www.toptal.com/java/huntingmemoryleaksinjava
7/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
PermGen space
This error message indicates
that the permanent generation
is full. The permanent
generation is the area of the
heap that stores class and
method objects. If an application
loads a large number of classes,
then the size of the permanent
generation might need to be
increased using the
XX:MaxPermSize option.
Interned
java.lang.String
class
8/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
9/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
10/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
Application Crash
Without OOM
Occasionally, an application
might crash soon after an
http://www.toptal.com/java/huntingmemoryleaksinjava
11/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
malloc
system
Diagnosing Leaks
In most cases, diagnosing
http://www.toptal.com/java/huntingmemoryleaksinjava
12/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
1. Identify Symptoms
As discussed, in many cases,
the Java process will eventually
throw an OOM runtime
exception, a clear indicator that
your memory resources have
been exhausted. In this case,
you need to distinguish
between a normal memory
exhaustion and a leak.
Analyzing the OOMs message
and try to find the culprit based
on the discussions provided
above.
Oftentimes, if a Java application
requests more storage than the
runtime heap offers, it can be
http://www.toptal.com/java/huntingmemoryleaksinjava
13/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
output.
http://www.toptal.com/java/huntingmemoryleaksinjava
14/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
GetExclusiveUpdates
3. Enable Profiling
Different JVMs offer different
ways to generate trace files to
reflect heap activity, which
http://www.toptal.com/java/huntingmemoryleaksinjava
15/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
16/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
http://www.toptal.com/java/huntingmemoryleaksinjava
17/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
18/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
.
Permanent Generation - A
third generation closely
related to the tenured
generation, the permanent
generation is special
because it holds data
required by the virtual
machine to describe objects
that do not have an
equivalence at the Java
language level. For example,
objects describing classes
and methods are stored in
the permanent generation.
Java is smart enough to apply
different garbage collection
methods to each generation.
The young generation is
handled using a tracing,
copying collector called the
Parallel New Collector. This
http://www.toptal.com/java/huntingmemoryleaksinjava
19/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
Detecting a Memory
Leak
To find and eliminate a memory
leak, you need the proper tools.
Its time to detect and remove
such a leak using the Java
VisualVM.
http://www.toptal.com/java/huntingmemoryleaksinjava
20/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
For example:
jstatdp1234JDjava.security.policy
http://www.toptal.com/java/huntingmemoryleaksinjava
21/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
22/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
MemLeak
Of course, there are a number
of ways to create memory leaks
in Java. For simplicity we will
define a class to be a key in a
HashMap, but we will not define
the equals() and hashcode()
methods.
A HashMap is a hash table
implementation for the Map
interface, and as such it defines
the basic concepts of key and
value: each value is related to a
unique key, so if the key for a
given key-value pair is already
present in the HashMap, its
current value is replaced.
Its mandatory that our key class
provides a correct
implementation of the equals()
and hashcode() methods.
Without them, there is no
guarantee that a good key will
be generated.
By not defining the equals()
and hashcode() methods, we
add the same key to the
HashMap over and over and,
instead of replacing the key as it
http://www.toptal.com/java/huntingmemoryleaksinjava
23/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
instead of replacing the key
as it
should, the HashMap grows
continuously, failing to identify
these identical keys and
throwing an OutOfMemoryError .
packagecom.post.memory.leak;
importjava.util.Map;
publicclassMemLeak{
publicfinalStringkey;
publicMemLeak(Stringkey){
this.key=key;
}
publicstaticvoidmain(Stringargs[]){
try{
Mapmap=System.getProperties();
for(;;){
map.put(newMemLeak(
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
24/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
http://www.toptal.com/java/huntingmemoryleaksinjava
25/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
Conclusion
Memory leaks are among the
most difficult Java application
problems to resolve, as the
symptoms are varied and
difficult to reproduce. Here,
weve outlined a step-to-step
approach to discovering
memory leaks and identifying
http://www.toptal.com/java/huntingmemoryleaksinjava
26/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
Appendix
Along with Java VisualVM, there
are several other tools that can
perform memory leak detection.
Many leak detectors operate at
the library level by intercepting
calls to memory management
routines. For example: HPROF,
is a simple command line tool
bundled with the Java 2
Platform Standard Edition (J2SE)
for heap and CPU profiling. The
output of HPROF can be
analyzed directly or used as an
input for others tools like JHAT.
When we work with Java 2
Enterprise Edition (J2EE)
applications, there are a number
of heapdump solutions that are
friendlier to analyze, such as
IBM Heapdumps for Websphere
application servers.
http://www.toptal.com/java/huntingmemoryleaksinjava
Jose
Ferreira
de
27/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
de
Souza
Filho,
Brazil
HiretheAuthor
MEMBER
SINCE
DECEMBER
27, 2012
Java
Struts
JavaServer
Faces
Hibernate
Spring
Struts
2
Java
Servlets
Java
ME
Java
EE
Java
SE
WebLogic
http://www.toptal.com/java/huntingmemoryleaksinjava
Jose
is
a
seasoned
software
developer
and
engineer
with
over
12
years
28/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
years
of
experience
in
IT.
He
has
experience
in
the
development,
migration,
and
integration
of
software
and
efficient
architectures.
He
is
especially
practiced
in
the
use
of
debugging
and
performance
profiling
tools.
[click
to
continue...]
Comments
Community
http://www.toptal.com/java/huntingmemoryleaksinjava
Login
29/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
Recommend 10
Login
Share
SortbyBest
Jointhediscussion
TapanKumar
ayearago
P{marginbottom:0.08in
}A:link{}
Appljustcontinuouslyadding
akey(objectwithastatic
instancevariable),valuein
Properties,aftersometime
belowexceptiondisplayed.
Exceptioninthread"main"
java.lang.OutOfMemoryError:
GCoverheadlimitexceeded
4
Reply Share
rags ayearago
Goodarticle.Onestatement
isincorrect"Thepermanent
generationistheareaofthe
heap"
Permanentgenerationisnot
partoftheheap.It'sa
separatespaceforclass
definitionsandrelateddata.
Ref:
http://docs.oracle.com/javase/...
EdenSpace(heap):Thepool
fromwhichmemoryisinitially
allocatedformostobjects.
SurvivorSpace(heap):The
poolcontainingobjectsthat
havesurvivedthegarbage
collectionoftheEdenspace.
TenuredGeneration(heap):
Thepoolcontainingobjects
thathaveexistedforsome
timeinthesurvivorspace.
http://www.toptal.com/java/huntingmemoryleaksinjava
30/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
PermanentGeneration(non
heap):Thepoolcontainingall
thereflectivedataofthe
virtualmachineitself,suchas
classandmethodobjects.
WithJavaVMsthatuseclass
datasharing,thisgeneration
isdividedintoreadonlyand
readwriteareas.
CodeCache(nonheap):The
HotSpotJavaVMalso
includesacodecache,
containingmemorythatis
usedforcompilationand
storageofnativecode.
2
Reply Share
JosFerreirade
SouzaFilho>
rags ayearago
Hirags,youabsolutely
rightthepermanent
generationisnotpart
oftheheap.Itisa
separatedareato
representnativecode,
strings,etc..Thank
youforpointingthat
out!
1
Reply
Share
teknotry 21daysago
Nicepost..
Reply Share
MahmoudHanafy
10monthsago
howcanIknowwhich
classesthatcreatethese
objects?
InaBigProjectthatcontains
manyclassesit'shardto
determinewhichclasses
http://www.toptal.com/java/huntingmemoryleaksinjava
31/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
Toptal connects
the top 3% of
freelance
developers all
over the world.
Join the
OR
Toptal
community.
TRENDING ON BLOG
HIRE A DEVELOPER
APPLY AS A DEVELOPER
NAVIGATION
CONTACT
SOCIAL
Programming
Editors: A Never
Ending Battle With
No Clear Winner
Top 3%
Work at
Toptal
Facebook
Google+
What
Apply as a
developer
Become a
partner
Dribbble
Rapid Application
Why
How
Clients
Team
Community
http://www.toptal.com/java/huntingmemoryleaksinjava
Twitter
GitHub
Send us an
email
32/33
07/03/2015
HowtoDetectJavaMemoryLeaks|Toptal
Development with
AllcountJS
Blog
The Roadmap to
Roadmaps: A Survey
of the Best Online
Mapping Tools
Videos
Top 10 Most
Common Java
Mistakes: Beginners
Tutorial
Contact
Resources
Call
888.604.3188
Client reviews
Developer reviews
FAQ
Are We Creating An
Insecure Internet of
Things?
http://www.toptal.com/java/huntingmemoryleaksinjava
Privacy Policy,
33/33