• Save
Scala profiling
Upcoming SlideShare
Loading in...5
×
 

Scala profiling

on

  • 6,548 views

The presentation I gave at Riviera Scala Clojure Group on Scala profiling techniques (http://www.meetup.com/riviera-scala-clojure/events/54559732/)

The presentation I gave at Riviera Scala Clojure Group on Scala profiling techniques (http://www.meetup.com/riviera-scala-clojure/events/54559732/)

Statistics

Views

Total Views
6,548
Views on SlideShare
4,229
Embed Views
2,319

Actions

Likes
12
Downloads
0
Comments
2

5 Embeds 2,319

http://www.scoop.it 2152
https://twitter.com 160
http://webcache.googleusercontent.com 4
https://abs.twimg.com 2
http://www.slashdocs.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • What did you use you draw your object graphs ?
    Are you sure you want to
    Your message goes here
    Processing…
  • good work! :)
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Scala profiling Scala profiling Presentation Transcript

  • SCALA PROFILING (for Java developers) Filippo Pacificimercoledì 23 maggio 12
  • Who am I • Filippo Pacifici • Twitter: OddId_ • mail: filippo.pacifici@gmail.com • Blog: http://outofmemoryblog.blogspot.com • One of the 9M devs thinking Java is not so bad • recently started looking at Scalamercoledì 23 maggio 12
  • What’s this all about? • Apply Java profiling methods to Scala programs • How do we deal with performance in Java? • Is it the same in Scala? • How do we optimize a JVM for Scala applications?mercoledì 23 maggio 12
  • Java profiling 101mercoledì 23 maggio 12
  • Java profiling 101 • Goals: • troubleshoot performance problems • estimate application performance • estimate application scalabilitymercoledì 23 maggio 12
  • Java profiling 101 • Ehi, I use Scala, why should I care about Java profiling? • Scala compiled in byte code and runs in a JVM • We can profile a Scala application as it was Java • We can use the same tools • I am not aware of alternativesmercoledì 23 maggio 12
  • Java methods profiling • Tracks methods invocations • Runtime instrumentation • Time analysismercoledì 23 maggio 12
  • Java memory profiling • Dumps heap content • Browse objects in the heap • Memory usage analysismercoledì 23 maggio 12
  • Profiling tools • Method profiling • Java Visual VM (embedded in JDK) • Yourkit, Dynatrace, etc. • Memory profiling • Eclipse MAT (www.eclipse.org/ mat) • Yourkit, Dynatrace, etc.mercoledì 23 maggio 12
  • Back to Scala... • We won’t find Scala specific constructs • Need to know how Scala is translated into bytecode • Goals: • Identify methods generated by Scala compilers • Characterize Scala data structures in memorymercoledì 23 maggio 12
  • Scala Functionsmercoledì 23 maggio 12
  • Short discouraging comparative demomercoledì 23 maggio 12
  • Scala functions vs byte code • Classic functions converted in methods • First class function do not exist in Java • Anonymous classes extending scala.runtime.AbstractFunction • apply method to execute.mercoledì 23 maggio 12
  • AbstractFunction • AbstractFunction2 • takes 2 input parameters • One apply method per combination of input and output types • example apply.mcFID • F= returns float • I takes one Int • D takes one Doublemercoledì 23 maggio 12
  • AbstractFunction • Find the call in the profile: • anonfun => instance of the anonymous class • main$1 => first anonymous class defined in main methodmercoledì 23 maggio 12
  • Functions in the heap • Each instance of AbstractFunction present in the heap • Very small impact • Statelessmercoledì 23 maggio 12
  • Where do we use them? • Our program did not contain any anonymous function, right? • AbstractFunction used: • For first class functions • For closures • For partially applied functions • To manage for loops blocks • To manage filter logic in for loopsmercoledì 23 maggio 12
  • Performance impact • Is this a performance impact? • Scala compiler performs optimizations: • Same anonymous functions reused (avoid multiple instantiations) • Anonymous functions doing the same thing are shared • Attention to partially applied: • New function created.mercoledì 23 maggio 12
  • Some examplesmercoledì 23 maggio 12
  • Scala data structures (Collections in heap dump)mercoledì 23 maggio 12
  • Scala Lists • A Scala view: • Linked lists (single link) • abstract class List + two case classes: :: and Nilmercoledì 23 maggio 12
  • Scala Lists • A byte code view: • Case classes become inner classes: • :: becomes $colon$colon • Nil becomes Nilmercoledì 23 maggio 12
  • Scala Lists • Heads and elements have the same typemercoledì 23 maggio 12
  • Scala Lists • What about mutable lists? • ListBuffer • Wrapper on a Linked List • Keeps an additional reference to the last element: last0mercoledì 23 maggio 12
  • Scala Sets • Immutable sets • scala.collection.immutable.Set • Case classes for different sizes • HashSet over 5 elementsmercoledì 23 maggio 12
  • Scala Maps • Immutable: • small number of elements: scala.collections.immutable.Map$MapN • N = number of elements • over 5 elements: scala.collection.immutable.HashMap • Mutable: scala.collection.mutable.HashMapmercoledì 23 maggio 12
  • Map and Sets examplesmercoledì 23 maggio 12
  • Primitive types boxingmercoledì 23 maggio 12
  • Primitive types and generics • Type parameters cannot be primitive in generic types. • Scala systematically boxes and unboxes them to Object • scala.runtime.BoxesRunTime methodsmercoledì 23 maggio 12
  • Basic tuning tipsmercoledì 23 maggio 12
  • Exploit tail recursion • Long recursion • Long stack • Performance impact on stack size • Scala compiler recognizes tail recursion • Recursive call must be the last operation of the method • Scala transforms it into iterative formmercoledì 23 maggio 12
  • Can’t exploit tail recursion? • If (and only if) you run out of stack space (frequent java.lang.StackOverflowError): • -Xss JVM option sets stack size • example: -Xss2048k • Normally limited at OS level • Each thread statically allocates stack size: • pay attentionmercoledì 23 maggio 12
  • Memory structure • Optimize for small, short lived objects • Anonymous functions: • small • frequently instantiated • Use a big young space • GC is fast and frequent • Objects do not get promotedmercoledì 23 maggio 12
  • Memory structure • What about the perm gen? • Anonymous classes reused • No insane usage of proxies • No specific issuesmercoledì 23 maggio 12
  • Which GC should I use? • Depends on your application requirements • The same consideration done for Java still hold • Need throughput : parallel GC • Need response time : CMS • You are brave : G1mercoledì 23 maggio 12
  • Questions?mercoledì 23 maggio 12