SlideShare a Scribd company logo
Eclipse Day India 2015 - Java bytecode analysis and JIT
Java ByteCode Analysis
Educate yourself with “Java Bytecode”
Vaibhav Choudhary (@vaibhav_c)
vaibhav.x.choudhary@oracle.com
http://blogs.oracle.com/vaibhav
Agenda
•
Java Bytecode Basics
•
Be familiar with Bytecode tools
•
Bytecode evaluation
•
Language Feature vs Bytecode
•
Dive deeper
•
Changes happened in JDK7 and onwards
•
QA
3
Java Bytecode Basics
•
Why to know Java Bytecode:-
– It will help you understand your code better.
– Knowing the kitchen from where food is coming
– Clear the myths
4
Java Bytecode Tools
●
javap -help
●
javap -c
●
javap -p
●
javap -verbose
●
javap -c -p -verbose → Its a good option
5
Bytecode Evaluation
●
Write Once, Run Anywhere (WORA)
●
Provides more safety and security – Managed
env.
●
Optimizations can be done at runtime
●
Its hard to deal with CPU instrs.
6
JVM Principals for Bytecode
●
All operations are stack based.
●
Parameters, locals – All uses stack
●
Push/Pop/Dup/Dup2
●
Stack slot – 32 bit (how to handle
long/double ?)
●
Classes - fully qualified name
7
Class file format
8
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
Invoke* method calls
●
invokestatic – invokes class (static) method
●
invokespecial - Invoke instance method;
special handling for superclass, private, and
instance initialization method invocations
●
invokeinterface – invoke interface method
●
invokevirutal - Invoke instance method;
dispatch based on class
9
JDK 7 onwards - InvokeDynmaic
Get rid of Myths
●
Java don't have GOTO statements.
– Why we need GOTO at first place ?
●
Use StringBuilders if your string is changing.
– What string concat do in java.
●
How inner classes work
– Do it mock on encapsulation.
●
Many mores …
10
Execution Process
11
.java .class
javac
Just In Time
Compiler
Just In Time
CompilerNative CodeNative OS
Java
Interpreter
Java
Interpreter
Java Virtual
Machine
Java Virtual
Machine
Flavors of Just In Time (JIT) Compiler
•
JIT comes in two flavors
•
C1 (Client compiler)
– -client option
•
C2 (Server compiler)
– -server option
•
-XX:+TieredCompilation
– Better decision of compilers.
•
What happens if: -client -XX:+TieredCompilation
12
JIT optimizations – Expression Optimization
•
Eliminate dead codes.
•
Expression optimization.
13
int someCalculation(int x1, int x2, int x3) {
int res1 = x1+x2;
int res2 = x1-x2;
int res3 = x1+x3;
return (res1+res2)/2; }
int someCalculation(int x1, int x2, int x3) {
return x1; }
JIT Optimization – Inline Method
•
Inline method
– Substitute body of the method (<35 bytes of JVM bytecode)
– This provides the best optimization by JIT
– A better inline that C++
– Reduction of function calling overhead. Something else ?
14
int compute(int var) {
int result;
if(var > 5) {
result = computeFurther(var);
}
else {
result = 100;
}
return result;
}
myVal = compute(3);
myVal = 100;
JIT Optimization – Caching Technique
•
Caching read calls
– Store it for future request.
– major performance impact in loops
15
Point findMid(Point p1, Point p2) {
Point p;
p.x = (p1.x + p2.x)/2;
p.y = (p1.y + p2.y)/2;
return p;
...
...
x – p1.x;
y – p1.y;
while(!monitor.flag)
….....
….....
boolean flag = monitor.flag
JIT Optimization – Uncommon Trap
•
De-optimization
– UncommonTrap
– Scrap the optimization.
16
int compute(int var) {
int result;
if(var > 5) {
result = computeFurther(var);
}
else {
result = 100;
}
return result;
}
int compute(int var) {
if(var > 5)
uncommonTrap();
return 100;
}
JIT Optimization – Monomorphic dispatch
•
Class Hierarchy Analysis
– Optimization decision based on assumptions.
– Re-evaluate assumption when new class loaded.
17
public class Animal {
private String color;
public String getColor() {
return color;
} }
myColor = animal.getColor();
public class Animal {
String color;
}
myColor = animal.color;
JIT Optimization – Null Checks
•
Implicit Null Check
– Virtual machine believe code will not throw null
pointer.
– What if VM got a null pointer ?
18
x = point.x;
y = point.y;
Equivalents to:
if(point==null) throw new NullPointerException();
else {
x = point.x;
y = point.y;
}
For each reference in your code, VM
checks null pointer. If VM assumes that
Your code will not throw NPE, it will
remove the check. If happened, de-optimize.
Handle the SEGV.
JIT Optimization – Multi-threading Env.
•
Eliminate locks if monitor is not reachable from other
threads
•
Join adjacent synchronized blocks on the same object
•
Do check options:
– XX:+UseBiasedLocking
19
JIT Optimization – Loop Optimization
•
Combining loops – Two loops can be combined if
taking equivalent time.
•
Inversion loops – Change while into do-while.
•
Tiling loops – Re-organize loop so that it will fix in
cache.
20
JVM Options
•
Xint – Interpreter mode
•
Xcomp – Compiled mode
•
Xmixed – Interpreter + Compiler
•
-server → C2 compiler
•
-client → C1 compiler
•
-XX:+TieredCompilation → C1 + C2 (used by 32/64 bit
mode)
•
-d32/-d64 options
21
Benchmarking Difficulties
•
Garbage collect can distort benchmarking.
•
Benchmarking can be distorted by unintentionally
dead code
•
Improper warm-up can distort benchmarking.
•
Dynamic De-optimziation can distort benchmarking.
•
Momomorphic call can distort benchmarking.
•
Inlining of code can distort benchmarking.
22
Logging Options
•
-XX:+UnlockDiagnosticVMOptions
•
-XX:+LogCompilation
•
-XX:LogFile=<path to file>
•
-XX:MaxInlineSize=<size>
•
-XX:FreqInlineSize=<size>
•
Many more ...
23
References
•
Subscribe with -
http://www.javaperformancetuning.com/
•
Java Performance Documentation - Link
•
Oracle JIT Compilation Documentation - Link
•
Understand JIT Compilation with JITWatch - Link
•
My Blog – http://blogs.oracle.com/vaibhav
24

More Related Content

What's hot (20)

PDF
Quick Introduction to Kotlin Coroutine for Android Dev
Shuhei Shogen
 
PDF
java8-patterns
Justin Lin
 
PDF
Counter Wars (JEEConf 2016)
Alexey Fyodorov
 
PDF
Skiron - Experiments in CPU Design in D
Mithun Hunsur
 
PPTX
Optimizing Java Notes
Adam Feldscher
 
PDF
GCC LTO
Wang Hsiangkai
 
PDF
Klee and angr
Wei-Bo Chen
 
PDF
clWrap: Nonsense free control of your GPU
John Colvin
 
PPTX
[Sitcon2018] Analysis and Improvement of IOTA PoW Implementation
Zhen Wei
 
PPTX
Fork and join framework
Minh Tran
 
PPTX
Iron Languages - NYC CodeCamp 2/19/2011
Jimmy Schementi
 
PPTX
Fm wtm12-v2
Miguel Gamboa
 
PDF
Productive OpenCL Programming An Introduction to OpenCL Libraries with Array...
AMD Developer Central
 
PDF
NYAN Conference: Debugging asynchronous scenarios in .net
Alexandra Hayere
 
PDF
Introduction to Kotlin for Java developer
Shuhei Shogen
 
PDF
PyParis2017 / Function-as-a-service - a pythonic perspective on severless com...
Pôle Systematic Paris-Region
 
ODP
Threads and concurrency in Java 1.5
Peter Antman
 
PDF
JavaScript Execution Context
Juan Medina
 
PDF
IL2CPP: Debugging and Profiling
joncham
 
PDF
are Code Katas always Agile ?
Ciro Borrelli
 
Quick Introduction to Kotlin Coroutine for Android Dev
Shuhei Shogen
 
java8-patterns
Justin Lin
 
Counter Wars (JEEConf 2016)
Alexey Fyodorov
 
Skiron - Experiments in CPU Design in D
Mithun Hunsur
 
Optimizing Java Notes
Adam Feldscher
 
Klee and angr
Wei-Bo Chen
 
clWrap: Nonsense free control of your GPU
John Colvin
 
[Sitcon2018] Analysis and Improvement of IOTA PoW Implementation
Zhen Wei
 
Fork and join framework
Minh Tran
 
Iron Languages - NYC CodeCamp 2/19/2011
Jimmy Schementi
 
Fm wtm12-v2
Miguel Gamboa
 
Productive OpenCL Programming An Introduction to OpenCL Libraries with Array...
AMD Developer Central
 
NYAN Conference: Debugging asynchronous scenarios in .net
Alexandra Hayere
 
Introduction to Kotlin for Java developer
Shuhei Shogen
 
PyParis2017 / Function-as-a-service - a pythonic perspective on severless com...
Pôle Systematic Paris-Region
 
Threads and concurrency in Java 1.5
Peter Antman
 
JavaScript Execution Context
Juan Medina
 
IL2CPP: Debugging and Profiling
joncham
 
are Code Katas always Agile ?
Ciro Borrelli
 

Viewers also liked (20)

PPTX
Java Bytecode Fundamentals - JUG.lv
Anton Arhipov
 
PDF
I can't believe it's not a queue: Kafka and Spring
Joe Kutner
 
ODP
The craft of meta programming on JVM
Igor Khotin
 
PDF
Programming JVM Bytecode with Jitescript
Joe Kutner
 
PDF
Jvm internals
Luiz Fernando Teston
 
KEY
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Charles Nutter
 
PDF
Invokedynamic in 45 Minutes
Charles Nutter
 
PDF
Ruby Performance - The Last Mile - RubyConf India 2016
Charles Nutter
 
PDF
Bytecode manipulation with Javassist and ASM
ashleypuls
 
ODP
Method Handles in Java
hendersk
 
PDF
Jvm internals 2015 - CorkJUG
Luiz Fernando Teston
 
PDF
Introduction to the Java bytecode - So@t - 20130924
yohanbeschi
 
PDF
Threads and Java Memory Model Explained
Luiz Fernando Teston
 
PDF
JPoint 2016 - Etudes of DIY Java profiler
Anton Arhipov
 
PPTX
Mastering java bytecode with ASM - GeeCON 2012
Anton Arhipov
 
PPTX
Java byte code & virtual machine
Laxman Puri
 
PDF
JVM Internals - Garbage Collection & Runtime Optimizations
Doug Hawkins
 
PPTX
Understanding Java byte code and the class file format
Rafael Winterhalter
 
PPTX
The Java memory model made easy
Rafael Winterhalter
 
PPTX
Java bytecode and classes
yoavwix
 
Java Bytecode Fundamentals - JUG.lv
Anton Arhipov
 
I can't believe it's not a queue: Kafka and Spring
Joe Kutner
 
The craft of meta programming on JVM
Igor Khotin
 
Programming JVM Bytecode with Jitescript
Joe Kutner
 
Jvm internals
Luiz Fernando Teston
 
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Charles Nutter
 
Invokedynamic in 45 Minutes
Charles Nutter
 
Ruby Performance - The Last Mile - RubyConf India 2016
Charles Nutter
 
Bytecode manipulation with Javassist and ASM
ashleypuls
 
Method Handles in Java
hendersk
 
Jvm internals 2015 - CorkJUG
Luiz Fernando Teston
 
Introduction to the Java bytecode - So@t - 20130924
yohanbeschi
 
Threads and Java Memory Model Explained
Luiz Fernando Teston
 
JPoint 2016 - Etudes of DIY Java profiler
Anton Arhipov
 
Mastering java bytecode with ASM - GeeCON 2012
Anton Arhipov
 
Java byte code & virtual machine
Laxman Puri
 
JVM Internals - Garbage Collection & Runtime Optimizations
Doug Hawkins
 
Understanding Java byte code and the class file format
Rafael Winterhalter
 
The Java memory model made easy
Rafael Winterhalter
 
Java bytecode and classes
yoavwix
 
Ad

Similar to Eclipse Day India 2015 - Java bytecode analysis and JIT (20)

PDF
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
Vladimir Ivanov
 
PDF
JVM JIT-compiler overview @ JavaOne Moscow 2013
Vladimir Ivanov
 
PDF
Владимир Иванов. JIT для Java разработчиков
Volha Banadyseva
 
PDF
JVM JIT compilation overview by Vladimir Ivanov
ZeroTurnaround
 
PPTX
Java performance tuning
Jerry Kurian
 
PDF
10 Reasons Why Java Now Rocks More Than Ever
Geert Bevin
 
PPT
1- java
Krishna Sujeer
 
PPTX
Dalvik jit
Srinivas Kothuri
 
PDF
Lifecycle of a JIT compiled code
J On The Beach
 
PPTX
JVM Memory Model - Yoav Abrahami, Wix
Codemotion Tel Aviv
 
PDF
Challenges of High-performance Language Virtual Machines
Clément Béra
 
PPTX
JIT vs. AOT: Unity And Conflict of Dynamic and Static Compilers
Nikita Lipsky
 
PDF
Seminar.2009.Performance.Intro
roialdaag
 
PPT
Java Virtual Machine
Taha Malampatti
 
PDF
Java Runtime: повседневные обязанности JVM
odnoklassniki.ru
 
PPTX
Cloud Native Compiler
Simon Ritter
 
PDF
43
srimoorthi
 
PDF
Optimizing Java Chris Newland James Gough Benjamin J Evans
lenicikeally
 
PDF
Performance van Java 8 en verder - Jeroen Borgers
NLJUG
 
PDF
Introduction to JVM JIT Optimizations
diegosoftware
 
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
Vladimir Ivanov
 
JVM JIT-compiler overview @ JavaOne Moscow 2013
Vladimir Ivanov
 
Владимир Иванов. JIT для Java разработчиков
Volha Banadyseva
 
JVM JIT compilation overview by Vladimir Ivanov
ZeroTurnaround
 
Java performance tuning
Jerry Kurian
 
10 Reasons Why Java Now Rocks More Than Ever
Geert Bevin
 
Dalvik jit
Srinivas Kothuri
 
Lifecycle of a JIT compiled code
J On The Beach
 
JVM Memory Model - Yoav Abrahami, Wix
Codemotion Tel Aviv
 
Challenges of High-performance Language Virtual Machines
Clément Béra
 
JIT vs. AOT: Unity And Conflict of Dynamic and Static Compilers
Nikita Lipsky
 
Seminar.2009.Performance.Intro
roialdaag
 
Java Virtual Machine
Taha Malampatti
 
Java Runtime: повседневные обязанности JVM
odnoklassniki.ru
 
Cloud Native Compiler
Simon Ritter
 
Optimizing Java Chris Newland James Gough Benjamin J Evans
lenicikeally
 
Performance van Java 8 en verder - Jeroen Borgers
NLJUG
 
Introduction to JVM JIT Optimizations
diegosoftware
 
Ad

More from Eclipse Day India (20)

PPTX
Java Performance Testing for Everyone - Shelley Lambert
Eclipse Day India
 
PDF
Eclipse IDE Tips and Tricks - Lakshmi Priya Shanmugam
Eclipse Day India
 
PDF
Pattern Matching in Java - Srikanth Sankaran
Eclipse Day India
 
PDF
Machine Learning for Java Developers - Nasser Ebrahim
Eclipse Day India
 
PPTX
Scaling Eclipse on HiDPI Monitors - Niraj Modi
Eclipse Day India
 
PPTX
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Eclipse Day India
 
PDF
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Eclipse Day India
 
PDF
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India
 
PPTX
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India
 
ODP
Eclipse Day India 2015 - Java 9
Eclipse Day India
 
PDF
Eclipse Day India 2015 - Keynote - Stephan Herrmann
Eclipse Day India
 
PDF
Eclipse Day India 2015 - Eclipse RCP testing using Jubula based automation
Eclipse Day India
 
PDF
Eclipse Day India 2015 - Oomph
Eclipse Day India
 
PDF
Eclipse Day India 2015 - Keynote (Mike Milinkovich)
Eclipse Day India
 
PDF
Eclipse Day India 2015 - Unleashing the Java 8 Tooling in Eclipse
Eclipse Day India
 
PDF
IDS and Bluemix
Eclipse Day India
 
PPT
SWT - Technical Deep Dive
Eclipse Day India
 
PPTX
PDE builds or Maven
Eclipse Day India
 
PPT
Orion - IDE on the cloud
Eclipse Day India
 
Java Performance Testing for Everyone - Shelley Lambert
Eclipse Day India
 
Eclipse IDE Tips and Tricks - Lakshmi Priya Shanmugam
Eclipse Day India
 
Pattern Matching in Java - Srikanth Sankaran
Eclipse Day India
 
Machine Learning for Java Developers - Nasser Ebrahim
Eclipse Day India
 
Scaling Eclipse on HiDPI Monitors - Niraj Modi
Eclipse Day India
 
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Eclipse Day India
 
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Eclipse Day India
 
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India
 
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India
 
Eclipse Day India 2015 - Java 9
Eclipse Day India
 
Eclipse Day India 2015 - Keynote - Stephan Herrmann
Eclipse Day India
 
Eclipse Day India 2015 - Eclipse RCP testing using Jubula based automation
Eclipse Day India
 
Eclipse Day India 2015 - Oomph
Eclipse Day India
 
Eclipse Day India 2015 - Keynote (Mike Milinkovich)
Eclipse Day India
 
Eclipse Day India 2015 - Unleashing the Java 8 Tooling in Eclipse
Eclipse Day India
 
IDS and Bluemix
Eclipse Day India
 
SWT - Technical Deep Dive
Eclipse Day India
 
PDE builds or Maven
Eclipse Day India
 
Orion - IDE on the cloud
Eclipse Day India
 

Recently uploaded (20)

PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 

Eclipse Day India 2015 - Java bytecode analysis and JIT

  • 2. Java ByteCode Analysis Educate yourself with “Java Bytecode” Vaibhav Choudhary (@vaibhav_c) [email protected] http://blogs.oracle.com/vaibhav
  • 3. Agenda • Java Bytecode Basics • Be familiar with Bytecode tools • Bytecode evaluation • Language Feature vs Bytecode • Dive deeper • Changes happened in JDK7 and onwards • QA 3
  • 4. Java Bytecode Basics • Why to know Java Bytecode:- – It will help you understand your code better. – Knowing the kitchen from where food is coming – Clear the myths 4
  • 5. Java Bytecode Tools ● javap -help ● javap -c ● javap -p ● javap -verbose ● javap -c -p -verbose → Its a good option 5
  • 6. Bytecode Evaluation ● Write Once, Run Anywhere (WORA) ● Provides more safety and security – Managed env. ● Optimizations can be done at runtime ● Its hard to deal with CPU instrs. 6
  • 7. JVM Principals for Bytecode ● All operations are stack based. ● Parameters, locals – All uses stack ● Push/Pop/Dup/Dup2 ● Stack slot – 32 bit (how to handle long/double ?) ● Classes - fully qualified name 7
  • 8. Class file format 8 ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; }
  • 9. Invoke* method calls ● invokestatic – invokes class (static) method ● invokespecial - Invoke instance method; special handling for superclass, private, and instance initialization method invocations ● invokeinterface – invoke interface method ● invokevirutal - Invoke instance method; dispatch based on class 9 JDK 7 onwards - InvokeDynmaic
  • 10. Get rid of Myths ● Java don't have GOTO statements. – Why we need GOTO at first place ? ● Use StringBuilders if your string is changing. – What string concat do in java. ● How inner classes work – Do it mock on encapsulation. ● Many mores … 10
  • 11. Execution Process 11 .java .class javac Just In Time Compiler Just In Time CompilerNative CodeNative OS Java Interpreter Java Interpreter Java Virtual Machine Java Virtual Machine
  • 12. Flavors of Just In Time (JIT) Compiler • JIT comes in two flavors • C1 (Client compiler) – -client option • C2 (Server compiler) – -server option • -XX:+TieredCompilation – Better decision of compilers. • What happens if: -client -XX:+TieredCompilation 12
  • 13. JIT optimizations – Expression Optimization • Eliminate dead codes. • Expression optimization. 13 int someCalculation(int x1, int x2, int x3) { int res1 = x1+x2; int res2 = x1-x2; int res3 = x1+x3; return (res1+res2)/2; } int someCalculation(int x1, int x2, int x3) { return x1; }
  • 14. JIT Optimization – Inline Method • Inline method – Substitute body of the method (<35 bytes of JVM bytecode) – This provides the best optimization by JIT – A better inline that C++ – Reduction of function calling overhead. Something else ? 14 int compute(int var) { int result; if(var > 5) { result = computeFurther(var); } else { result = 100; } return result; } myVal = compute(3); myVal = 100;
  • 15. JIT Optimization – Caching Technique • Caching read calls – Store it for future request. – major performance impact in loops 15 Point findMid(Point p1, Point p2) { Point p; p.x = (p1.x + p2.x)/2; p.y = (p1.y + p2.y)/2; return p; ... ... x – p1.x; y – p1.y; while(!monitor.flag) …..... …..... boolean flag = monitor.flag
  • 16. JIT Optimization – Uncommon Trap • De-optimization – UncommonTrap – Scrap the optimization. 16 int compute(int var) { int result; if(var > 5) { result = computeFurther(var); } else { result = 100; } return result; } int compute(int var) { if(var > 5) uncommonTrap(); return 100; }
  • 17. JIT Optimization – Monomorphic dispatch • Class Hierarchy Analysis – Optimization decision based on assumptions. – Re-evaluate assumption when new class loaded. 17 public class Animal { private String color; public String getColor() { return color; } } myColor = animal.getColor(); public class Animal { String color; } myColor = animal.color;
  • 18. JIT Optimization – Null Checks • Implicit Null Check – Virtual machine believe code will not throw null pointer. – What if VM got a null pointer ? 18 x = point.x; y = point.y; Equivalents to: if(point==null) throw new NullPointerException(); else { x = point.x; y = point.y; } For each reference in your code, VM checks null pointer. If VM assumes that Your code will not throw NPE, it will remove the check. If happened, de-optimize. Handle the SEGV.
  • 19. JIT Optimization – Multi-threading Env. • Eliminate locks if monitor is not reachable from other threads • Join adjacent synchronized blocks on the same object • Do check options: – XX:+UseBiasedLocking 19
  • 20. JIT Optimization – Loop Optimization • Combining loops – Two loops can be combined if taking equivalent time. • Inversion loops – Change while into do-while. • Tiling loops – Re-organize loop so that it will fix in cache. 20
  • 21. JVM Options • Xint – Interpreter mode • Xcomp – Compiled mode • Xmixed – Interpreter + Compiler • -server → C2 compiler • -client → C1 compiler • -XX:+TieredCompilation → C1 + C2 (used by 32/64 bit mode) • -d32/-d64 options 21
  • 22. Benchmarking Difficulties • Garbage collect can distort benchmarking. • Benchmarking can be distorted by unintentionally dead code • Improper warm-up can distort benchmarking. • Dynamic De-optimziation can distort benchmarking. • Momomorphic call can distort benchmarking. • Inlining of code can distort benchmarking. 22
  • 23. Logging Options • -XX:+UnlockDiagnosticVMOptions • -XX:+LogCompilation • -XX:LogFile=<path to file> • -XX:MaxInlineSize=<size> • -XX:FreqInlineSize=<size> • Many more ... 23
  • 24. References • Subscribe with - http://www.javaperformancetuning.com/ • Java Performance Documentation - Link • Oracle JIT Compilation Documentation - Link • Understand JIT Compilation with JITWatch - Link • My Blog – http://blogs.oracle.com/vaibhav 24