Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
J2SE 5.0, 6.0 Generics  Concurrency, Garbabe Collection  <ul><li>Carol McDonald </li></ul><ul><ul><li>Java Technology Arch...
Speaker’s Qualifications <ul><li>Carol  cDonald:  </li></ul><ul><ul><li>Java Architect at Sun Microsystems </li></ul></ul>...
Agenda <ul><li>Language Changes </li></ul><ul><ul><li>Generics & Metadata </li></ul></ul><ul><li>Library API Changes </li>...
J2SE 5.0 Design Themes <ul><li>Focus on  quality , stability, compatibility   </li></ul><ul><li>Big emphasis on  scalabili...
Java Language Changes <ul><li>JDK 1.0 </li></ul><ul><ul><li>Initial language, very popular </li></ul></ul><ul><li>JDK1.1 <...
<ul><ul><li>2004 </li></ul></ul><ul><ul><li>2006 </li></ul></ul><ul><ul><li>J2SE 5 </li></ul></ul><ul><ul><li>“ Tiger” </l...
Tiger Is Everywhere Tiger 1.4.x Completed downloads per month
Over 262,295,496 downloads served! Tiger Is Everywhere
Pre-installed on > 60 % of new PCs Tiger Is Everywhere
≥   99.999 % availability Previous best: 99.98% for 1.4.2 Tiger Is Stable
Tiger Is Fast Server Benchmark: SPECjbb2000
<ul><li>Tiger [Java SE 5] Features Overview </li></ul><ul><li>JDK 6 Features Overview </li></ul>
Java SE 1.5 Language Changes
Seven Major New Features <ul><li>Generics </li></ul><ul><li>Autoboxing/Unboxing </li></ul><ul><li>Enhanced for loop (“fore...
Generics
Is there a problem in here? Vector v = new Vector(); v.add(new  Integer (4)); OtherClass.expurgate(v); ... static void exp...
The Problem (Pre-J2SE 5.0) Vector v = new Vector(); v.add(new  Integer (4)); OtherClass.expurgate(v); ... static void expu...
Generics <ul><li>Problem: Collection element types </li></ul><ul><ul><li>Compiler is unable to  verify types </li></ul></u...
Using Generic Classes <ul><li>Instantiate a  generic  class to create type specific object </li></ul><ul><li>Example </li>...
Wildcards <ul><li>Method to print contents of any Collection? </li></ul><ul><li>Wrong! </li></ul><ul><li>Passing a  Collec...
Wildcards <ul><li>Correct way: </li></ul><ul><li>?  is the  wildcard  type </li></ul><ul><li>Collection <?>  means Collect...
Bounded Wildcards <ul><li>A wildcard can be specified with an  upper bound   </li></ul>public void  drawAll (List< ? exten...
Autoboxing & Unboxing
Autoboxing/Unboxing of Primitive Types <ul><li>Problem: (pre-J2SE 5.0)  Conversion between  primitive  types and  wrapper ...
Autoboxing/Unboxing of Primitive Types <ul><li>Solution: Let the compiler do it </li></ul><ul><ul><li>Integer   intObj  = ...
Enhanced for Loop
Enhanced for Loop (foreach) <ul><li>Problem: (pre-J2SE 5.0) </li></ul><ul><ul><li>Iterating over collections is tricky </l...
Enhanced for Loop Example <ul><li>Old  code pre-J2SE 5.0 </li></ul><ul><ul><li>void cancelAll(Collection c) { </li></ul></...
Type-safe Enumerations
Type-safe Enumerations <ul><li>Problem: (pre-J2SE 5.0)  to define an enumeration: </li></ul><ul><ul><li>Defined a bunch of...
Enumeration Example: public class  Card  { public  enum   Suit  { spade, diamond, club, heart }; public  enum   Rank  {  a...
Varargs
Before Varargs Example  //example method that takes a variable number of  parameters int sum( Integer[] numbers ) {  for(i...
Varargs Example  (Cont) //example method that takes a variable number of parameters int sum ( Integer... numbers )  { for(...
Varargs examples <ul><li>APIs have been modified  so that methods accept variable-length argument lists  where appropriate...
Static Imports
Static Imports <ul><li>Problem: (pre-J2SE 5.0) </li></ul><ul><ul><li>Having to fully qualify every static referenced from ...
Formatted I/O
Simple Formatted I/O  <ul><li>Printf  is popular with C/C++ developers </li></ul><ul><ul><li>Powerful, easy to use </li></...
Annotations
Annotations Metadata (JSR-175) <ul><li>Provide standardised way of  adding annotations  to Java code </li></ul><ul><ul><li...
Annotations Example: JAX-RPC <ul><li>Old Code </li></ul><ul><li>public   interface  PingIF  implements java.rmi.Remote  { ...
Virtual Machine
Some Performance Enhancements in JDK 1.4 <ul><li>java.nio  package </li></ul><ul><ul><li>Non blocking I/O : improved  perf...
Some J2SE 5.0 Performance Features  <ul><li>Concurrency libraries </li></ul><ul><li>Garbage collection  improvements </li>...
Quick Performance Fix <ul><li>Always upgrade to the latest version of the JDK/JRE </li></ul><ul><ul><li>Sun is always work...
Tiger is Fast Client Benchmark: SwingMark
Tiger Is Fast Server Benchmark: SPECjbb2000
x64 SPECjbb2005 Performance SPECjbb2005 Sun Fire X4100 (2 chip, 2 core, 2 threads) 32,018 SPECjbb2005 bops, 32,018 SPECjbb...
Java is Fast!  Grizzly Web Server Benchmark
Java Concurrency
Motivation for Concurrency Utilities <ul><li>Developing concurrent classes was too hard </li></ul><ul><li>Java has concurr...
Concurrency Utilities Goals <ul><li>Provide a good set of  concurrency building blocks </li></ul><ul><ul><li>library for c...
Puzzle:  “Ping Pong” <ul><li>01 class PingPong { </li></ul><ul><li>02  public static  synchronized  void  main (String[] a...
What Does It Print? <ul><li>(a)  PingPong </li></ul><ul><li>(b)  PongPing </li></ul><ul><li>(c) It varies </li></ul>
What Does It Print? <ul><li>(a)  PingPong </li></ul><ul><li>(b)  PongPing </li></ul><ul><li>(c) It varies </li></ul><ul><l...
Example How to start a thread <ul><li>public class HelloRunnable  implements Runnable  { </li></ul><ul><li>public void  ru...
Another Look <ul><li>01 class PingPong { </li></ul><ul><li>02  public static synchronized void main(String[] a) { </li></u...
How Do You Fix It? <ul><li>01 class PingPong { </li></ul><ul><li>02  public static synchronized void main(String[] a) { </...
The Moral <ul><li>Invoke  Thread.start , not  Thread.run </li></ul><ul><ul><li>Common error </li></ul></ul><ul><ul><li>Can...
Concurrency Utilities: JSR-166 <ul><li>Task Scheduling Framework: Executor   interface replaces direct use of   Thread </l...
Concurrency Utilities: JSR-166 <ul><li>Task Scheduling Framework: Executor   interface replaces direct use of   Thread </l...
Executor Framework for asynchronous execution public interface  Executor  { void execute (Runnable command); } public inte...
Creating Executors <ul><li>Factory methods in the  Executors  class </li></ul>public class  Executors  { static ExecutorSe...
Thread Pool Example class WebService { public static void main(String[] args) { Executor pool = Executors.newFixedThreadPo...
ExecutorService for Lifecycle Support <ul><li>ExecutorService  supports graceful and  immediate shutdown </li></ul>public ...
ScheduledExecutorService <ul><li>Deferred and recurring tasks </li></ul><ul><ul><li>Schedule execution of  Callable  or  R...
ScheduledExecutorService Example ScheduledExecutorService sched =  Executors. newSingleThreadScheduledExecutor (); public ...
Synchronize Critical Section <ul><li>E.g.,  shared resource  is an customer account.  Certain methods called by multiple t...
Locks <ul><li>Java provides basic locking via   synchronized </li></ul><ul><li>Good for many situations, but some issues <...
Lock Interface <ul><li>No automatic unlocking </li></ul>Interface Lock { void  lock (); void  lockInterruptibly () throws ...
RentrantLock <ul><li>Simplest concrete implementation of Lock </li></ul><ul><li>Same semantics as synchronized, but with m...
Lock Example Lock  lock = new RentrantLock(); public void accessProtectedResource()  throws IllegalMonitorStateException {...
ReadWriteLock Interface <ul><li>Has  two locks  controlling read and write access </li></ul><ul><ul><li>Multiple threads  ...
ReadWriteLock Example ReentrantReadWriteLock rwl = new  ReentrantReadWriteLock (); Lock rLock =  rwl.readLock (); Lock wLo...
Synchronizers <ul><li>Co-ordinate access and control </li></ul><ul><li>Semaphore </li></ul><ul><ul><li>Manages a fixed siz...
BlockingQueue Interface <ul><li>Provides thread safe way for multiple threads to manipulate collection </li></ul>Interface...
BlockingQueue Implementations <ul><li>ArrayBlockingQueue </li></ul><ul><ul><li>Bounded queue, backed by an array, FIFO </l...
Blocking Queue Example: 1 private  BlockingQueue<String>   msgQueue ; public  Logger ( BlockingQueue<String>  mq) { msgQue...
Blocking Queue Example: 2 private  ArrayBlockingQueue messageQueue =  new ArrayBlockingQueue<String>(10); Logger logger = ...
Concurrent Collections <ul><li>ConcurrentMap  (interface) </li></ul><ul><ul><li>Extends Map interface with atomic operatio...
Summary <ul><li>New concurrency features are very powerful </li></ul><ul><li>Lots of great features </li></ul><ul><li>Take...
For More Information
Garbage  Collection
Classic Memory Leak in  C  <ul><li>User does the memory management </li></ul><ul><li>void service(int n, char** names) { <...
Garbage Collection <ul><li>Find and reclaim  unreachable  objects </li></ul><ul><ul><li>Anything not transitively reachabl...
Generational Garbage Collection <ul><li>Keeps young and old objects separately </li></ul><ul><ul><li>In spaces called  gen...
Garbage Collection <ul><li>Garbage collection:  Pros </li></ul><ul><ul><li>Increased reliability  – no memory leaks, no da...
Incremental Garbage Collection <ul><li>decreases/minimizes GC disruption </li></ul><ul><li>GC works at the same time as th...
Garbage Collection <ul><li>Myths  about  garbage collection  abound </li></ul><ul><ul><li>Myth: Allocation and garbage col...
Object Allocation (1/2) <ul><li>Typically, object allocation is  very cheap! </li></ul><ul><ul><li>10 native instructions ...
Object Allocation (2/2) <ul><li>We  do not  advise </li></ul><ul><ul><li>Needless  allocation </li></ul></ul><ul><ul><ul><...
Large Objects <ul><li>Very  large  objects are: </li></ul><ul><ul><li>Expensive to  allocate  (maybe not through the fast ...
Object Pooling (1) <ul><li>Legacy of older VMs with terrible allocation performance </li></ul><ul><li>Remember </li></ul><...
Object Pooling (3/3) <ul><li>Exceptions </li></ul><ul><ul><li>Objects that are  expensive  to  allocate  and/or  initializ...
Memory Leaks? <ul><li>But, the GC is supposed to fix memory leaks! </li></ul><ul><li>The GC will collect all  unreachable ...
Memory Leak Types <ul><li>“Traditional” memory leaks </li></ul><ul><ul><li>Heap keeps  growing , and  growing,  and  growi...
Memory Leak Sources <ul><li>Objects in the wrong scope </li></ul><ul><li>Lapsed listeners </li></ul><ul><li>Exceptions cha...
Objects in the Wrong Scope (1/2) <ul><li>Below, names really local to doIt() </li></ul><ul><ul><li>It will not be reclaime...
Objects in the Wrong Scope (2/2) <ul><li>Remember </li></ul><ul><ul><li>Generational GCs love short-lived objects </li></u...
Memory Leak Sources <ul><li>Objects in the wrong scope </li></ul><ul><li>Lapsed listeners </li></ul><ul><li>Exceptions cha...
Exceptions Change Control Flow  (1/2) <ul><li>Beware </li></ul><ul><ul><li>Thrown exceptions can change control flow </li>...
Exceptions Change Control Flow  (2/2) <ul><li>Always use finally  blocks </li></ul><ul><li>  ImageReader reader = new Imag...
Memory Leak Sources <ul><li>Objects in the wrong scope </li></ul><ul><li>Lapsed listeners </li></ul><ul><li>Exceptions cha...
Metadata Mismanagement (1/2) <ul><li>Sometimes, we want to: </li></ul><ul><ul><li>Keep track of object metadata </li></ul>...
Metadata Mismanagement (2/2) <ul><li>What happens if we  forget  to call  remove (image)? </li></ul><ul><ul><li>never be r...
Some Memory Management Myths <ul><li>Myth: Explicitly  nulling references  helps GC </li></ul><ul><ul><li>Rarely helpful <...
Local Variable Nulling <ul><li>Local variable nulling i s n ot necessary </li></ul><ul><ul><li>The JIT can do liveness ana...
Some Memory Management Myths <ul><li>Myth: Finalizers are Java's idea of destructors </li></ul><ul><ul><li>Finalizers are ...
Virtual Machine Smart Tuning
How “Smart Tuning” Works <ul><li>Provide  good  “ out of the box ” performance  without  hand  tuning </li></ul><ul><li>De...
“ Smart Tuning” <ul><li>Dynamically adjust  Java HotSpot VM software environment at  runtime </li></ul><ul><li>Adaptive He...
Effects of Tuning Tuned vs. Non-tuned JVM
Hand Tuned vs. Smart Tuning
Monitoring & Management
Memory Leak Detection Tools <ul><li>Many tools to choose from </li></ul><ul><li>“ Is there a memory leak”? </li></ul><ul><...
Monitoring, Management, Diagnostics <ul><li>GUI tools: JConsole, jhat, VisualGC (NetBeans), dynamic attach </li></ul><ul><...
Monitoring and Management <ul><li>Attach on demand for  </li></ul><ul><ul><li>jconsole : can connect to applications that ...
J2SE 5.0 Monitoring & Management
Platform Beans (MXBean's) <ul><li>Provides  API access to </li></ul><ul><ul><li>number of classes loaded,  </li></ul></ul>...
Jconsole http://www.netbeans.org/kb/articles/jmx-getstart.html
NetBeans Profiler <ul><li>Low overhead profiling </li></ul><ul><li>Attach to running applications </li></ul><ul><li>CPU  p...
NetBeans Profiler
JDK 6 (Mustang)  Features Overview
JDK 6 Component JSRs 202: Class File Update 199: Compiler API 269: Annotation Processors 260: Javadoc ™  Tag Update  Ease ...
The JDK 6 Top 10 <ul><li>Scripting </li></ul><ul><li>Web Services </li></ul><ul><li>Database (JDBC 4.0, Java DB) </li></ul...
Resources and Summary
For More Information (1/2) <ul><li>Memory management white paper </li></ul><ul><ul><li>http://java.sun.com/j2se/reference/...
For More Information (2/2) <ul><li>FindBugs </li></ul><ul><ul><li>http://findbugs.sourceforge.net </li></ul></ul><ul><li>H...
Call for Action! <ul><li>Download and install JDK 5 or JDK 6 </li></ul><ul><li>Start using the new language features ASAP ...
Resources <ul><li>http://java.sun.com/javase </li></ul><ul><li>Java.net </li></ul><ul><li>http://jdk.dev.java.net </li></ul>
Stay in Touch with Java SE  <ul><li>http://java.sun.com/javase </li></ul><ul><li>JDK Software Community </li></ul><ul><ul>...
Thank You! <ul><li>Carol McDonald </li></ul><ul><ul><li>Java Technology Architect </li></ul></ul><ul><ul><ul><li>Sun Micro...
Upcoming SlideShare
Loading in …5
×

Java 5 6 Generics, Concurrency, Garbage Collection, Tuning

13,316 views

Published on

Published in: Technology, News & Politics

Java 5 6 Generics, Concurrency, Garbage Collection, Tuning

  1. 1. J2SE 5.0, 6.0 Generics Concurrency, Garbabe Collection <ul><li>Carol McDonald </li></ul><ul><ul><li>Java Technology Architect </li></ul></ul><ul><ul><ul><li>Sun Microsystems, Inc. </li></ul></ul></ul>
  2. 2. Speaker’s Qualifications <ul><li>Carol cDonald: </li></ul><ul><ul><li>Java Architect at Sun Microsystems </li></ul></ul><ul><ul><li>Before Sun, worked on software development of: </li></ul></ul><ul><ul><ul><li>Application to manage car Loans for Toyota (>10 million loans) </li></ul></ul></ul><ul><ul><ul><li>Pharmaceutical Intranet ( Roche Switzerland) </li></ul></ul></ul><ul><ul><ul><li>Telecom Network Mgmt ( Digital France) </li></ul></ul></ul><ul><ul><ul><li>X.400 Email Server ( IBM Germany) </li></ul></ul></ul>
  3. 3. Agenda <ul><li>Language Changes </li></ul><ul><ul><li>Generics & Metadata </li></ul></ul><ul><li>Library API Changes </li></ul><ul><ul><li>Concurrency utilities </li></ul></ul><ul><li>Garbage Collection </li></ul><ul><li>Virtual Machine </li></ul><ul><li>Monitoring & Management </li></ul><ul><li>Next Release: Mustang </li></ul>
  4. 4. J2SE 5.0 Design Themes <ul><li>Focus on quality , stability, compatibility </li></ul><ul><li>Big emphasis on scalability </li></ul><ul><ul><li>exploit big heaps, big I/O, big everything </li></ul></ul><ul><li>Ease of development </li></ul><ul><ul><li>Faster, cheaper, more reliable </li></ul></ul>
  5. 5. Java Language Changes <ul><li>JDK 1.0 </li></ul><ul><ul><li>Initial language, very popular </li></ul></ul><ul><li>JDK1.1 </li></ul><ul><ul><li>Inner classes, new event model </li></ul></ul><ul><li>JDK 1.2, 1.3 </li></ul><ul><ul><li>No changes at language level </li></ul></ul><ul><li>JDK 1.4 </li></ul><ul><ul><li>Assertions (minor change) </li></ul></ul><ul><li>JDK 5.0 </li></ul><ul><ul><li>Biggest changes to language since release 1.0 </li></ul></ul>
  6. 6. <ul><ul><li>2004 </li></ul></ul><ul><ul><li>2006 </li></ul></ul><ul><ul><li>J2SE 5 </li></ul></ul><ul><ul><li>“ Tiger” </li></ul></ul><ul><ul><li>Java SE 6 </li></ul></ul><ul><ul><li>“ Mustang” </li></ul></ul><ul><ul><li>2007 </li></ul></ul><ul><ul><li>Java SE 7 </li></ul></ul><ul><ul><li>“ Dolphin” </li></ul></ul>Java SE road map <ul><ul><li>2008 </li></ul></ul><ul><ul><li>2003 </li></ul></ul><ul><ul><li>2002 </li></ul></ul><ul><ul><li>2005 </li></ul></ul><ul><ul><li>J2SE 1.4 </li></ul></ul><ul><ul><li>EOL </li></ul></ul><ul><ul><li>J2SE 1.3.1 </li></ul></ul><ul><li>5.0 Java SE 5 2004/9 </li></ul><ul><li>1. 6 .0 Java SE 6 2006/12 </li></ul><ul><li>1. 7 .0 Java SE 7 2008 </li></ul>
  7. 7. Tiger Is Everywhere Tiger 1.4.x Completed downloads per month
  8. 8. Over 262,295,496 downloads served! Tiger Is Everywhere
  9. 9. Pre-installed on > 60 % of new PCs Tiger Is Everywhere
  10. 10. ≥ 99.999 % availability Previous best: 99.98% for 1.4.2 Tiger Is Stable
  11. 11. Tiger Is Fast Server Benchmark: SPECjbb2000
  12. 12. <ul><li>Tiger [Java SE 5] Features Overview </li></ul><ul><li>JDK 6 Features Overview </li></ul>
  13. 13. Java SE 1.5 Language Changes
  14. 14. Seven Major New Features <ul><li>Generics </li></ul><ul><li>Autoboxing/Unboxing </li></ul><ul><li>Enhanced for loop (“foreach”) </li></ul><ul><li>Type-safe enumerations </li></ul><ul><li>Varargs </li></ul><ul><li>Static import </li></ul><ul><li>Metadata </li></ul>
  15. 15. Generics
  16. 16. Is there a problem in here? Vector v = new Vector(); v.add(new Integer (4)); OtherClass.expurgate(v); ... static void expurgate(Collection c) { for (Iterator it = c.iterator(); it.hasNext();) if ((( String )it.next()).length() == 4) it.remove(); }
  17. 17. The Problem (Pre-J2SE 5.0) Vector v = new Vector(); v.add(new Integer (4)); OtherClass.expurgate(v); ... static void expurgate(Collection c) { for (Iterator it = c.iterator(); it.hasNext();) /* ClassCastException */ if ((( String )it.next()).length() == 4) it.remove(); }
  18. 18. Generics <ul><li>Problem: Collection element types </li></ul><ul><ul><li>Compiler is unable to verify types </li></ul></ul><ul><ul><li>Assignment must use the cast operator </li></ul></ul><ul><ul><li>This can generate runtime errors ( ClassCastException ) </li></ul></ul><ul><li>Solution: </li></ul><ul><ul><li>Tell the compiler what type the collection is </li></ul></ul><ul><ul><li>Let the compiler fill in the cast </li></ul></ul>
  19. 19. Using Generic Classes <ul><li>Instantiate a generic class to create type specific object </li></ul><ul><li>Example </li></ul><ul><li>Vector <String> x = new Vector <String> (); </li></ul><ul><li>x.add(new Integer(5)); // Compiler error! </li></ul><ul><li>Vector <Integer> y = new Vector <Integer> (); </li></ul>
  20. 20. Wildcards <ul><li>Method to print contents of any Collection? </li></ul><ul><li>Wrong! </li></ul><ul><li>Passing a Collection of type String will give a compiler error </li></ul>void printCollection(Collection <Object> c) { for (Object o : c) System.out.println( o ); }
  21. 21. Wildcards <ul><li>Correct way: </li></ul><ul><li>? is the wildcard type </li></ul><ul><li>Collection <?> means Collection of unknown </li></ul>void printCollection( Collection <?> c) { for (Object o : c) System.out.println(o); }
  22. 22. Bounded Wildcards <ul><li>A wildcard can be specified with an upper bound </li></ul>public void drawAll (List< ? extends Shape >s) { ... } List<Circle> c = getCircles(); drawAll(c) ; List<Triangle> t = getTriangles(); drawAll(t) ;
  23. 23. Autoboxing & Unboxing
  24. 24. Autoboxing/Unboxing of Primitive Types <ul><li>Problem: (pre-J2SE 5.0) Conversion between primitive types and wrapper types (and vice-versa) </li></ul><ul><ul><li>must manually convert a primitive type to a wrapper type before adding it to a collection </li></ul></ul><ul><ul><li>int i = 22; </li></ul></ul><ul><ul><li>List l = new LinkedList(); </li></ul></ul><ul><ul><li>l.add( new Integer(i) ); </li></ul></ul>
  25. 25. Autoboxing/Unboxing of Primitive Types <ul><li>Solution: Let the compiler do it </li></ul><ul><ul><li>Integer intObj = 22 ; // Autoboxing conversion </li></ul></ul><ul><ul><li>int i = intObj; // Unboxing conversion </li></ul></ul><ul><ul><li>ArrayList< Integer > al = new ArrayList<Integer>(); </li></ul></ul><ul><ul><li>al .add( i ); // Autoboxing conversion </li></ul></ul>
  26. 26. Enhanced for Loop
  27. 27. Enhanced for Loop (foreach) <ul><li>Problem: (pre-J2SE 5.0) </li></ul><ul><ul><li>Iterating over collections is tricky </li></ul></ul><ul><ul><li>Often, iterator only used to get an element </li></ul></ul><ul><ul><li>Iterator is error prone (Can occur three times in a for loop) </li></ul></ul><ul><li>Solution: Let the compiler do it </li></ul><ul><ul><li>New for loop syntax for (variable : collection) </li></ul></ul><ul><ul><li>Works for Collections and arrays </li></ul></ul>
  28. 28. Enhanced for Loop Example <ul><li>Old code pre-J2SE 5.0 </li></ul><ul><ul><li>void cancelAll(Collection c) { </li></ul></ul><ul><ul><li>for ( Iterator i = c.iterator(); i.hasNext() ; ){ </li></ul></ul><ul><ul><ul><li>TimerTask task = (TimerTask) i.next(); </li></ul></ul></ul><ul><ul><ul><li>task.cancel(); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>New Code </li></ul><ul><ul><li>void cancelAll(Collection <TimerTask> c) { </li></ul></ul><ul><ul><ul><li>for ( TimerTask task : c ) </li></ul></ul></ul><ul><ul><ul><li>task.cancel(); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul>Iterating over collections, tricky, error prone <ul><li>New for loop syntax: </li></ul><ul><li>Let the compiler do it </li></ul><ul><li>Works for Collections and arrays </li></ul>
  29. 29. Type-safe Enumerations
  30. 30. Type-safe Enumerations <ul><li>Problem: (pre-J2SE 5.0) to define an enumeration: </li></ul><ul><ul><li>Defined a bunch of integer constants: </li></ul></ul><ul><ul><li>public static final int SEASON_WINTER = 0; </li></ul></ul><ul><ul><li>public static final int SEASON_SPRING = 1; </li></ul></ul><ul><li>Issues of using Integer constants </li></ul><ul><ul><li>Not type safe (any integer will pass),Brittleness (how do add value in-between?), Printed values uninformative (prints just int values) </li></ul></ul><ul><li>Solution: New type of class declaration </li></ul><ul><ul><li>enum type has public, self-typed members for each enum constant </li></ul></ul><ul><ul><li>enum Season { WINTER, SPRING, SUMMER, FALL } </li></ul></ul>
  31. 31. Enumeration Example: public class Card { public enum Suit { spade, diamond, club, heart }; public enum Rank { ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king }; private Card (Rank rank, Suit suit) { this.rank = rank; this.suit = suit; } } List< Card > deck = new ArrayList< Card >(); for ( Suit suit : Suit.values ()) for ( Rank rank : Rank.values ()) deck.add(new Card(rank, suit)); Think how much JDK1.4 code this would require!
  32. 32. Varargs
  33. 33. Before Varargs Example //example method that takes a variable number of parameters int sum( Integer[] numbers ) { for(int i: numbers) // do something } // Code fragment that calls the sum method sum( new Integer[] {12,13,20} ); http://www.javaworld.com/javaworld/jw-04-2004/jw-0426-tiger1.html <ul><li>Problem: (in pre-J2SE 5.0) To have a method that takes a variable number of parameters </li></ul><ul><ul><li>Can be done with an array, but caller has to create the array first </li></ul></ul>
  34. 34. Varargs Example (Cont) //example method that takes a variable number of parameters int sum ( Integer... numbers ) { for(int i: numbers) // do something } // Code fragment that calls the sum method sum( 12,13,20 ); http://www.javaworld.com/javaworld/jw-04-2004/jw-0426-tiger1.html <ul><li>Solution: Let the compiler do it for you: </li></ul><ul><ul><li>String format (String fmt, Object... args); </li></ul></ul><ul><ul><li>Java now supports printf(...) </li></ul></ul>
  35. 35. Varargs examples <ul><li>APIs have been modified so that methods accept variable-length argument lists where appropriate </li></ul><ul><ul><li>Class.getMethod </li></ul></ul><ul><ul><li>Method.invoke </li></ul></ul><ul><ul><li>Constructor.newInstance </li></ul></ul><ul><ul><li>Proxy.getProxyClass </li></ul></ul><ul><ul><li>MessageFormat.format </li></ul></ul><ul><li>New APIs do this too </li></ul><ul><ul><li>System.out. printf (“%d + %d = %d ”, a, b, a+b); </li></ul></ul>
  36. 36. Static Imports
  37. 37. Static Imports <ul><li>Problem: (pre-J2SE 5.0) </li></ul><ul><ul><li>Having to fully qualify every static referenced from external classes </li></ul></ul><ul><li>Solution: New import syntax </li></ul><ul><ul><li>import static TypeName.Identifier; </li></ul></ul><ul><ul><li>import static Typename.*; </li></ul></ul><ul><ul><li>Also works for static methods and enums </li></ul></ul><ul><ul><li>e.g Math.sin(x) becomes sin(x) </li></ul></ul>
  38. 38. Formatted I/O
  39. 39. Simple Formatted I/O <ul><li>Printf is popular with C/C++ developers </li></ul><ul><ul><li>Powerful, easy to use </li></ul></ul><ul><li>Finally adding printf to J2SE 5.0 (using varargs) </li></ul><ul><ul><li>out.printf(“%-12s is %2d long”, name, l); </li></ul></ul><ul><ul><li>out.printf(“value = %2.2F”, value); </li></ul></ul>
  40. 40. Annotations
  41. 41. Annotations Metadata (JSR-175) <ul><li>Provide standardised way of adding annotations to Java code </li></ul><ul><ul><li>public @Remote void foo() {} </li></ul></ul><ul><li>Annotations are used by tools that work with Java code: </li></ul><ul><ul><li>Compiler </li></ul></ul><ul><ul><li>IDE </li></ul></ul><ul><ul><li>Runtime tools </li></ul></ul><ul><li>Used to generate interfaces, deployment descriptors... </li></ul>
  42. 42. Annotations Example: JAX-RPC <ul><li>Old Code </li></ul><ul><li>public interface PingIF implements java.rmi.Remote { </li></ul><ul><ul><li>public void foo() throws java.rmi.RemoteException; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public class Ping implements PingIF { </li></ul></ul><ul><ul><li>public void foo() {...} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>New Code </li></ul><ul><ul><li>public class Ping { </li></ul></ul><ul><ul><li>public @Remote void foo() {} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  43. 43. Virtual Machine
  44. 44. Some Performance Enhancements in JDK 1.4 <ul><li>java.nio package </li></ul><ul><ul><li>Non blocking I/O : improved performance in buffer management, scalable network and file i/o </li></ul></ul><ul><li>Reflection </li></ul><ul><ul><li>Many reflective operations rewritten for higher performance (20x) </li></ul></ul>
  45. 45. Some J2SE 5.0 Performance Features <ul><li>Concurrency libraries </li></ul><ul><li>Garbage collection improvements </li></ul><ul><li>“ Smart tuning” </li></ul><ul><li>Small systems performance optimizations </li></ul><ul><li>Client features </li></ul><ul><ul><li>Class data sharing </li></ul></ul><ul><ul><li>Various JFC/Swing and Java 2D™ APIs improvements </li></ul></ul><ul><li>X86 Optimizations </li></ul>
  46. 46. Quick Performance Fix <ul><li>Always upgrade to the latest version of the JDK/JRE </li></ul><ul><ul><li>Sun is always working to improve performance </li></ul></ul><ul><ul><li>Sun is always working to reduce the number of 'undocumented features' </li></ul></ul>
  47. 47. Tiger is Fast Client Benchmark: SwingMark
  48. 48. Tiger Is Fast Server Benchmark: SPECjbb2000
  49. 49. x64 SPECjbb2005 Performance SPECjbb2005 Sun Fire X4100 (2 chip, 2 core, 2 threads) 32,018 SPECjbb2005 bops, 32,018 SPECjbb2005 bops/JVM, Sun Fire X4100 (2 chip, 2 core, 2 threads) 38,090 SPECjbb2005 bops, 19,045 SPECjbb2005 bops/JVM, submitted for review, IBM eServer p5 510 (2 chips, 2 cores, 4 thread) 36,039 bops, 36,039 bops/JVM, Dell SC1425 (2 chips, 2 cores, 4 thread) 24,208 SPECjbb2005 bops, 24,208 SPECjbb2005 bops/JVM, Dell PE 850 (1 chip, 2 cores, 2 thread) 31,138 SPECjbb2005 bops, 31,138 SPECjbb2005 bops/JVM. SPEC® and the benchmark name SPECjbb™ are trademarks of the Standard Performance Evaluation Corporation. Competitive benchmark results stated above reflect results published on www.spec.org as of May 11, 2005. For the latest SPECjbb2005 benchmark results, visit http://www.spec.org/osg/jbb2005. Sun Fire™ X4100 Server Wins
  50. 50. Java is Fast! Grizzly Web Server Benchmark
  51. 51. Java Concurrency
  52. 52. Motivation for Concurrency Utilities <ul><li>Developing concurrent classes was too hard </li></ul><ul><li>Java has concurrency primitives: </li></ul><ul><ul><li>wait() , notify() , sleep() , interrupt() , synchronized </li></ul></ul><ul><ul><li>These are too primitive, Too low level </li></ul></ul><ul><ul><li>Easy to use incorrectly </li></ul></ul><ul><ul><ul><li>Incorrect use can produce poor performance </li></ul></ul></ul>
  53. 53. Concurrency Utilities Goals <ul><li>Provide a good set of concurrency building blocks </li></ul><ul><ul><li>library for concurrency like Collections for data structures </li></ul></ul><ul><li>Enhance scalability, performance, readability and thread safety of Java applications </li></ul><ul><li>Beat C performance in high-end server applications </li></ul>
  54. 54. Puzzle: “Ping Pong” <ul><li>01 class PingPong { </li></ul><ul><li>02 public static synchronized void main (String[] a) { </li></ul><ul><li>03 Thread t = new Thread() { </li></ul><ul><li>04 public void run() { </li></ul><ul><li>05 pong(); </li></ul><ul><li>06 } </li></ul><ul><li>07 }; </li></ul><ul><li>08 </li></ul><ul><li>09 t.run(); </li></ul><ul><li>10 System.out.print(&quot;Ping&quot;); </li></ul><ul><li>11 } </li></ul><ul><li>12 </li></ul><ul><li>13 static synchronized void pong() { </li></ul><ul><li>14 System.out.print(&quot;Pong&quot;); </li></ul><ul><li>15 } </li></ul><ul><li>16 } </li></ul>
  55. 55. What Does It Print? <ul><li>(a) PingPong </li></ul><ul><li>(b) PongPing </li></ul><ul><li>(c) It varies </li></ul>
  56. 56. What Does It Print? <ul><li>(a) PingPong </li></ul><ul><li>(b) PongPing </li></ul><ul><li>(c) It varies </li></ul><ul><li>Not a multithreaded program! </li></ul>
  57. 57. Example How to start a thread <ul><li>public class HelloRunnable implements Runnable { </li></ul><ul><li>public void run() { </li></ul><ul><li>System.out.println(&quot;Hello from a thread!&quot;); </li></ul><ul><li>} </li></ul><ul><li>public static void main(String args[]) { </li></ul><ul><li>(new Thread (new HelloRunnable())) .start(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  58. 58. Another Look <ul><li>01 class PingPong { </li></ul><ul><li>02 public static synchronized void main(String[] a) { </li></ul><ul><li>03 Thread t = new Thread() { </li></ul><ul><li>04 public void run () { </li></ul><ul><li>05 pong(); </li></ul><ul><li>06 } </li></ul><ul><li>07 }; </li></ul><ul><li>08 </li></ul><ul><li>09 t. run (); // Common typo! </li></ul><ul><li>10 System.out.print(&quot;Ping&quot;); </li></ul><ul><li>11 } </li></ul><ul><li>12 </li></ul><ul><li>13 static synchronized void pong() { </li></ul><ul><li>14 System.out.print(&quot;Pong&quot;); </li></ul><ul><li>15 } </li></ul><ul><li>16 } </li></ul>
  59. 59. How Do You Fix It? <ul><li>01 class PingPong { </li></ul><ul><li>02 public static synchronized void main(String[] a) { </li></ul><ul><li>03 Thread t = new Thread() { </li></ul><ul><li>04 public void run() { </li></ul><ul><li>05 pong(); </li></ul><ul><li>06 } </li></ul><ul><li>07 }; </li></ul><ul><li>08 </li></ul><ul><li>09 t. start (); </li></ul><ul><li>10 System.out.print(&quot;Ping&quot;); </li></ul><ul><li>11 } </li></ul><ul><li>12 </li></ul><ul><li>13 static synchronized void pong() { </li></ul><ul><li>14 System.out.print(&quot;Pong&quot;); </li></ul><ul><li>15 } </li></ul><ul><li>16 } </li></ul>
  60. 60. The Moral <ul><li>Invoke Thread.start , not Thread.run </li></ul><ul><ul><li>Common error </li></ul></ul><ul><ul><li>Can be very difficult to diagnose </li></ul></ul>
  61. 61. Concurrency Utilities: JSR-166 <ul><li>Task Scheduling Framework: Executor interface replaces direct use of Thread </li></ul><ul><li>Callable and Future </li></ul><ul><li>Synchronisers </li></ul><ul><ul><li>Semaphore, CyclicBarrier, CountDownLatch </li></ul></ul><ul><li>Concurrent collections : BlockingQueue </li></ul><ul><li>Lock </li></ul><ul><li>Atomic </li></ul>
  62. 62. Concurrency Utilities: JSR-166 <ul><li>Task Scheduling Framework: Executor interface replaces direct use of Thread </li></ul><ul><li>No more direct Thread invocation </li></ul><ul><ul><li>Use myExecutor.execute(aRunnable); </li></ul></ul><ul><ul><li>Not new Thread(aRunnable).start(); </li></ul></ul>public interface Executor { void execute (Runnable command); }
  63. 63. Executor Framework for asynchronous execution public interface Executor { void execute (Runnable command); } public interface ExecutorService extends Executor { .. } public class Executors { //Factory methods static ExecutorService newFixedThreadPool(int poolSize); ... } Executor pool = Executors.newFixedThreadPool(5); pool.execute ( runnable ) ;
  64. 64. Creating Executors <ul><li>Factory methods in the Executors class </li></ul>public class Executors { static ExecutorService newSingleThreadedExecutor (); static ExecutorService newFixedThreadPool (int poolSize); static ExecutorService newCachedThreadPool (); static ScheduledExecutorService newScheduledThreadPool (); // Other methods not listed }
  65. 65. Thread Pool Example class WebService { public static void main(String[] args) { Executor pool = Executors.newFixedThreadPool(5); ServerSocket socket = new ServerSocket(999); for (;;) { final Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { new Handler().process ( connection ); } } pool.execute ( task ) ; } } } class Handler { void process (Socket s); }
  66. 66. ExecutorService for Lifecycle Support <ul><li>ExecutorService supports graceful and immediate shutdown </li></ul>public interface ExecutorService extends Executor { void shutdown (); List<Runnable> shutdownNow (); boolean isShutdown (); boolean isTerminated (); boolean awaitTermination (long timeout, TimeUnit unit); // additional methods not listed }
  67. 67. ScheduledExecutorService <ul><li>Deferred and recurring tasks </li></ul><ul><ul><li>Schedule execution of Callable or Runnable to run once after a fixed delay </li></ul></ul><ul><ul><ul><li>schedule () </li></ul></ul></ul><ul><ul><li>Schedule a Runnable to run periodically at a fixed rate </li></ul></ul><ul><ul><ul><li>scheduleAtFixedRate () </li></ul></ul></ul><ul><ul><li>Schedule a Runnable to run periodically with a fixed delay between executions </li></ul></ul><ul><ul><ul><li>scheduleWithFixedDelay () </li></ul></ul></ul><ul><li>Submission returns a ScheduledFuture </li></ul><ul><ul><li>Can be used to cancel task </li></ul></ul>
  68. 68. ScheduledExecutorService Example ScheduledExecutorService sched = Executors. newSingleThreadScheduledExecutor (); public void runTwiceAnHour(long howLong) { final Runnable rTask = new Runnable() { public void run() { /* Work to do */ } }; final ScheduledFuture<?> rTaskFuture = sched. scheduleAtFixedRate (rTask, 0, 1800, SECONDS); sched. schedule (new Runnable { public void run { rTaskFuture.cancel(true); } }, howLong, SECONDS); }
  69. 69. Synchronize Critical Section <ul><li>E.g., shared resource is an customer account. Certain methods called by multiple threads. </li></ul><ul><li>Hold monitor lock for as short a time as possible . </li></ul>synchronized double getBalance() { Account acct = verify(name, password); return acct.balance; } Lock held for long time double getBalance() { synchronized (this) { Account acct = verify(name, password); return acct.balance; } } Current object is locked Equivalent to above double getBalance() { Account acct = verify(name, password); synchronized (acct) { return acct.balance}; } Better Only acct object is locked – for shorter time
  70. 70. Locks <ul><li>Java provides basic locking via synchronized </li></ul><ul><li>Good for many situations, but some issues </li></ul><ul><ul><li>Single monitor per object </li></ul></ul><ul><ul><li>Not possible to interrupt thread waiting for lock </li></ul></ul><ul><ul><li>Not possible to time-out when waiting for a lock </li></ul></ul><ul><ul><li>Block structured approach </li></ul></ul><ul><ul><ul><li>Aquiring multiple locks is complex </li></ul></ul></ul><ul><ul><ul><li>Advanced techniques like hand-over-hand locking are not possible </li></ul></ul></ul><ul><li>New Lock interface addresses these issues </li></ul>
  71. 71. Lock Interface <ul><li>No automatic unlocking </li></ul>Interface Lock { void lock (); void lockInterruptibly () throws IE ; boolean tryLock (); boolean tryLock (long t, TimeUnit u) throws IE ; //returns true if lock is aquired void unlock (); Condition newCondition () throws UnsupportedOperationException; } IE = InterruptedException
  72. 72. RentrantLock <ul><li>Simplest concrete implementation of Lock </li></ul><ul><li>Same semantics as synchronized, but with more features </li></ul><ul><li>Generally better performance under contention than synchronized </li></ul><ul><li>Remember Lock is not automatically released </li></ul><ul><ul><li>Must use a finally block to release </li></ul></ul><ul><li>Multiple wait-sets supported </li></ul><ul><ul><li>Using Condition interface </li></ul></ul>
  73. 73. Lock Example Lock lock = new RentrantLock(); public void accessProtectedResource() throws IllegalMonitorStateException { lock.lock(); try { // Access lock protected resource } finally { // Ensure lock is always released lock.unlock(); } }
  74. 74. ReadWriteLock Interface <ul><li>Has two locks controlling read and write access </li></ul><ul><ul><li>Multiple threads can aquire the read lock if no threads have a write lock </li></ul></ul><ul><ul><li>Only one thread can aquire the write lock </li></ul></ul><ul><ul><li>Methods to access locks </li></ul></ul><ul><ul><li>rwl.readLock().lock(); </li></ul></ul><ul><ul><li>rwl.writeLock().lock(); </li></ul></ul><ul><ul><li>Better performance for read-mostly data access </li></ul></ul>
  75. 75. ReadWriteLock Example ReentrantReadWriteLock rwl = new ReentrantReadWriteLock (); Lock rLock = rwl.readLock (); Lock wLock = rwl.writeLock (); ArrayList<String> data = new ArrayList<String>(); public String getData(int pos) { r.lock () ; try { return data.get (pos); } finally { r.unlock (); } } public void addData(int pos, String value) { w.lock (); try { data.add (pos, value); } finally { w.unlock (); } }
  76. 76. Synchronizers <ul><li>Co-ordinate access and control </li></ul><ul><li>Semaphore </li></ul><ul><ul><li>Manages a fixed sized pool of resources </li></ul></ul><ul><li>CountDownLatch </li></ul><ul><ul><li>One or more threads wait for a set of threads to complete an action </li></ul></ul><ul><li>CyclicBarrier </li></ul><ul><ul><li>Set of threads wait until they all reach a specified point </li></ul></ul><ul><li>Exchanger </li></ul><ul><ul><li>Two threads reach a fixed point and exchange data </li></ul></ul>
  77. 77. BlockingQueue Interface <ul><li>Provides thread safe way for multiple threads to manipulate collection </li></ul>Interface BlockingQueue<E> { void put (E o) throws IE; boolean offer (E o) throws IE; boolean offer (E o, long t, TimeUnit u) throws IE; E take () throws IE; E poll () throws IE; E poll (long t, TimeUnit u) throws IE; int drainTo (Collection<? super E> c); int drainTo (Collection<? super E> c, int max); // Other methods not listed }
  78. 78. BlockingQueue Implementations <ul><li>ArrayBlockingQueue </li></ul><ul><ul><li>Bounded queue, backed by an array, FIFO </li></ul></ul><ul><li>LinkedBlockingQueue </li></ul><ul><ul><li>Optionally bounded queue, backed by linked nodes, FIFO </li></ul></ul><ul><li>PriorityBlockingQueue </li></ul><ul><ul><li>Unbounded queue </li></ul></ul><ul><ul><li>Uses comparator or natural ordering to determine the order of the queue </li></ul></ul>
  79. 79. Blocking Queue Example: 1 private BlockingQueue<String> msgQueue ; public Logger ( BlockingQueue<String> mq) { msgQueue = mq; } public void run() { try { while (true) { String message = msgQueue.take (); /* Log message */ } } catch (InterruptedException ie) { } }
  80. 80. Blocking Queue Example: 2 private ArrayBlockingQueue messageQueue = new ArrayBlockingQueue<String>(10); Logger logger = new Logger( messageQueue ); public void run() { String someMessage; try { while (true) { /* Do some processing */ /* Blocks if no space available */ messageQueue.put(someMessage) ; } } catch (InterruptedException ie) { } }
  81. 81. Concurrent Collections <ul><li>ConcurrentMap (interface) </li></ul><ul><ul><li>Extends Map interface with atomic operations </li></ul></ul><ul><li>ConcurrentHashMap </li></ul><ul><ul><li>Fully concurrent retrieval </li></ul></ul><ul><ul><li>Tunable concurrency for updates </li></ul></ul><ul><ul><ul><li>Constructor takes number of expected concurrent threads </li></ul></ul></ul><ul><li>ConcurrentLinkedQueue </li></ul><ul><ul><li>Unbounded, thread safe queue, FIFO </li></ul></ul><ul><li>CopyOnWriteArrayList </li></ul><ul><ul><li>Optimised for frequent iteration, infrequent modifications </li></ul></ul>
  82. 82. Summary <ul><li>New concurrency features are very powerful </li></ul><ul><li>Lots of great features </li></ul><ul><li>Take time to learn how to use them correctly </li></ul><ul><li>Use them! </li></ul>
  83. 83. For More Information
  84. 84. Garbage Collection
  85. 85. Classic Memory Leak in C <ul><li>User does the memory management </li></ul><ul><li>void service(int n, char** names) { </li></ul><ul><li>for (int i = 0; i < n; i++) { </li></ul><ul><ul><li>char* buf = (char*) malloc (strlen(names[i])); </li></ul></ul><ul><ul><li>strncpy(buf, names[i], strlen(names[i])); </li></ul></ul><ul><li>} </li></ul><ul><li>// memory leaked here </li></ul><ul><li>} </li></ul><ul><li>User is responsible for calling free() </li></ul><ul><li>User is vulnerable to dangling pointers and double frees. </li></ul>
  86. 86. Garbage Collection <ul><li>Find and reclaim unreachable objects </li></ul><ul><ul><li>Anything not transitively reachable from the application roots: </li></ul></ul><ul><ul><ul><li>(thread stacks, static fields, registers.) </li></ul></ul></ul><ul><ul><li>Traces the heap starting at the roots </li></ul></ul><ul><ul><ul><li>Visits every live object </li></ul></ul></ul><ul><ul><li>Anything not visited is unreachable </li></ul></ul><ul><ul><ul><li>Therefore garbage </li></ul></ul></ul><ul><li>Variety of approaches </li></ul><ul><ul><li>Compacting/non-compacting </li></ul></ul><ul><ul><li>Algorithms: copying, mark-sweep, mark-compact, etc. </li></ul></ul>
  87. 87. Generational Garbage Collection <ul><li>Keeps young and old objects separately </li></ul><ul><ul><li>In spaces called generations </li></ul></ul><ul><li>The weak generational hypothesis </li></ul><ul><ul><li>Most new objects will die young, Concentrate effort on young generation </li></ul></ul>Young Generation Old Generation Object Promotion Object Allocation Track These ( Remembered Set ) <ul><ul><ul><ul><li>Need to keep track of old-to-young pointers </li></ul></ul></ul></ul><ul><ul><ul><li>Eventually, have to also collect the old generation </li></ul></ul></ul><ul><ul><li>Different GC algorithms for each generation </li></ul></ul><ul><ul><ul><li>“ Use the right tool for the job” </li></ul></ul></ul>
  88. 88. Garbage Collection <ul><li>Garbage collection: Pros </li></ul><ul><ul><li>Increased reliability – no memory leaks, no dangling pointers </li></ul></ul><ul><ul><ul><li>Eliminates entire classes of (Pointer) bugs , no segmentation fault, no double frees </li></ul></ul></ul><ul><ul><li>Improved developer productivity </li></ul></ul><ul><ul><li>True memory leaks are not possible </li></ul></ul><ul><ul><ul><li>Still possible for an object to be reachable but not used by the program </li></ul></ul></ul><ul><ul><ul><li>Best described as unintentional object retention , Can cause OutOfMemoryError </li></ul></ul></ul><ul><ul><ul><li>Happens less often than in C, and easier to track down </li></ul></ul></ul><ul><li>Cons </li></ul><ul><ul><li>Pauses </li></ul></ul><ul><ul><li>May require tuning – but auto-tuning is getting better!!! </li></ul></ul>
  89. 89. Incremental Garbage Collection <ul><li>decreases/minimizes GC disruption </li></ul><ul><li>GC works at the same time as the application </li></ul><ul><ul><li>Short stop do a little work go back </li></ul></ul>
  90. 90. Garbage Collection <ul><li>Myths about garbage collection abound </li></ul><ul><ul><li>Myth: Allocation and garbage collection are slow </li></ul></ul><ul><ul><ul><li>In JDK 1.0 , they were slow (as was everything else) </li></ul></ul></ul><ul><ul><ul><li>Memory management (allocation + collection) in Java is often significantly faster than in C </li></ul></ul></ul><ul><ul><ul><ul><li>Cost of new Object() is typically ten machine instructions </li></ul></ul></ul></ul><ul><ul><ul><ul><li>It's just easier to see the collection cost because it happens all in one place </li></ul></ul></ul></ul><ul><li>Early performance advice suggested avoiding allocation </li></ul><ul><ul><li>Bad idea! </li></ul></ul><ul><ul><li>Alternatives (like object pooling ) are often slower , more error prone , and less memory-efficient </li></ul></ul>
  91. 91. Object Allocation (1/2) <ul><li>Typically, object allocation is very cheap! </li></ul><ul><ul><li>10 native instructions in the fast common case </li></ul></ul><ul><ul><li>C/C++ has faster allocation? No! </li></ul></ul><ul><li>Reclamation of new objects is very cheap too! </li></ul><ul><ul><li>Young GCs in generationa l systems </li></ul></ul><ul><li>So </li></ul><ul><ul><li>Do not be afraid to allocate small objects for intermediate results </li></ul></ul><ul><ul><li>Generational GCs love small, short-lived objects </li></ul></ul>
  92. 92. Object Allocation (2/2) <ul><li>We do not advise </li></ul><ul><ul><li>Needless allocation </li></ul></ul><ul><ul><ul><li>More frequent allocations will cause more frequent GCs </li></ul></ul></ul><ul><li>We do advise </li></ul><ul><ul><li>Using short-lived immutable objects instead of long-lived mutable objects </li></ul></ul><ul><ul><li>Using clearer, simpler code with more allocations instead of more obscure code with fewer allocations </li></ul></ul>
  93. 93. Large Objects <ul><li>Very large objects are: </li></ul><ul><ul><li>Expensive to allocate (maybe not through the fast path) </li></ul></ul><ul><ul><li>Expensive to initialize (zeroing) </li></ul></ul><ul><ul><li>Can cause performance issues </li></ul></ul><ul><li>Large objects of different sizes can cause fragmentation </li></ul><ul><ul><li>For non-compacting or partially-compacting GCs </li></ul></ul><ul><li>Avoid if you can </li></ul><ul><ul><li>And, yes, this is not always possible or desirable </li></ul></ul>
  94. 94. Object Pooling (1) <ul><li>Legacy of older VMs with terrible allocation performance </li></ul><ul><li>Remember </li></ul><ul><ul><li>Generational GCs love short-lived, immutable objects… </li></ul></ul><ul><li>Unused objects in pools </li></ul><ul><ul><li>Are like a bad tax, the GC must process them </li></ul></ul><ul><ul><li>Safety </li></ul></ul><ul><ul><ul><li>Reintroduce malloc/free mistakes </li></ul></ul></ul><ul><ul><li>Scalability </li></ul></ul><ul><ul><ul><li>Must allocate/de-allocate efficiently </li></ul></ul></ul><ul><ul><ul><li>synchronized defeats the VM’s fast allocation mechanism </li></ul></ul></ul>
  95. 95. Object Pooling (3/3) <ul><li>Exceptions </li></ul><ul><ul><li>Objects that are expensive to allocate and/or initialize </li></ul></ul><ul><ul><li>Objects that represent scarce resources </li></ul></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><ul><li>Threads pools </li></ul></ul></ul><ul><ul><ul><li>Database connection pools </li></ul></ul></ul><ul><ul><li>Use existing libraries wherever possible </li></ul></ul>
  96. 96. Memory Leaks? <ul><li>But, the GC is supposed to fix memory leaks! </li></ul><ul><li>The GC will collect all unreachable objects </li></ul><ul><li>But, it will not collect objects that are still reachable </li></ul><ul><li>Memory leaks in garbage collected heaps </li></ul><ul><ul><li>Objects that are reachable but unused </li></ul></ul><ul><ul><li>Unintentional object retention </li></ul></ul>
  97. 97. Memory Leak Types <ul><li>“Traditional” memory leaks </li></ul><ul><ul><li>Heap keeps growing , and growing, and growing … </li></ul></ul><ul><ul><li>OutOfMemoryError </li></ul></ul><ul><li>“Temporary” memory leaks </li></ul><ul><ul><li>Heap usage is temporarily very high , then it decreases </li></ul></ul><ul><ul><li>Bursts of frequent GCs </li></ul></ul>
  98. 98. Memory Leak Sources <ul><li>Objects in the wrong scope </li></ul><ul><li>Lapsed listeners </li></ul><ul><li>Exceptions change control flow </li></ul><ul><li>Instances of inner classes </li></ul><ul><li>Metadata mismanagement </li></ul><ul><li>Use of finalizers/reference objects </li></ul>
  99. 99. Objects in the Wrong Scope (1/2) <ul><li>Below, names really local to doIt() </li></ul><ul><ul><li>It will not be reclaimed while the instance of Foo is live </li></ul></ul><ul><li>class Foo { </li></ul><ul><li>private String[] names ; </li></ul><ul><li>public void doIt (int length) { </li></ul><ul><li>if (names == null || names.length < length) </li></ul><ul><li>names = new String[length]; </li></ul><ul><li>populate(names); </li></ul><ul><li>print(names); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  100. 100. Objects in the Wrong Scope (2/2) <ul><li>Remember </li></ul><ul><ul><li>Generational GCs love short-lived objects </li></ul></ul><ul><li>class Foo { </li></ul><ul><li>public void doIt(int length) { </li></ul><ul><li>String[] names = new String[length]; </li></ul><ul><li>populate(names); </li></ul><ul><li>print(names); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  101. 101. Memory Leak Sources <ul><li>Objects in the wrong scope </li></ul><ul><li>Lapsed listeners </li></ul><ul><li>Exceptions change control flow </li></ul><ul><li>Instances of inner classes </li></ul><ul><li>Metadata mismanagement </li></ul><ul><li>Use of finalizers/reference objects </li></ul>
  102. 102. Exceptions Change Control Flow (1/2) <ul><li>Beware </li></ul><ul><ul><li>Thrown exceptions can change control flow </li></ul></ul><ul><li> try { </li></ul><ul><li>ImageReader reader = new ImageReader(); </li></ul><ul><li>cancelButton.addActionListener(reader); </li></ul><ul><li>reader.readImage(inputFile); </li></ul><ul><li>cancelButton.removeActionListener(reader); </li></ul><ul><li>} catch (IOException e) { </li></ul><ul><li>// if thrown from readImage(), reader will not </li></ul><ul><li>// be removed from cancelButton's listener set </li></ul><ul><li>} </li></ul>
  103. 103. Exceptions Change Control Flow (2/2) <ul><li>Always use finally blocks </li></ul><ul><li> ImageReader reader = new ImageReader(); </li></ul><ul><li>cancelButton.addActionListener(reader); </li></ul><ul><li>try { </li></ul><ul><li>reader.readImage(inputFile); </li></ul><ul><li>} catch (IOException e) { </li></ul><ul><li>... </li></ul><ul><li>} finally { </li></ul><ul><li>cancelButton.removeActionListener(reader); </li></ul><ul><li>} </li></ul>
  104. 104. Memory Leak Sources <ul><li>Objects in the wrong scope </li></ul><ul><li>Lapsed listeners </li></ul><ul><li>Exceptions change control flow </li></ul><ul><li>Instances of inner classes </li></ul><ul><li>Metadata mismanagement </li></ul><ul><li>Use of finalizers/reference objects </li></ul>
  105. 105. Metadata Mismanagement (1/2) <ul><li>Sometimes, we want to: </li></ul><ul><ul><li>Keep track of object metadata </li></ul></ul><ul><ul><li>In a separate map </li></ul></ul><ul><li>class ImageManager { </li></ul><ul><li>private Map<Image,File> map = </li></ul><ul><li>new HashMap<Image,File>(); </li></ul><ul><li>public void add(Image image, File file) { ... } </li></ul><ul><li>public void remove(Image image) { ... } </li></ul><ul><li>Public File get(Image image) { ... } </li></ul><ul><li>} </li></ul>
  106. 106. Metadata Mismanagement (2/2) <ul><li>What happens if we forget to call remove (image)? </li></ul><ul><ul><li>never be removed from the map </li></ul></ul><ul><ul><li>Very common source of memory leaks </li></ul></ul><ul><li>We want: </li></ul><ul><ul><li>purge the corresponding entry when the key is not reachable… </li></ul></ul><ul><li>That’s exactly what a WeakHashMap does </li></ul><ul><ul><li>purge the corresponding entry </li></ul></ul><ul><li>private Map<Image,File> map = </li></ul><ul><li>new Weak HashMap<Image,File>(); </li></ul>
  107. 107. Some Memory Management Myths <ul><li>Myth: Explicitly nulling references helps GC </li></ul><ul><ul><li>Rarely helpful </li></ul></ul><ul><ul><ul><li>Unless you are managing your own memory </li></ul></ul></ul><ul><ul><li>Can be harmful to correctness or performance </li></ul></ul><ul><li>Myth: Calling System.gc() helps GC </li></ul><ul><ul><li>Triggers full collection – less efficient </li></ul></ul><ul><ul><li>Can be a huge performance loss </li></ul></ul><ul><li>Myth: Avoid object allocation </li></ul><ul><ul><li>Allocation in Java is lightning fast </li></ul></ul><ul><ul><ul><li>Avoidance techniques (e.g., pooling ) are very tricky to get right </li></ul></ul></ul>
  108. 108. Local Variable Nulling <ul><li>Local variable nulling i s n ot necessary </li></ul><ul><ul><li>The JIT can do liveness analysis </li></ul></ul><ul><li>void foo() { </li></ul><ul><li>int[] array = new int[1024]; </li></ul><ul><li>populate(array); </li></ul><ul><li>print(array); // last use of array in method foo() </li></ul><ul><li>array = null; // unnecessary! </li></ul><ul><li>// array is no longer considered live by the GC </li></ul><ul><li>... </li></ul><ul><li>} </li></ul>
  109. 109. Some Memory Management Myths <ul><li>Myth: Finalizers are Java's idea of destructors </li></ul><ul><ul><li>Finalizers are rarely needed and very hard to use correctly! </li></ul></ul><ul><ul><ul><li>Should only be used for native resources </li></ul></ul></ul><ul><ul><ul><li>Adds significant work to GC , has significant performance effect </li></ul></ul></ul><ul><ul><li>Instead, use finally blocks to release resources </li></ul></ul><ul><li>Resource r = acquireResource(); </li></ul><ul><li>try { </li></ul><ul><li>useResource(r); } finally { </li></ul><ul><li>releaseResource(r); </li></ul><ul><li>} </li></ul><ul><ul><ul><li>Note resource acquisition is outside the try block </li></ul></ul></ul><ul><ul><ul><li>Use for file handles, database connections, etc </li></ul></ul></ul>
  110. 110. Virtual Machine Smart Tuning
  111. 111. How “Smart Tuning” Works <ul><li>Provide good “ out of the box ” performance without hand tuning </li></ul><ul><li>Determine type of machine JVM is running on configure Hotspot appropriately </li></ul><ul><li>Server machine </li></ul><ul><ul><li>Larger heap, parallel garbage collector , and server compiler </li></ul></ul><ul><li>Client machine </li></ul><ul><ul><li>Same as 1.4.2 ( small heap , serial garbage collector, and client compiler </li></ul></ul>
  112. 112. “ Smart Tuning” <ul><li>Dynamically adjust Java HotSpot VM software environment at runtime </li></ul><ul><li>Adaptive Heap Sizing policy </li></ul><ul><li>Simple tuning options based on application requirements not JVM internals </li></ul>
  113. 113. Effects of Tuning Tuned vs. Non-tuned JVM
  114. 114. Hand Tuned vs. Smart Tuning
  115. 115. Monitoring & Management
  116. 116. Memory Leak Detection Tools <ul><li>Many tools to choose from </li></ul><ul><li>“ Is there a memory leak”? </li></ul><ul><ul><li>Monitor VM’s heap usage with jconsole and jstat </li></ul></ul><ul><li>“ Which objects are filling up the heap?” </li></ul><ul><ul><li>Get a class histogram with jmap or </li></ul></ul><ul><ul><li>-XX:+PrintClassHistogram and Ctrl-Break </li></ul></ul><ul><li>“ Why are these objects still reachable?” </li></ul><ul><ul><li>Get reachability analysis with jhat </li></ul></ul>
  117. 117. Monitoring, Management, Diagnostics <ul><li>GUI tools: JConsole, jhat, VisualGC (NetBeans), dynamic attach </li></ul><ul><li>Command line tools: jps, jstat, jstack, jmap, jinfo </li></ul><ul><li>Diagnostics: CTRL-Break handler, heap dump, better OutOfMemoryError and fatal error handling, JNI crashes </li></ul><ul><li>Tracing/logging: VM tracing and HotSpot probes, DTrace integration </li></ul>http://blogs.sun.com/roller/page/dannycoward/20060310
  118. 118. Monitoring and Management <ul><li>Attach on demand for </li></ul><ul><ul><li>jconsole : can connect to applications that did not start up with the JMX agent </li></ul></ul><ul><ul><li>jstack : takes a 'photograph' of all the threads and what they are up to in their own stack frames </li></ul></ul><ul><ul><li>jmap : takes a detailed 'photograph' of what's going on in memory at any one point in time </li></ul></ul><ul><ul><li>jhat : forensic expert that will help you interpret the result of jmap </li></ul></ul>
  119. 119. J2SE 5.0 Monitoring & Management
  120. 120. Platform Beans (MXBean's) <ul><li>Provides API access to </li></ul><ul><ul><li>number of classes loaded, </li></ul></ul><ul><ul><li>threads running </li></ul></ul><ul><ul><li>Thread state </li></ul></ul><ul><ul><li>contention stats </li></ul></ul><ul><ul><li>stack traces </li></ul></ul><ul><ul><li>GC statistics </li></ul></ul><ul><ul><li>memory consumption, low memory detection </li></ul></ul><ul><ul><li>VM uptime, system properties, input arguments </li></ul></ul><ul><ul><li>On-demand deadlock detection </li></ul></ul>
  121. 121. Jconsole http://www.netbeans.org/kb/articles/jmx-getstart.html
  122. 122. NetBeans Profiler <ul><li>Low overhead profiling </li></ul><ul><li>Attach to running applications </li></ul><ul><li>CPU performance profiling </li></ul><ul><li>Memory profiling </li></ul><ul><li>Memory leak debugging </li></ul><ul><li>Task based profiling </li></ul><ul><li>Processing collected data offline </li></ul><ul><li>http://www.netbeans.org/kb/55/profiler-tutorial.html </li></ul>
  123. 123. NetBeans Profiler
  124. 124. JDK 6 (Mustang) Features Overview
  125. 125. JDK 6 Component JSRs 202: Class File Update 199: Compiler API 269: Annotation Processors 260: Javadoc ™ Tag Update Ease of Development 221: JDBC ™ 4.0 223: Scripting 105: XML Digital Signature 173: Streaming API for XML XML 222: JAXB 2.0 250: Common Annotations 181: WS Metadata Web Services 224: JAX-WS 2.0 See JSR 270 at http://jcp.org
  126. 126. The JDK 6 Top 10 <ul><li>Scripting </li></ul><ul><li>Web Services </li></ul><ul><li>Database (JDBC 4.0, Java DB) </li></ul><ul><li>More Desktop APIs </li></ul><ul><li>Monitoring and Management </li></ul><ul><li>Compiler Access </li></ul><ul><li>Pluggable Annotations </li></ul><ul><li>Desktop Deployment </li></ul><ul><li>Security </li></ul><ul><li>Quality, Compatibility, Stability </li></ul>
  127. 127. Resources and Summary
  128. 128. For More Information (1/2) <ul><li>Memory management white paper </li></ul><ul><ul><li>http://java.sun.com/j2se/reference/whitepapers/ </li></ul></ul><ul><li>Destructors, Finalizers, and Synchronization </li></ul><ul><ul><li>http://portal.acm.org/citation.cfm?id=604153 </li></ul></ul><ul><li>Finalization, Threads, and the Java Technology Memory Model </li></ul><ul><ul><li>http://developers.sun.com/learning/javaoneonline/2005/coreplatform/TS-3281.html </li></ul></ul><ul><li>Memory-retention due to finalization article </li></ul><ul><ul><li>http://www.devx.com/Java/Article/30192 </li></ul></ul>
  129. 129. For More Information (2/2) <ul><li>FindBugs </li></ul><ul><ul><li>http://findbugs.sourceforge.net </li></ul></ul><ul><li>Heap analysis tools </li></ul><ul><ul><li>Monitoring and Management in 6.0 </li></ul></ul><ul><ul><ul><li>http://java.sun.com/developer/technicalArticles/J2SE/monitoring/ </li></ul></ul></ul><ul><ul><li>Troubleshooting guide </li></ul></ul><ul><ul><ul><li>http://java.sun.com/javase/6/webnotes/trouble/ </li></ul></ul></ul><ul><ul><li>JConsole </li></ul></ul><ul><ul><ul><li>http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html </li></ul></ul></ul>
  130. 130. Call for Action! <ul><li>Download and install JDK 5 or JDK 6 </li></ul><ul><li>Start using the new language features ASAP </li></ul>
  131. 131. Resources <ul><li>http://java.sun.com/javase </li></ul><ul><li>Java.net </li></ul><ul><li>http://jdk.dev.java.net </li></ul>
  132. 132. Stay in Touch with Java SE <ul><li>http://java.sun.com/javase </li></ul><ul><li>JDK Software Community </li></ul><ul><ul><li>planetjdk.org </li></ul></ul><ul><ul><li>community.java.net/jdk </li></ul></ul><ul><li>JDK 6 </li></ul><ul><ul><li>http://jdk6.dev.java.net/ </li></ul></ul><ul><ul><li>http://jcp.org/en/jsr/detail?id=270 </li></ul></ul><ul><li>JDK 7 </li></ul><ul><ul><li>http://jdk7.dev.java.net/ </li></ul></ul><ul><ul><li>http://jcp.org/en/jsr/detail?id=277 </li></ul></ul>
  133. 133. Thank You! <ul><li>Carol McDonald </li></ul><ul><ul><li>Java Technology Architect </li></ul></ul><ul><ul><ul><li>Sun Microsystems, Inc. </li></ul></ul></ul>

×