SlideShare a Scribd company logo
1 of 29
DIY Java Profiling
Roman Elizarov / Роман Елизаров
Devexperts / Эксперт-Система
elizarov at devexperts dot com
Profiling
“Profiling … is the investigation of a program's
behavior using information gathered as the program
executes. The usual purpose of this analysis is to
determine which sections of a program to optimize -
to increase its overall speed, decrease its memory
requirement or sometimes both.”
-- Wikipedia
OptimizeProfile
Why Do-It-Yourself?
What are the problems with tools?
• When you cannot run 3rd party code in
production/live environment
• Reliability concerns
• Compliance concerns
• Tools are often opaque (even if open source)
• In their performance effect
• In their means of operation
• Tools have their learning curve
• While DIY is Fun!
Yes, we work for financial industry
Learning curve?
Learning a tool:
• Pays off if you use it often
• Pays off if it gets you results faster/better
• It is good to know modern tools to avoid NIH syndrome
DIY for knowledge reuse:
• Apply your existing knowledge
• Expand and deepen your existing knowledge
• Know your day-to-day tools (like Java VM) better
Why Java?
Top language since 2001 (TIOBE)
Great for enterprise applications
• Write front & back in the same language
• share code and libraries between them
• Run everywhere
• Windows, Mac OS (typical for front)
• Linux, Solaris (typical for back)
Managed language – makes it easy to profile
Agenda: Java DIY Approaches
Just code it in Java
• Standard Java classes are your friends
Know your JVM features
• -X… and -XX:… JVM options are your friends
Use bytecode manipulation
• Java Virtual Machine specification is your friend
The knowledge of all the above gets
you more that just profiling!
Agenda: Profiling types
Profiling
CPU
Times/Calls Sampling
Memory
Usage Allocation
CPU profiling: Wall clock time/Calls
Account getAccount(AccountKey key) {
long startTime = System.currentTimeMillis();
checkAccountPermission(key);
Account account = AccountCache.lookupAccount(key);
if (account != null) {
Profiler.record(“getAccount.cached”,
System.currentTimeMillis() - startTime);
return account;
}
account = AccountDAO.loadAccount(key);
AccountCache.putAccount(account);
Profiler.record(“getAccount.loaded”,
System.currentTimeMillis() - startTime);
return account;
}
Straight in code
Goes to DB, slow
CPU profiling: Wall clock time/Calls
Profiler class implementation can be as simple
as concurrent map
• Maps string keys to any stats you want
• Total number of calls, total time, max time
• Easy to compute avg time
• Can store histograms and compute percentiles
• Periodically dump stats to console/logs
• Report stats via JMX, HTTP, or <insert approach
that you use in your project>
CPU profiling: Wall clock time/Calls
When to use
• Relatively “big” business methods
• Where number of invocations per second are under
1000s and time per invocation is measured in ms.
• If you need to know the number of calls and the
actual (wall clock) time spent in the method
• If you need to trace different execution paths
• If you need to integrate profiling into your code as
“always on” feature
Shorter/faster methods?
CPU Profiling: Short/Fast Calls
static final AtomicLong lookupAccountCalls =
new AtomicLong();
Account lookupAccount(AccoutKey key) {
lookupAccountCalls.incrementAndGet();
return accountByKey.get(key);
}
Just count
Way under 1ms
CPU Profiling: Short/Fast Calls
When to use
• If number of calls is in the order of 10k per second
• If you don’t need to measure time spent
• Counting distorts time for very short methods
• Attempt to measure time distorts it even more
• To really measure time go native with rdtsc on x86
Solution for 100k+ calls per second?
• Sampling!
CPU Profiling: Sampling
JVM has ability to produce “thread dump”
• Press Ctrl+Break in Windows console
• “kill -3 <pid>” on Linux/Solaris
If program spends most of its time on one line:
double[][] multiply(double[][] a, double [][] b) {
int n = a.length, r = a[0].length, m = b[0].length;
double[][] c = new double[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
for (int k = 0; k < r; k++)
c[i][j] += a[i][k] * b[k][j];
return c;
}
Hotspot
CPU Profiling: Sampling
You get something like this on the console:
Full thread dump Java … <JVM version info>
<other threads here>
"main" prio=6 tid=0x006e9c00 nid=0x18d8 runnable
java.lang.Thread.State: RUNNABLE
at YourClass.multiply(YouClass.java:<lineno>)
at <the context of the call> …
CPU Profiling: Sampling
Hotspot – is where the most of CPU is spent
Next time you need to find hotspot
• Don’t reach for profiling tools
• Just try a single thread dump first
• Multiple thread dumps will help you verify it
You can use “jstack <pid>”
• Gets more detailed info about native methods
with “-m” option on Solaris
CPU Profiling: More thread dumps
More ideas
• Redirect output to a file
• Use a script to do “kill -3” every 3 seconds
• Minimal impact on system stability (TD is well tested)
• Write a simple code to parse resulting file
• Count a number of occurrences of certain methods
• Analyze traces to get better data than any 3rd party tool
– Figure what methods block going to DB or Network
– Figure what methods block synchronizations
– Figure out what you need to know
CPU Profiling: Integration
You can get “thread dump” programmatically:
• See Thread.getAllStackTraces
• Or Thread.getStackTrace
– If you’re interested in a particular one, like Swing EDT
• Great and lean way to integrate “always on”
profiling into end-user Java application or server
CPU Profiling: Caveats
Thread dumps stop JVM at “safe point”
• You get a point of the nearest safepoint
• Not necessarily the hotspot itself
The work-around: Native Profiling
• Works via undocumented “async threadump”
• Hard to get from inside of Java (need native code)
• That’s where you’d rather use tool like Intel VTune,
AMD CodeAnalyst, Oracle Solaris Studio Performance
Analyzer
Memory usage profiling
Use “jmap –histo <pid>”
• Use “jps” to find pids of your java processes
• You get something like this:
num #instances #bytes class name
----------------------------------------------
1: 772 115768 [C
2: 8 72664 [I
3: 77 39576 [B
4: 575 13800 java.lang.String
… <etc>
char[], top consumer
Total number of bytes consumed
Memory usage profiling caveats
You get all objects in heap
• Including garbage
• Can make a big difference
• Use “jmap -histo:live <pid>”
• Will do GC before collecting histogram
– Slow, the process will be suspended
• Will work only on live process (as GC needs safepoint)
You don’t know where allocation was made
• On fast & DIY solution to this problem later
More useful JVM options
-XX:+PrintClassHistogram
• on Ctrl-Break or “kill -3” gets “jmap -histo”
-XX:+HeapDumpOnOutOfMemoryError
• Produces dump in hprof format
• You can use tools offline on the resulting file
• No need to integrate 3rd party tools into live JVM
• But still get many of the benefits of modern tools
• Other ways to get HeapDump:
• Use “jmap –dump:<options> <pid>”
• Use HotSpotDiagnostic MBean
– Right from Java via JMX
Memory allocation profiling
You will not see “new MyClass” as a hotspot
• But it will eat your CPU time
• Because time will be spent collecting garbage
Figure out how much you spend in GC
• Use the following options
• -verbose:gc or -XX:+PrintGC or -XX:+PrintGCDetails
• -XX:+PrintGCTimeStamps
• Worry if you spend a lot
Memory allocation profiling
Use “-Xaprof” option in your JVM
• Prints something like this on process termination:
Allocation profile (sizes in bytes, cutoff = 0 bytes):
___________Size__Instances__Average__Class________________
555807584 34737974 16 java.lang.Integer
321112 5844 55 [I
106104 644 165 [C
37144 63 590 [B
13744 325 42 [Ljava.lang.Object;
… <the rest>
Top alloc’d
Memory allocation profile
But where is it allocated?
• If you have a clue – just add counting via
AtomicLong in the suspect places
• If you don’t have a clue… just add it everywhere
• Using aspect-oriented programming
• Using bytecode manipulation More DIY style
Bytecode manipulation
Change bytecode instead of source code for all
your profiling needs
• Counting, time measuring
• Decouples profiling from code logic
• Great if you don’t need it always on
• Can do it ahead-of-time and on-the-fly
• Great for tasks like “profile each place of code
where new XXX is invoked”
Bytecode manipulation
ObjectWeb ASM is an open source lib to help
• Easy to use for bytecode manipulation
• Extremely fast (suited to on-the-fly manipulation)
ClassReader ClassVisitor ClassWriter
MethodVisitor
Bytecode manipulation with ASM
class AClassVisitor extends ClassAdapter {
public MethodVisitor visitMethod(…) {
return new AMethodVisitor(super.visitMethod(…))
}
}
class AMethodVisitor extends MethodAdapter {
public void visitIntInsn(int opcode, int operand) {
super.visitIntInsn(opcode, operand);
if (opcode == NEWARRAY) {
// add new instructions here into this
// point of class file… Will even preserve
// original source code line numbers
}
}
}
To trace each array allocation
On-the-fly bytecode manipulation
Use java.lang.instrument package
Use “-javaagent:<jarfile>” JVM option
• Will run “premain” method in “Premain-Class”
from jar file’s manifest
• Will provide an instance of Instrumentation
• It lets you install system-wide ClassFileTransformer
– That transforms even system classes!
• It has other useful methods like getObjectSize
Conclusion
Questions?
Know bytecode
Know JVM options
Know Java libraries

More Related Content

What's hot

JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesCharles Nutter
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
An Introduction To Java Profiling
An Introduction To Java ProfilingAn Introduction To Java Profiling
An Introduction To Java Profilingschlebu
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuningosa_ora
 
Effective testing for spark programs Strata NY 2015
Effective testing for spark programs   Strata NY 2015Effective testing for spark programs   Strata NY 2015
Effective testing for spark programs Strata NY 2015Holden Karau
 
Profiling & Testing with Spark
Profiling & Testing with SparkProfiling & Testing with Spark
Profiling & Testing with SparkRoger Rafanell Mas
 
Lockless Programming GDC 09
Lockless Programming GDC 09Lockless Programming GDC 09
Lockless Programming GDC 09Lee Hanxue
 
JProfiler8 @ OVIRT
JProfiler8 @ OVIRTJProfiler8 @ OVIRT
JProfiler8 @ OVIRTLiran Zelkha
 
Coding for multiple cores
Coding for multiple coresCoding for multiple cores
Coding for multiple coresLee Hanxue
 
iOS Multithreading
iOS MultithreadingiOS Multithreading
iOS MultithreadingRicha Jain
 
Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsCarol McDonald
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]Kuba Břečka
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunningguest1f2740
 
Application Profiling for Memory and Performance
Application Profiling for Memory and PerformanceApplication Profiling for Memory and Performance
Application Profiling for Memory and Performancepradeepfn
 
The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018Charles Nutter
 
Java Performance Monitoring & Tuning
Java Performance Monitoring & TuningJava Performance Monitoring & Tuning
Java Performance Monitoring & TuningMuhammed Shakir
 
Introduction to TPL
Introduction to TPLIntroduction to TPL
Introduction to TPLGyuwon Yi
 
Fast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaFast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaCharles Nutter
 

What's hot (20)

JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for Dummies
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
An Introduction To Java Profiling
An Introduction To Java ProfilingAn Introduction To Java Profiling
An Introduction To Java Profiling
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuning
 
Effective testing for spark programs Strata NY 2015
Effective testing for spark programs   Strata NY 2015Effective testing for spark programs   Strata NY 2015
Effective testing for spark programs Strata NY 2015
 
Profiling & Testing with Spark
Profiling & Testing with SparkProfiling & Testing with Spark
Profiling & Testing with Spark
 
Lockless Programming GDC 09
Lockless Programming GDC 09Lockless Programming GDC 09
Lockless Programming GDC 09
 
JProfiler8 @ OVIRT
JProfiler8 @ OVIRTJProfiler8 @ OVIRT
JProfiler8 @ OVIRT
 
Coding for multiple cores
Coding for multiple coresCoding for multiple cores
Coding for multiple cores
 
iOS Multithreading
iOS MultithreadingiOS Multithreading
iOS Multithreading
 
The Java Memory Model
The Java Memory ModelThe Java Memory Model
The Java Memory Model
 
Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and Trends
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunning
 
Application Profiling for Memory and Performance
Application Profiling for Memory and PerformanceApplication Profiling for Memory and Performance
Application Profiling for Memory and Performance
 
The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018
 
Down the Rabbit Hole
Down the Rabbit HoleDown the Rabbit Hole
Down the Rabbit Hole
 
Java Performance Monitoring & Tuning
Java Performance Monitoring & TuningJava Performance Monitoring & Tuning
Java Performance Monitoring & Tuning
 
Introduction to TPL
Introduction to TPLIntroduction to TPL
Introduction to TPL
 
Fast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaFast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible Java
 

Similar to DIY Java Profiling

Building source code level profiler for C++.pdf
Building source code level profiler for C++.pdfBuilding source code level profiler for C++.pdf
Building source code level profiler for C++.pdfssuser28de9e
 
Tips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native codeTips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native codeKenneth Geisshirt
 
High Performance With Java
High Performance With JavaHigh Performance With Java
High Performance With Javamalduarte
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchMats Bryntse
 
Building High Performance Android Applications in Java and C++
Building High Performance Android Applications in Java and C++Building High Performance Android Applications in Java and C++
Building High Performance Android Applications in Java and C++Kenneth Geisshirt
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 
The Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance TuningThe Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance TuningjClarity
 
The basics of hacking and penetration testing 이제 시작이야 해킹과 침투 테스트 kenneth.s.kwon
The basics of hacking and penetration testing 이제 시작이야 해킹과 침투 테스트 kenneth.s.kwonThe basics of hacking and penetration testing 이제 시작이야 해킹과 침투 테스트 kenneth.s.kwon
The basics of hacking and penetration testing 이제 시작이야 해킹과 침투 테스트 kenneth.s.kwonKenneth Kwon
 
Typhoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitTyphoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitDimitry Snezhkov
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profilerIhor Bobak
 
MFF UK - Introduction to iOS
MFF UK - Introduction to iOSMFF UK - Introduction to iOS
MFF UK - Introduction to iOSPetr Dvorak
 
Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.CocoaHeads France
 
Optimizing mobile applications - Ian Dundore, Mark Harkness
Optimizing mobile applications - Ian Dundore, Mark HarknessOptimizing mobile applications - Ian Dundore, Mark Harkness
Optimizing mobile applications - Ian Dundore, Mark Harknessozlael ozlael
 
¡El mejor lenguaje para automatizar pruebas!
¡El mejor lenguaje para automatizar pruebas!¡El mejor lenguaje para automatizar pruebas!
¡El mejor lenguaje para automatizar pruebas!Antonio Robres Turon
 
Smart Data Conference: DL4J and DataVec
Smart Data Conference: DL4J and DataVecSmart Data Conference: DL4J and DataVec
Smart Data Conference: DL4J and DataVecJosh Patterson
 
Jvm profiling under the hood
Jvm profiling under the hoodJvm profiling under the hood
Jvm profiling under the hoodRichardWarburton
 
Sista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceSista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceESUG
 

Similar to DIY Java Profiling (20)

Building source code level profiler for C++.pdf
Building source code level profiler for C++.pdfBuilding source code level profiler for C++.pdf
Building source code level profiler for C++.pdf
 
Tips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native codeTips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native code
 
High Performance With Java
High Performance With JavaHigh Performance With Java
High Performance With Java
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
 
Building High Performance Android Applications in Java and C++
Building High Performance Android Applications in Java and C++Building High Performance Android Applications in Java and C++
Building High Performance Android Applications in Java and C++
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
The Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance TuningThe Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance Tuning
 
.NET Debugging Workshop
.NET Debugging Workshop.NET Debugging Workshop
.NET Debugging Workshop
 
The basics of hacking and penetration testing 이제 시작이야 해킹과 침투 테스트 kenneth.s.kwon
The basics of hacking and penetration testing 이제 시작이야 해킹과 침투 테스트 kenneth.s.kwonThe basics of hacking and penetration testing 이제 시작이야 해킹과 침투 테스트 kenneth.s.kwon
The basics of hacking and penetration testing 이제 시작이야 해킹과 침투 테스트 kenneth.s.kwon
 
Typhoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitTyphoon Managed Execution Toolkit
Typhoon Managed Execution Toolkit
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profiler
 
MFF UK - Introduction to iOS
MFF UK - Introduction to iOSMFF UK - Introduction to iOS
MFF UK - Introduction to iOS
 
Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.
 
0507 057 01 98 * Adana Cukurova Klima Servisleri
0507 057 01 98 * Adana Cukurova Klima Servisleri0507 057 01 98 * Adana Cukurova Klima Servisleri
0507 057 01 98 * Adana Cukurova Klima Servisleri
 
Optimizing mobile applications - Ian Dundore, Mark Harkness
Optimizing mobile applications - Ian Dundore, Mark HarknessOptimizing mobile applications - Ian Dundore, Mark Harkness
Optimizing mobile applications - Ian Dundore, Mark Harkness
 
¡El mejor lenguaje para automatizar pruebas!
¡El mejor lenguaje para automatizar pruebas!¡El mejor lenguaje para automatizar pruebas!
¡El mejor lenguaje para automatizar pruebas!
 
Smart Data Conference: DL4J and DataVec
Smart Data Conference: DL4J and DataVecSmart Data Conference: DL4J and DataVec
Smart Data Conference: DL4J and DataVec
 
Jvm profiling under the hood
Jvm profiling under the hoodJvm profiling under the hood
Jvm profiling under the hood
 
Sista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceSista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performance
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 

More from Roman Elizarov

Kotlin Coroutines in Practice @ KotlinConf 2018
Kotlin Coroutines in Practice @ KotlinConf 2018Kotlin Coroutines in Practice @ KotlinConf 2018
Kotlin Coroutines in Practice @ KotlinConf 2018Roman Elizarov
 
Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017Roman Elizarov
 
Introduction to Coroutines @ KotlinConf 2017
Introduction to Coroutines @ KotlinConf 2017Introduction to Coroutines @ KotlinConf 2017
Introduction to Coroutines @ KotlinConf 2017Roman Elizarov
 
Fresh Async with Kotlin @ QConSF 2017
Fresh Async with Kotlin @ QConSF 2017Fresh Async with Kotlin @ QConSF 2017
Fresh Async with Kotlin @ QConSF 2017Roman Elizarov
 
Scale Up with Lock-Free Algorithms @ JavaOne
Scale Up with Lock-Free Algorithms @ JavaOneScale Up with Lock-Free Algorithms @ JavaOne
Scale Up with Lock-Free Algorithms @ JavaOneRoman Elizarov
 
Kotlin Coroutines Reloaded
Kotlin Coroutines ReloadedKotlin Coroutines Reloaded
Kotlin Coroutines ReloadedRoman Elizarov
 
Lock-free algorithms for Kotlin Coroutines
Lock-free algorithms for Kotlin CoroutinesLock-free algorithms for Kotlin Coroutines
Lock-free algorithms for Kotlin CoroutinesRoman Elizarov
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutinesRoman Elizarov
 
ACM ICPC 2016 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2016 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2016 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2016 NEERC (Northeastern European Regional Contest) Problems ReviewRoman Elizarov
 
Многопоточное Программирование - Теория и Практика
Многопоточное Программирование - Теория и ПрактикаМногопоточное Программирование - Теория и Практика
Многопоточное Программирование - Теория и ПрактикаRoman Elizarov
 
ACM ICPC 2015 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2015 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2015 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2015 NEERC (Northeastern European Regional Contest) Problems ReviewRoman Elizarov
 
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems ReviewRoman Elizarov
 
Многопоточные Алгоритмы (для BitByte 2014)
Многопоточные Алгоритмы (для BitByte 2014)Многопоточные Алгоритмы (для BitByte 2014)
Многопоточные Алгоритмы (для BitByte 2014)Roman Elizarov
 
Теоретический минимум для понимания Java Memory Model (для JPoint 2014)
Теоретический минимум для понимания Java Memory Model (для JPoint 2014)Теоретический минимум для понимания Java Memory Model (для JPoint 2014)
Теоретический минимум для понимания Java Memory Model (для JPoint 2014)Roman Elizarov
 
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems ReviewRoman Elizarov
 
ACM ICPC 2012 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2012 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2012 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2012 NEERC (Northeastern European Regional Contest) Problems ReviewRoman Elizarov
 
The theory of concurrent programming for a seasoned programmer
The theory of concurrent programming for a seasoned programmerThe theory of concurrent programming for a seasoned programmer
The theory of concurrent programming for a seasoned programmerRoman Elizarov
 
Пишем самый быстрый хеш для кэширования данных
Пишем самый быстрый хеш для кэширования данныхПишем самый быстрый хеш для кэширования данных
Пишем самый быстрый хеш для кэширования данныхRoman Elizarov
 

More from Roman Elizarov (18)

Kotlin Coroutines in Practice @ KotlinConf 2018
Kotlin Coroutines in Practice @ KotlinConf 2018Kotlin Coroutines in Practice @ KotlinConf 2018
Kotlin Coroutines in Practice @ KotlinConf 2018
 
Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017
 
Introduction to Coroutines @ KotlinConf 2017
Introduction to Coroutines @ KotlinConf 2017Introduction to Coroutines @ KotlinConf 2017
Introduction to Coroutines @ KotlinConf 2017
 
Fresh Async with Kotlin @ QConSF 2017
Fresh Async with Kotlin @ QConSF 2017Fresh Async with Kotlin @ QConSF 2017
Fresh Async with Kotlin @ QConSF 2017
 
Scale Up with Lock-Free Algorithms @ JavaOne
Scale Up with Lock-Free Algorithms @ JavaOneScale Up with Lock-Free Algorithms @ JavaOne
Scale Up with Lock-Free Algorithms @ JavaOne
 
Kotlin Coroutines Reloaded
Kotlin Coroutines ReloadedKotlin Coroutines Reloaded
Kotlin Coroutines Reloaded
 
Lock-free algorithms for Kotlin Coroutines
Lock-free algorithms for Kotlin CoroutinesLock-free algorithms for Kotlin Coroutines
Lock-free algorithms for Kotlin Coroutines
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutines
 
ACM ICPC 2016 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2016 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2016 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2016 NEERC (Northeastern European Regional Contest) Problems Review
 
Многопоточное Программирование - Теория и Практика
Многопоточное Программирование - Теория и ПрактикаМногопоточное Программирование - Теория и Практика
Многопоточное Программирование - Теория и Практика
 
ACM ICPC 2015 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2015 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2015 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2015 NEERC (Northeastern European Regional Contest) Problems Review
 
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
 
Многопоточные Алгоритмы (для BitByte 2014)
Многопоточные Алгоритмы (для BitByte 2014)Многопоточные Алгоритмы (для BitByte 2014)
Многопоточные Алгоритмы (для BitByte 2014)
 
Теоретический минимум для понимания Java Memory Model (для JPoint 2014)
Теоретический минимум для понимания Java Memory Model (для JPoint 2014)Теоретический минимум для понимания Java Memory Model (для JPoint 2014)
Теоретический минимум для понимания Java Memory Model (для JPoint 2014)
 
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
 
ACM ICPC 2012 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2012 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2012 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2012 NEERC (Northeastern European Regional Contest) Problems Review
 
The theory of concurrent programming for a seasoned programmer
The theory of concurrent programming for a seasoned programmerThe theory of concurrent programming for a seasoned programmer
The theory of concurrent programming for a seasoned programmer
 
Пишем самый быстрый хеш для кэширования данных
Пишем самый быстрый хеш для кэширования данныхПишем самый быстрый хеш для кэширования данных
Пишем самый быстрый хеш для кэширования данных
 

Recently uploaded

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 

Recently uploaded (20)

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 

DIY Java Profiling

  • 1. DIY Java Profiling Roman Elizarov / Роман Елизаров Devexperts / Эксперт-Система elizarov at devexperts dot com
  • 2. Profiling “Profiling … is the investigation of a program's behavior using information gathered as the program executes. The usual purpose of this analysis is to determine which sections of a program to optimize - to increase its overall speed, decrease its memory requirement or sometimes both.” -- Wikipedia OptimizeProfile
  • 3. Why Do-It-Yourself? What are the problems with tools? • When you cannot run 3rd party code in production/live environment • Reliability concerns • Compliance concerns • Tools are often opaque (even if open source) • In their performance effect • In their means of operation • Tools have their learning curve • While DIY is Fun! Yes, we work for financial industry
  • 4. Learning curve? Learning a tool: • Pays off if you use it often • Pays off if it gets you results faster/better • It is good to know modern tools to avoid NIH syndrome DIY for knowledge reuse: • Apply your existing knowledge • Expand and deepen your existing knowledge • Know your day-to-day tools (like Java VM) better
  • 5. Why Java? Top language since 2001 (TIOBE) Great for enterprise applications • Write front & back in the same language • share code and libraries between them • Run everywhere • Windows, Mac OS (typical for front) • Linux, Solaris (typical for back) Managed language – makes it easy to profile
  • 6. Agenda: Java DIY Approaches Just code it in Java • Standard Java classes are your friends Know your JVM features • -X… and -XX:… JVM options are your friends Use bytecode manipulation • Java Virtual Machine specification is your friend The knowledge of all the above gets you more that just profiling!
  • 7. Agenda: Profiling types Profiling CPU Times/Calls Sampling Memory Usage Allocation
  • 8. CPU profiling: Wall clock time/Calls Account getAccount(AccountKey key) { long startTime = System.currentTimeMillis(); checkAccountPermission(key); Account account = AccountCache.lookupAccount(key); if (account != null) { Profiler.record(“getAccount.cached”, System.currentTimeMillis() - startTime); return account; } account = AccountDAO.loadAccount(key); AccountCache.putAccount(account); Profiler.record(“getAccount.loaded”, System.currentTimeMillis() - startTime); return account; } Straight in code Goes to DB, slow
  • 9. CPU profiling: Wall clock time/Calls Profiler class implementation can be as simple as concurrent map • Maps string keys to any stats you want • Total number of calls, total time, max time • Easy to compute avg time • Can store histograms and compute percentiles • Periodically dump stats to console/logs • Report stats via JMX, HTTP, or <insert approach that you use in your project>
  • 10. CPU profiling: Wall clock time/Calls When to use • Relatively “big” business methods • Where number of invocations per second are under 1000s and time per invocation is measured in ms. • If you need to know the number of calls and the actual (wall clock) time spent in the method • If you need to trace different execution paths • If you need to integrate profiling into your code as “always on” feature Shorter/faster methods?
  • 11. CPU Profiling: Short/Fast Calls static final AtomicLong lookupAccountCalls = new AtomicLong(); Account lookupAccount(AccoutKey key) { lookupAccountCalls.incrementAndGet(); return accountByKey.get(key); } Just count Way under 1ms
  • 12. CPU Profiling: Short/Fast Calls When to use • If number of calls is in the order of 10k per second • If you don’t need to measure time spent • Counting distorts time for very short methods • Attempt to measure time distorts it even more • To really measure time go native with rdtsc on x86 Solution for 100k+ calls per second? • Sampling!
  • 13. CPU Profiling: Sampling JVM has ability to produce “thread dump” • Press Ctrl+Break in Windows console • “kill -3 <pid>” on Linux/Solaris If program spends most of its time on one line: double[][] multiply(double[][] a, double [][] b) { int n = a.length, r = a[0].length, m = b[0].length; double[][] c = new double[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) for (int k = 0; k < r; k++) c[i][j] += a[i][k] * b[k][j]; return c; } Hotspot
  • 14. CPU Profiling: Sampling You get something like this on the console: Full thread dump Java … <JVM version info> <other threads here> "main" prio=6 tid=0x006e9c00 nid=0x18d8 runnable java.lang.Thread.State: RUNNABLE at YourClass.multiply(YouClass.java:<lineno>) at <the context of the call> …
  • 15. CPU Profiling: Sampling Hotspot – is where the most of CPU is spent Next time you need to find hotspot • Don’t reach for profiling tools • Just try a single thread dump first • Multiple thread dumps will help you verify it You can use “jstack <pid>” • Gets more detailed info about native methods with “-m” option on Solaris
  • 16. CPU Profiling: More thread dumps More ideas • Redirect output to a file • Use a script to do “kill -3” every 3 seconds • Minimal impact on system stability (TD is well tested) • Write a simple code to parse resulting file • Count a number of occurrences of certain methods • Analyze traces to get better data than any 3rd party tool – Figure what methods block going to DB or Network – Figure what methods block synchronizations – Figure out what you need to know
  • 17. CPU Profiling: Integration You can get “thread dump” programmatically: • See Thread.getAllStackTraces • Or Thread.getStackTrace – If you’re interested in a particular one, like Swing EDT • Great and lean way to integrate “always on” profiling into end-user Java application or server
  • 18. CPU Profiling: Caveats Thread dumps stop JVM at “safe point” • You get a point of the nearest safepoint • Not necessarily the hotspot itself The work-around: Native Profiling • Works via undocumented “async threadump” • Hard to get from inside of Java (need native code) • That’s where you’d rather use tool like Intel VTune, AMD CodeAnalyst, Oracle Solaris Studio Performance Analyzer
  • 19. Memory usage profiling Use “jmap –histo <pid>” • Use “jps” to find pids of your java processes • You get something like this: num #instances #bytes class name ---------------------------------------------- 1: 772 115768 [C 2: 8 72664 [I 3: 77 39576 [B 4: 575 13800 java.lang.String … <etc> char[], top consumer Total number of bytes consumed
  • 20. Memory usage profiling caveats You get all objects in heap • Including garbage • Can make a big difference • Use “jmap -histo:live <pid>” • Will do GC before collecting histogram – Slow, the process will be suspended • Will work only on live process (as GC needs safepoint) You don’t know where allocation was made • On fast & DIY solution to this problem later
  • 21. More useful JVM options -XX:+PrintClassHistogram • on Ctrl-Break or “kill -3” gets “jmap -histo” -XX:+HeapDumpOnOutOfMemoryError • Produces dump in hprof format • You can use tools offline on the resulting file • No need to integrate 3rd party tools into live JVM • But still get many of the benefits of modern tools • Other ways to get HeapDump: • Use “jmap –dump:<options> <pid>” • Use HotSpotDiagnostic MBean – Right from Java via JMX
  • 22. Memory allocation profiling You will not see “new MyClass” as a hotspot • But it will eat your CPU time • Because time will be spent collecting garbage Figure out how much you spend in GC • Use the following options • -verbose:gc or -XX:+PrintGC or -XX:+PrintGCDetails • -XX:+PrintGCTimeStamps • Worry if you spend a lot
  • 23. Memory allocation profiling Use “-Xaprof” option in your JVM • Prints something like this on process termination: Allocation profile (sizes in bytes, cutoff = 0 bytes): ___________Size__Instances__Average__Class________________ 555807584 34737974 16 java.lang.Integer 321112 5844 55 [I 106104 644 165 [C 37144 63 590 [B 13744 325 42 [Ljava.lang.Object; … <the rest> Top alloc’d
  • 24. Memory allocation profile But where is it allocated? • If you have a clue – just add counting via AtomicLong in the suspect places • If you don’t have a clue… just add it everywhere • Using aspect-oriented programming • Using bytecode manipulation More DIY style
  • 25. Bytecode manipulation Change bytecode instead of source code for all your profiling needs • Counting, time measuring • Decouples profiling from code logic • Great if you don’t need it always on • Can do it ahead-of-time and on-the-fly • Great for tasks like “profile each place of code where new XXX is invoked”
  • 26. Bytecode manipulation ObjectWeb ASM is an open source lib to help • Easy to use for bytecode manipulation • Extremely fast (suited to on-the-fly manipulation) ClassReader ClassVisitor ClassWriter MethodVisitor
  • 27. Bytecode manipulation with ASM class AClassVisitor extends ClassAdapter { public MethodVisitor visitMethod(…) { return new AMethodVisitor(super.visitMethod(…)) } } class AMethodVisitor extends MethodAdapter { public void visitIntInsn(int opcode, int operand) { super.visitIntInsn(opcode, operand); if (opcode == NEWARRAY) { // add new instructions here into this // point of class file… Will even preserve // original source code line numbers } } } To trace each array allocation
  • 28. On-the-fly bytecode manipulation Use java.lang.instrument package Use “-javaagent:<jarfile>” JVM option • Will run “premain” method in “Premain-Class” from jar file’s manifest • Will provide an instance of Instrumentation • It lets you install system-wide ClassFileTransformer – That transforms even system classes! • It has other useful methods like getObjectSize
  • 29. Conclusion Questions? Know bytecode Know JVM options Know Java libraries