1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr....

23
1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer

Transcript of 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr....

Page 1: 1 Maximize Java Performance by using YOURKIT with uPortal development Faizan Ahmed – Sr. Application Developer.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

23

Questions & Comments “!”