![Page 1: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/1.jpg)
1
Maximize Java Performance by using YOURKIT with uPortal development
Faizan Ahmed – Sr. Application Developer
![Page 2: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/2.jpg)
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.
![Page 3: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/3.jpg)
3
Why memory related issues can be performance problem?
Memory-related issues affect execution speed and reliability of application.
![Page 4: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/4.jpg)
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
![Page 5: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/5.jpg)
5
Snapshots
YourKit investigative model is based upon snapshots which capture the state of the application at specific points in time.
![Page 6: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/6.jpg)
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
![Page 7: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/7.jpg)
7
Dynamic Nature of a RootSet
Private void method1 {
FooObject fooObject = new FooObject();
……..
}
Root Set:uPortal portalString[] argsFooObject fooObject
![Page 8: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/8.jpg)
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
![Page 9: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/9.jpg)
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
![Page 10: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/10.jpg)
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.
![Page 11: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/11.jpg)
11
What is memory leak in java?
Java Memory leak
AllocatedReachable
Live
Handled by JVM
![Page 12: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/12.jpg)
12
What are loitered Objects?
Loitered objects are reachable, but they are not live.
![Page 13: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/13.jpg)
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.
![Page 14: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/14.jpg)
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.
![Page 15: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/15.jpg)
15
How to find leaked Objects?
By Comparing snapshots.
![Page 16: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/16.jpg)
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)
![Page 17: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/17.jpg)
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.
![Page 18: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/18.jpg)
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.
![Page 19: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/19.jpg)
19
Some Tips
As a general rule do not use finalizer. Where appropriate to your design,
use the method attributes: static final private
![Page 20: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/20.jpg)
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
![Page 21: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/21.jpg)
21
Some Tips (Continued)
Replace …for (int i=0; i < collection.size(); i+
+){….
}Withfor(int i=0, n=collection.size(); i<n;
i++) { ---}
![Page 22: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/22.jpg)
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.
![Page 23: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer](https://reader038.vdocument.in/reader038/viewer/2022103022/56649cfa5503460f949cc2f7/html5/thumbnails/23.jpg)
23
Questions & Comments “!”