Learn the internal workings of the Java memory heap, how generational memory heaps work, and the different heap optimization parameters. Discover how to monitor and diagnose memory issues with AppDynamics Automatic Leak Detection and Object Instance Tracking.
3. Objectives
By the end of this course, you will be able to:
• Describe Java memory managementin detail
• Visualize Java garbage collection processes
with a profiler
• Use the AppDynamics Memory dashboard
to monitor and troubleshootgarbage collection
issues in your applications
• Use AppDynamics ObjectInstance Tracking
and Automatic Leak Detection features to
find the root cause of many common memory
issues in Java applications
• Describe the issues with ClassLoader leaks
and other PermGen / MetaSpace issues,
and how to preventthem
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 3
Course
After completing the labs in the course,you will be
able to use Object Instance Tracking,and Automatic
Leak Detection to help troubleshootmemory problems
and find the root cause of these issues.
Labs
4. APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 4
Topic 1 Garbage Collection Explained
A. How Garbage Collection Works In Java
B. The Anatomy Of The Java Memory Heap
C. Using A Profiler To Visualize The Java Memory Heap
Topic 2 Memory Heap Analysis Tooling & Workflows
A. Available Tooling
B. Using Automatic Leak Detection And Object Instance Tracking
C. OutOfMemoryError ! = Memory Leak
D. The Problem Of ClassLoader Leaks
Memory Heap Analysis with AppDynamics
Agenda
5. APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 5
Topic 1 Garbage Collection Explained
A. How Garbage Collection Works In Java
B. The Anatomy Of The Java Memory Heap
C. Using A Profiler To Visualize The Java Memory Heap
Agenda
Memory Heap Analysis with AppDynamics
6. Java Memory Management
Most Java developers know thatJava uses automatic
memory management,and that a garbage collector
manages memory withoutthe need for explicit
developer interaction.
Few of us know the details and limitations of the
garbage collector’s inner workings,and even fewer
know what to look for in order to ensure that a Java
application is notinadvertently preventing the garbage
collector from doing its job effectively.
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 6
Concepts, Details & Limitations
We’ll explain the concepts behind memory
management,and go into detail abouthow garbage
collection works,then we’ll cover the types of garbage
collectors available to Java virtual machines,and
some situations in which they might be useful.
Finally,we’ll actually look at garbage collection
in action with the help of a profiler.
7. Garbage Collection Concepts
First, what we all know about Java memory
management and garbage collection
• Java collects memory automatically by running one
or more garbage collector threads thatremove
objects that are no longer needed by the application
• Therefore, Java can not produce a memory leak per
se, right? We’ll not really.We’ll examine this question
further.
The biggestreason that the 2nd
bulleton the left
is not always true is that what Java thinks is “no longer
needed” may differ from what you, the developer think,
so an application may leak memory because objects
that you think are “no longer needed” are keptin
memory by Java.
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 7
Concepts, Details & Limitations
BAD NEWS
Knowing how the
garbage collector
works is more
complicated than
it seems.
GOOD NEWS
Java generally “does
the right thing”,so if you
know how the garbage
collector works, you should
be able to avoid most
memory management
issues
8. Knowing How The Garbage Collector Works
Because one can not force the garbage collector to do its job on
command, this is more complex than following simple guidelines
like
• Setting all objects to null when no longer needed
• Calling System.gc()
• Using certain design patterns
Each situation you encounter may be different and requires analysis.
Rather than following a recipe,it is best to understand conceptually
how the garbage collection process works.
Once you know the conceptual framework,you will usually be able to
analyze a situation and make the right decisions to avoid memory issues.
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 8
Concepts, Details & Limitations
9. APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 9
Topic 1 Garbage Collection Explained
A. How Garbage Collection Works In Java
B. The Anatomy Of The Java Memory Heap
C. Using A Profiler To Visualize The Java Memory Heap
Memory Heap Analysis with AppDynamics
Agenda
10. Setting The Stage
There are many kinds of Java applications, and, as stated
earlier, they are all different and need to be analyzed.
However, we can make some basic assumptions that
will help narrow the space to a manageable one.
1. Our target application will be a Java Web Application running
on an Oracle Java 7+ JVM
2. We’ll use Tomcat Application Server,but most Java EE
servers have similar memory managementcharacteristics
3. The JVM is using the -server switch, and therefore using
a server-optimized garbage collector
4. The JVM is not using the G1 garbage collector
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 10
11. The HotSpot Memory Space
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 11
A HotSpot JVM contains a number of different memory spaces
In our discussions of memory management,
we’ll concentrate on the heap,as it contains
the majority of application objectinstances that
are garbage collected during the life of the application
The various memory spaces are
The heap contains (most) objectreferences.
This is segmented into different regions
or generations
Stacks and other memory spaces includes
the stacks as well as method, thread, native,
and JVM execution spaces,and are smaller
than the heap
12. Short- v. Long-Lived Objects In Web Apps
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 12
In our discussions of memory management,we’ll concentrate on the
heap,as it contains the majority of application objectinstances that
are garbage collected during the life of the application.
OBJECT AGE
NUMBEROFOBJECTS
Web apps contain many more short-lived than long-lived objects,
and show an inverse relationship between the number of instances and their age
17. Ways to Control the Shape & Size of the Heap
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 17
-Xms //initial total heap size
-Xmx //Maximum total heap size; allows the heap to grow from initial to maximum size
-Xmn //Young generation size
-XX:PermSize //Initial PermGen size
-XX:MaxPermSize //Maximum PermGen size; allows PermGen to grow from initial to maximum
size
Java has a number of system switches that allow developers to control the size
or relative size of the different parts of the heap
18. APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 18
Topic 1 Garbage Collection Explained
A. How Garbage Collection Works In Java
B. The Anatomy Of The Java Memory Heap
C. Using A Profiler To Visualize The Java Memory Heap
Memory Heap Analysis with AppDynamics
Agenda
19. Types Of Garbage Collectors
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 19
SerialGC
Optimized for client
applications with
small memory
footprints
ParallelGC
Uses multiple threads to GC.
Optimized for situations in
which throughputis
importantand long pauses
are acceptable,such as
batch processing
Concurrent
Mark-Sweep(CMS) GC
Attempts to minimize
pauses.Optimized for
server JVMs
Garbage First (G1) GC
Attempts to improve on
CMS GC performance
while providing
configurable pause
thresholds
20. Serial GC
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 20
-XX:+UseSerialGC
Java has a number of system switches that allow developers to control the size
or relative size of the different parts of the heap
• Uses a single thread for both major and minor collections
• Uses compacting in the Old Gen to move surviving
objects to contiguous memory,which makes it faster to
allocate memory for new objects
• Best for clientapplications with small memory footprints
To use the SerialGC collector,add the following switch
to your Java command:
STOP THE WORLD
PAUSE
STOP THE WORLD
PAUSE
21. Parallel GC
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 21
-XX:+UseParallelGC //(default) parallel Young
Gen + serial compacting Old Gen
-XX:+UseParallelOldGC //parallel Young Gen +
parallel compacting Old Gen
Java has a number of system switches that allow developers to control the size
or relative size of the different parts of the heap
• Uses multiple threads,generally more efficientthan
SerialGC
• Will perform better than SerialGC on host with 3+ CPUs
• Optimized for throughput.However,long pauses will
occur for major collections
• Best for batch processing environments thatrequire high
throughput,but not high responsiveness
The ParallelGC collector comes in two flavors:
STOP THE WORLD
PAUSE
STOP THE WORLD
PAUSE
22. Concurrent Mark-Sweep(CMS) GC
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 22
-XX:+UseConcMarkSweepGC //in addition,
the number of threads used is set with
-XX:ParallelCMSThreads=<n> //where <n>
is the number of threads required
Java has a number of system switches that allow developers to control the size
or relative size of the different parts of the heap
• Only an Old Gen collector.Uses the same algorithm
as ParallelGC for Young Gen
• No compaction.If fragmentation is an issue,
switch to ParallelGC or use larger heap
• Optimized for low pause length relative to ParallelGC
• Performs majority of GC work concurrently with
the application threads (less Stop The World time).
• Best for server apps that need to be highly responsive
The CMS GC collector is invoked with:
STOP THE WORLD PAUSE INITIAL MARK
CONCURRENTMARK
REMARK
23. Garbage First (G1) GC
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 23
-XX:+UseG1GC //in addition, the maximum pause length
can be set with
-XX:MaxGCPauseMillis=<n> //the default is 200
Java has a number of system switches that allow developers to control the size
or relative size of the different parts of the heap
• Is a parallel,concurrent,incrementally compacting low-pause
collector – the most advanced general purpose collector available
• Divides the heap into small regions of equal size, rather than
Young and Old generations
• Focuses on collecting regions with few live objects
• Always compacts objects into differentregion,and leaves
the current region empty
• Decreases pause time, because whole region can be collected
in one operation;Includes a pause-time threshold control
The CMS GC collector is invoked with
25. Java Garbage Collection In Action
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 25
To take the concepts we’ve learned aboutgarbage
collection and make them more concrete,we’ll actually
take a look at garbage collection in action
We will use the standard Java profiler that comes with
the JDK, JVisualVM,on a web application running on
Tomcat 7 and JDK 7, and see whatthe memory heap
actually looks like in real-time
26. APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 26
Topic 2 Memory HeapAnalysis Tooling & Workflows
A. Available Tooling
B. Using Automatic Leak Detection And Object Instance Tracking
C. OutOfMemoryError ! = Memory Leak
D. The Problem Of ClassLoader Leaks
Memory Heap Analysis with AppDynamics
Agenda
27. Node-Level Memory Dashboard
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 27
AGENTS
Shows state of all AppDynamics
agents associated with this node
• App Server Agent
• Machine Agent
• Agent Diagnostics
HARDWARE
Shows CPU/ Memory
Utilization and
File/Network
I/O metrics
MEMORY
Shows JVM/CLR Heap state
•Object Instance Tracking
•Automatic Leak Detection
Provides detailed information on the state of each node
28. Object Instance Tracking(OIT)
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 28
Shows the allocated trend for
• Top 20 Application Classes
• Top 20 System Classes
• Custom Classes
OIT allows a drill-down into how objects are being
instantiated and which part of your application code is
creating each objecttype. It is useful for diagnostic
memory leak/memory thrash situations.
29. APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 29
Topic 2 Memory HeapAnalysis Tooling & Workflows
A. Available Tooling
B. Using Automatic Leak DetectionAnd Object Instance Tracking
C. OutOfMemoryError ! = Memory Leak
D. The Problem Of ClassLoader Leaks
Memory Heap Analysis with AppDynamics
Agenda
30. Automatic Leak Detection(ALD)
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 30
• Monitors all collection classes
in your application
• Shows any collections thatare
growing monotonously for a
long time
• Useful for detecting collection-
based memory leaks
• Can cause significant
overhead:Best used in
dev/test. If used in production,
enable on a single node rather
than the whole tier
32. Using Object Instance Tracking
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 32
We will cause our web
application to experience a
memory leak by repeatedly
adding instances ofan
objectin memory.
Then we’ll monitor the app
with Object Instance Tracking
and see if we can find the
objects responsible for the
memory leak.
If we do find some suspect
classes, we’ll start an
allocation tracking session to
see where these objects are
being created in our code.
33. Using Automatic Leak Detection
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 33
We will now cause our
web application to
experience a collection-
based memory leak.
Then we’ll monitor with
Automatic Leak Detection
and see if ALD can pinpoint
the leaking collection.
If we do find a culprit,
we can run allocation
sessions to find out what
is in the collection and
where in our code it
is being allocated.
34. APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 34
Topic 2 Memory HeapAnalysis Tooling & Workflows
A. Available Tooling
B. Using Automatic Leak Detection And Object Instance Tracking
C. OutOfMemoryError ! = Memory Leak
D. The Problem Of ClassLoader Leaks
Memory Heap Analysis with AppDynamics
Agenda
35. Non-Memory Leak Cause of OutOfMemoryError
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 35
Heap is too small
for (normal)
operation
Overload causes
heap to fill
OutOfMemoryError
in PermGen
Misconfiguration or Capacity
Mismatch
Misconfiguration or Capacity
Mismatch
Misconfiguration
(but see ClassLoader leak
issue, discussed next)
OutOfMemoryError can happen for a number of reasons
36. APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 36
Topic 2 Memory HeapAnalysis Tooling & Workflows
A. Available Tooling
B. Using Automatic Leak Detection And Object Instance Tracking
C. OutOfMemoryError ! = Memory Leak
D. The Problem Of ClassLoader Leaks
Memory Heap Analysis with AppDynamics
Agenda
37. ClassLoader Reference Cannot Be Garbage
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 37
Except sometimes
Any time that a class is loaded inside your application
by the system ClassLoader,a memory leak may result
Given the fact that the ClassLoader has references to every Class
objectin your application,this can chew up the PermGen quickly
• Best way to explain is to look at an example.An excellent
one is the issue of loading JDBC drivers
• The below code loads a JDBC driver
Class.forName(“com.mysql.jdbc.Driver”);
Connection c =
DriverManager.getConnection(“jdbc:mysql://dbhost/dbName”);
…
38. Many So-Called System Classes
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 38
This can occur with any of them
While the JDBC driver is a very common example,
what I’m about to show you can happen with any
of the following
• Bean introspection caches
• Shutdown hooks
• Custom defaultauthenticator
• Custom security providers
• Custom MBeans,ThreadGroups,or PropertyEditors
Now back to loading our JDBC driver…
39. Reference Map From JRE System Object
to Your Application ClassLoader
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 39
40. General Rule For Web Apps
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 40
The general rule applies to many other System classes and resources
• If you load a class in your application thatwill register itselfwith the system,
when your web application is reloaded,the JVM may not be able to garbage
collectthe ClassLoader
• Any time you have an OutOfMemoryError in PermGen, you should suspect
a ClassLoader leak
• Since many classes register themselves with a central system registry, this is
often called the “central registration problem” and itleads directly to PermGen
memory leaks as web applications are redeployed by the app server
• The problem can be particularly severe in continuous integration environments
in which applications are redeployed regularly
The way to solve the JDBC issue in our example is easy — simply move the JDBC driver
class from your application to the common app server
41. What We Learned Today
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 41
• Describe in detail the structure of the Java HotSpot JVM memory space
• Understand the process of garbage collection and how objects are promoted
through the differentmemory spaces in the Java heap
• Describe the differenttypes of garbage collectors available in Java,and what
conditions they are optimized for
• Use AppDynamics memory managementtools like ALD and OIT to diagnose
memory issues in your application
• Understand the causes of ClassLoader memory leaks and how to avoid them
You should now be able to complete a few tasks
43. University
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 43
Multiple Track Courses and Certifications @ appdynamics.com/university
Sign Up for Multiple Tracks and Course Dates
for Live Training Sessions
Watch quick task-based tutorials
Take Self-Paced courses to learn at your own pace
Access subscriber only contents
44. Community
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 44
Connect With Other Users And Experts @ community.appdynamics.com
Exchange information with other
Power Users and AppDynamics experts
Get AppDynamics-supported
and author-supported extensions
45. Tech Webinars
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 45
Attend for free
Every other Wednesday at10:00am PT
45 Min presentation followed by a Q&A session
View the Tech Webinars Schedule to register for
upcoming webinars and watch pastpresentations.
46. Join The FanClub
APPDYNAMICS CONFIDENTIAL AND PROPRIETARY 46
We love our customers and partners. Let us thank you for your ongoing support.
See new features and roadmaps
Try new products and beta programs
Access custom extensions and training
MeetAppDynamics Executives
Get premium swag and invitations to events
Sign up and claim your benefits
by clicking here to submit the request!
47. Thank You
Please fill out the survey:
https://www.surveymonkey.com/r/AppDynamicsUniversity
Your feedback is very important to us. We use it to help us improve our content
and incorporate new features to keep our courses relevantfor our audience.
Ray Baco
ray.baco@appdynamics.com