Post on 17-Dec-2015
1
Maximize Java Performance by using YOURKIT with uPortal development
Faizan Ahmed – Sr. Application Developer
2
Overview
Why memory related issues are problem?
Introduction to YourKit. Some terms What is Java Memory Leak? Find and resolve memory leak (demo) Some code tips.
3
Why memory related issues can be performance problem?
Memory-related issues affect execution speed and reliability of application.
4
Getting started with YourKit Download and extract the zip file. Set PATH to dll
<YJP Home>\bin\win32 Check installation
java -Xrunyjpagent:help IDE configuration
Plug-in installation Enable action group “profile”Windows| customize perspective| commands
Agent configuration-agentlib:yjpagent
5
Snapshots
YourKit investigative model is based upon snapshots which capture the state of the application at specific points in time.
6
Dynamic Nature of a RootSet Root Set is a set of foundational object
references within your application
public class uPortal {
private static uPortal portal = null;
public static void main(String args[]){
portal = new uPortal();
portal.method1();
}
Root Set:uPortal portalString[] args
7
Dynamic Nature of a RootSet
Private void method1 {
FooObject fooObject = new FooObject();
……..
}
Root Set:uPortal portalString[] argsFooObject fooObject
8
Dynamic Nature of a RootSet
public class uPortal {private static uPortal portal = null;public static void main(String args[]){portal = new uPortal();portal.method1();….}
Root Set:uPortal portalString[] args
9
What is Memory leak in Java?
Occurs when objects that have outlived their usefulness to the application remain within the heap through successive garbage collections
10
What is a memory leak in Java?
We can extend the set of object states to three: Allocated
Exists within the JVM’s heap Reachable
A path exists (directly or indirectly) from a member of the root set, through a sequence of references to that object.
Live From the intent of the application design the
program will use the object.
11
What is memory leak in java?
Java Memory leak
AllocatedReachable
Live
Handled by JVM
12
What are loitered Objects?
Loitered objects are reachable, but they are not live.
13
Most common memory leak causes
Failure to remove Stale Object References from Data Structures.
Lingering Transitional references. A reference to a short-term object is
used transiently by a long-term object but the long-term object does not clear the reference when it’s done with it.
14
Preparing for your investigation Have a firm conceptual grasp of the
architecture of the application you are about to analyze.
An attitude that you can find, and resolve, the underlying problem.
15
How to find leaked Objects?
By Comparing snapshots.
16
How to find leaked Objects?
More Disciplined approach. Establish a hypothesis (around the
expected leak area) (What you expect to see).
Design and run an experiment to prove your hypothesis (a use case).
Compare the experimental results against your hypothesis.
Resolve the differences. (if any)
17
Loitering Objects Recap Objects that remain within the heap
past their useful life to the application. To eliminate loitering objects from your
application, you must first: Identify which objects (if any) are loitering
within your application. Determine why those objects remain
reachable beyond their designed lifetime.
18
Some Tips
Reference variable scope: Don’t be concerned about assignments
to method-based reference variables within methods of short execution time.
Be attentive of assignments to class-based and object-based reference variables, and method-based reference variables within methods of long execution time.
19
Some Tips
As a general rule do not use finalizer. Where appropriate to your design,
use the method attributes: static final private
20
Some Tips (Continued)
Try to keep critical methods to a few statements in length, and declare them static, final or private.
In critical loops, avoid method calls within the loop termination test..
Example on next page
21
Some Tips (Continued)
Replace …for (int i=0; i < collection.size(); i+
+){….
}Withfor(int i=0, n=collection.size(); i<n;
i++) { ---}
22
Some Tips (Continued)
If you cache some objects you better have a “working” caching strategy and remove objects from cache at some point.
No user session really means “0” user session in the system.
23
Questions & Comments “!”