SlideShare a Scribd company logo
Java  Garbage Collection  Carol McDonald Java Architect Sun Microsystems, Inc.
Speaker Carol  cDonald:  Java Architect at Sun Microsystems Before Sun, worked on software development of:  Application to  manage Loans  for  Big Banks  (>10 million loans)  Pharmaceutical  Intranet  ( Roche  Switzerland)  Telecom  Network Mgmt  ( Digital  France)  X.400  Email Server  ( IBM  Germany)
Garbage  Collection
Classic Memory Leak in  C  User does the memory management void service(int n, char** names) { for (int i = 0; i < n; i++) { char* buf = (char*)  malloc (strlen(names[i])); strncpy(buf, names[i], strlen(names[i])); } // memory leaked here } User is responsible for calling  free() User is vulnerable to  dangling pointers and double frees.
Garbage Collection Find and reclaim  unreachable  objects not reachable from the application  roots: (thread stacks, static fields, registers.) Traces the  heap  starting at the  roots Visits every live object Anything not visited is  unreachable Therefore garbage Variety of approaches Algorithms: copying, mark-sweep, mark-compact, etc.
Garbage Collection Garbage collection:  Pros Increased reliability  – no memory leaks, no dangling pointers Eliminates  entire classes of  (Pointer)   bugs ,  no segmentation fault, no double frees Improved developer productivity True  memory leaks  are not possible possible for an object to be   reachable  but  not  used  by the program unintentional object retention ,  Can cause OutOfMemoryError Happens less often than in C, and easier to track down Cons Pauses
Statistics Most objects are very short lived 80-98% Old objects tend to live a long time avoid marking and sweeping the old
Generational Garbage Collection Keep young and old objects separate In spaces called  generations Different GC algorithms  for each generation “ Use the right tool for the job”
How Generational GC Works
Incremental Garbage Collection Minor Garbage Collection  (scavenge)  When eden is “full” a minor gc is invoked  Sweeps through eden and the current survivor space, removing the dead and moving the living to survivor space or old  Ss0 and ss1 switch which is “current” A new tenuring age is calculated Major Garbage Collection  When old is “full”  All spaces are garbage collected including perm space  All other activities in the jvm are suspended
New Old Space Tuning 25-40% should be new space how much new space depends on App: Stateless Request centric Application with high morbidity rate needs more new space for scalability Stateful Workflow Application with more older objects needs more old space
Garbage Collection Myths  about  garbage collection  abound Myth: Allocation and garbage collection  are slow In  JDK  1.0 , they were slow (as was everything else) Memory management (allocation + collection) in Java is often significantly faster than in C Cost of new Object() is typically ten machine instructions It's just easier to see the collection cost because it happens all in one place Early performance advice suggested  avoiding allocation Bad idea! Alternatives (like  object pooling ) are often  slower , more  error prone , and  less memory-efficient
Object Allocation (1/2) Typically, object allocation is  very cheap! 10 native instructions in the fast common case C/C++ has faster allocation? No! Reclamation of  new objects  is very cheap too! Young GCs  in  generationa l systems So Do not be afraid to allocate  small objects for intermediate results Generational GCs  love  small, short-lived objects
Object Allocation (2/2) We  do not  advise Needless  allocation More frequent allocations will cause more frequent GCs We  do  advise Using short-lived immutable objects instead of long-lived mutable objects Using  clearer,  simpler  code  with  more allocations  instead of more  obscure  code with fewer  allocations
Large Objects Very  large  objects are: Expensive to  allocate  (maybe not through the fast path) Expensive to  initialize  (zeroing) Can cause  performance  issues Large objects of different sizes can cause  fragmentation For non-compacting or partially-compacting GCs Avoid  if you can And, yes, this is not always possible or desirable
Object Pooling (1) Legacy of older VMs with terrible allocation performance Remember Generational GCs   love  short-lived, immutable objects… Unused objects in pools Are like a  bad  tax, the GC must process them Safety Reintroduce  malloc/free mistakes Scalability Must allocate/de-allocate efficiently synchronized  defeats the  VM’s  fast allocation mechanism
Object Pooling (3/3) Exceptions Objects that are  expensive  to  allocate  and/or  initialize Objects that represent scarce  resources Examples Threads pools Database connection  pools Use existing libraries wherever possible
Memory Leaks? But, the GC is supposed to fix memory leaks! The GC will collect all  unreachable  objects But, it will not collect objects that are  still  reachable Memory leaks in garbage collected heaps Objects that are  reachable  but  unused Unintentional object retention
Memory Leak Types “Traditional” memory leaks Heap keeps  growing , and  growing,  and  growing … OutOfMemoryError “Temporary” memory leaks Heap usage is  temporarily  very  high , then it  decreases Bursts of  frequent GCs
Memory Leak Sources Objects in the wrong scope Lapsed listeners Exceptions change control flow Instances of inner classes Metadata mismanagement Use of finalizers/reference objects
Objects in the Wrong Scope (1/2) Below, names really local to doIt() It will not be reclaimed while the instance of Foo is live class Foo { private String[]  names ; public void  doIt (int length) { if (names == null || names.length < length) names = new String[length]; populate(names); print(names); } }
Objects in the Wrong Scope (2/2) Remember Generational GCs love short-lived objects class Foo { public void doIt(int length) { String[] names = new String[length]; populate(names); print(names); } }
Memory Leak Sources Objects in the wrong scope Lapsed listeners Exceptions change control flow Instances of inner classes Metadata mismanagement Use of finalizers/reference objects
Exceptions Change Control Flow  (1/2) Beware Thrown exceptions can change control flow   try { ImageReader reader = new ImageReader(); cancelButton.addActionListener(reader); reader.readImage(inputFile); cancelButton.removeActionListener(reader); } catch (IOException e) { // if thrown from readImage(), reader will not // be removed from cancelButton's listener set }
Exceptions Change Control Flow  (2/2) Always use finally  blocks   ImageReader reader = new ImageReader(); cancelButton.addActionListener(reader); try { reader.readImage(inputFile); } catch (IOException e) { ... }  finally { cancelButton.removeActionListener(reader); }
Memory Leak Sources Objects in the wrong scope Lapsed listeners Exceptions change control flow Instances of inner classes Metadata mismanagement Use of finalizers/reference objects
Metadata Mismanagement (1/2) Sometimes, we want to: Keep track of object metadata In a separate map class ImageManager { private Map<Image,File> map = new HashMap<Image,File>(); public void add(Image image, File file) { ... } public void remove(Image image) { ... } Public File get(Image image) { ... } }
Metadata Mismanagement (2/2) What happens if we  forget  to call  remove (image)? never be removed from the map Very  common  source of  memory leaks We want: purge the corresponding entry when the key is not reachable… That’s  exactly  what a WeakHashMap does purge the corresponding entry private Map<Image,File> map = new  Weak HashMap<Image,File>();
Some Memory Management Myths Myth: Explicitly  nulling references  helps GC Rarely helpful Unless you are managing your own memory Can be  harmful  to correctness or performance Myth: Calling  System.gc()  helps GC Triggers full collection  – less efficient Can be a huge performance loss Myth:  Avoid object allocation Allocation  in Java is lightning  fast Avoidance techniques (e.g.,  pooling ) are very  tricky  to get right
Local Variable Nulling Local variable nulling i s n ot necessary The JIT can do liveness analysis void foo() { int[] array = new int[1024]; populate(array); print(array);  // last use of array in method foo() array = null;  // unnecessary! // array is no longer considered live by the GC ... }
Some Memory Management Myths Myth: Finalizers are Java's idea of destructors Finalizers are rarely needed  and very hard to use correctly! Should only be used for native resources Adds  significant work to GC , has  significant performance  effect Instead, use finally blocks to release resources Resource r = acquireResource(); try {  useResource(r); } finally {  releaseResource(r); } Note resource acquisition is outside the try block Use for  file handles, database connections,  etc
Virtual Machine Smart Tuning
How “Smart Tuning” Works Provide  good  “ out of the box ” performance  without  hand  tuning Determine type of machine  JVM is running on  configure Hotspot  appropriately Server  machine Larger heap,   parallel garbage collector , and server compiler Client  machine Same as 1.4.2 ( small heap ,  serial garbage  collector, and client compiler
“ Smart Tuning” Dynamically adjust  Java HotSpot VM software environment at  runtime Adaptive Heap Sizing policy Simple tuning options  based on application requirements not JVM internals
Effects of Tuning Tuned vs. Non-tuned JVM
Hand Tuned vs. Smart Tuning
Monitoring & Management
Memory Leak Detection Tools Many tools to choose from “ Is there a memory leak”? Monitor  VM’s   heap  usage with  jconsole  and  jstat “ Which objects are filling up the heap?” Get a class histogram with   jmap  or -XX:+PrintClassHistogram and Ctrl-Break “ Why are these objects still reachable?” Get  reachability  analysis with  jhat
Monitoring, Management, Diagnostics GUI tools: JConsole, jhat, VisualGC (NetBeans), dynamic attach Command line tools: jps, jstat, jstack, jmap, jinfo Diagnostics: CTRL-Break handler, heap dump, better OutOfMemoryError and fatal error handling, JNI crashes Tracing/logging: VM tracing and HotSpot probes, DTrace integration http://blogs.sun.com/roller/page/dannycoward/20060310
Monitoring and Management Attach on demand for  jconsole : can connect to applications that did not start up with the JMX agent jstack : takes a 'photograph' of all the threads and what they are up to in their own stack frames jmap : takes a detailed 'photograph' of what's going on in memory at any one point in time jhat : forensic expert that will help you interpret the result of jmap
Jconsole http://www.netbeans.org/kb/articles/jmx-getstart.html
NetBeans Profiler Low overhead profiling Attach to running applications CPU  performance profiling Memory  profiling Memory leak  debugging Task based profiling Processing collected data offline http://www.netbeans.org/kb/55/profiler-tutorial.html
NetBeans Profiler
Demo http://www.javapassion.com/handsonlabs/5116_nbprofilermemory.zip Memory leak detection with Netbeans Profiler
VisualVM A new Integrated and Extensible Troubleshooting Tool for the Java Platform Integrates existing JDK Management, Monitoring and Troubleshooting tools and adds support for lightweight CPU and Memory profiling Extensible through VisualVM Plugins Center Production and development time tool Audience: developers, administrators, performance and sustaining engineers, etc. https://visualvm.dev.java.net
VisualVM Features (1/3) Monitor local & remote Java applications Show configuration & environment Monitor performance, memory, classes...
VisualVM Features (2/3) Monitor threads Profile performance & memory Take & display thread dumps
VisualVM Features (3/3) Take & browse/analyze heap dumps Analyze core dumps Take & display application snapshots
Plugins Sampler MBeans Browser Visual GC BTrace Buffer Monitor ME Snapshot Viewer GlassFish (+GFPM) OQL Editor TDA Plugin (3 rd  p.) OSGi Plugin (3 rd  p.) Message Queue (GF) Sun Grid Engine Inspect https://visualvm.dev.java.net/plugins.html
Resources and Summary
For More Information (1/2) Memory management white paper http://java.sun.com/j2se/reference/whitepapers/ Destructors, Finalizers, and Synchronization http://portal.acm.org/citation.cfm?id=604153  Memory-retention due to finalization article http://www.devx.com/Java/Article/30192
For More Information (2/2) FindBugs http://findbugs.sourceforge.net Heap analysis tools Monitoring and Management  http://java.sun.com/developer/technicalArticles/J2SE/monitoring/ Troubleshooting guide http://java.sun.com/javase/6/webnotes/trouble/ JConsole http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
Resources Performance, Monitoring and Management, Testing, and Debugging of Java Applications  http://www.javapassion.com/javaperformance/ http://netbeans.org/kb/docs/java/profiler-intro.html http://www.netbeans.org/community/magazine/html/04/profiler.html
Resources Performance, Monitoring and Management, Testing, and Debugging of Java Applications  Monitoring and Management in 6.0  http://java.sun.com/developer/technicalArticles/J2SE/monitoring/  Troubleshooting guide  http://java.sun.com/javase/6/webnotes/trouble/  JConsole  http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
Stay in Touch with Java SE  http://java.sun.com/javase JDK 6 http://jdk6.dev.java.net/ http://jcp.org/en/jsr/detail?id=270 JDK 7 http://jdk7.dev.java.net/ http://jcp.org/en/jsr/detail?id=277
Thank You! Carol McDonald Java Technology Architect Sun Microsystems, Inc.

More Related Content

ODP
Quick introduction to Java Garbage Collector (JVM GC)
PPT
An Introduction to JVM Internals and Garbage Collection in Java
PDF
Let's talk about Garbage Collection
PDF
Java Garbage Collection - How it works
PPT
Garbage collection in JVM
PPTX
Java GC
PDF
Debugging Your Production JVM
PDF
Understanding Java Garbage Collection - And What You Can Do About It
Quick introduction to Java Garbage Collector (JVM GC)
An Introduction to JVM Internals and Garbage Collection in Java
Let's talk about Garbage Collection
Java Garbage Collection - How it works
Garbage collection in JVM
Java GC
Debugging Your Production JVM
Understanding Java Garbage Collection - And What You Can Do About It

What's hot (20)

PDF
Understanding Java Garbage Collection
PDF
Performance and predictability (1)
PDF
How long can you afford to Stop The World?
PDF
Performance and predictability
PDF
Collections forceawakens
PPT
Basic Garbage Collection Techniques
PDF
JCConf 2020 - New Java Features Released in 2020
PPT
Java Performance Tuning
PDF
Java Performance Tuning
PDF
Jvm profiling under the hood
PDF
Java collections the force awakens
PPT
Jvm Performance Tunning
PPT
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
PPTX
HotSpot JVM Tuning
PDF
Why GC is eating all my CPU?
PDF
Concurrency Concepts in Java
PDF
[JavaOne 2011] Models for Concurrent Programming
PDF
Exploiting Concurrency with Dynamic Languages
PPTX
Seeing with Python presented at PyCon AU 2014
PPTX
Garbage collection
Understanding Java Garbage Collection
Performance and predictability (1)
How long can you afford to Stop The World?
Performance and predictability
Collections forceawakens
Basic Garbage Collection Techniques
JCConf 2020 - New Java Features Released in 2020
Java Performance Tuning
Java Performance Tuning
Jvm profiling under the hood
Java collections the force awakens
Jvm Performance Tunning
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
HotSpot JVM Tuning
Why GC is eating all my CPU?
Concurrency Concepts in Java
[JavaOne 2011] Models for Concurrent Programming
Exploiting Concurrency with Dynamic Languages
Seeing with Python presented at PyCon AU 2014
Garbage collection
Ad

Similar to Java Garbage Collection, Monitoring, and Tuning (20)

PPTX
ConFoo - Exploring .NET’s memory management – a trip down memory lane
PPTX
Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...
PPTX
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
PPTX
Exploring .NET memory management - JetBrains webinar
PPTX
Exploring .NET memory management (iSense)
PDF
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
ODP
Garbage Collection in Hotspot JVM
PPTX
DotNetFest - Let’s refresh our memory! Memory management in .NET
PDF
performance optimization: Memory
PPTX
CodeStock - Exploring .NET memory management - a trip down memory lane
PDF
Mobile Developer Summit 2012, Pune
PPTX
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
PPT
.Net Garbage Collector 101
PPTX
dotMemory 4 - What's inside?
PDF
Memory Leaks in Android Applications
PPTX
Memory Management in the Java Virtual Machine(Garbage collection)
PDF
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
PPTX
How & why-memory-efficient?
PPT
Handling Exceptions In C &amp; C++ [Part B] Ver 2
PPTX
NET Systems Programming Learned the Hard Way.pptx
ConFoo - Exploring .NET’s memory management – a trip down memory lane
Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
Exploring .NET memory management - JetBrains webinar
Exploring .NET memory management (iSense)
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
Garbage Collection in Hotspot JVM
DotNetFest - Let’s refresh our memory! Memory management in .NET
performance optimization: Memory
CodeStock - Exploring .NET memory management - a trip down memory lane
Mobile Developer Summit 2012, Pune
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
.Net Garbage Collector 101
dotMemory 4 - What's inside?
Memory Leaks in Android Applications
Memory Management in the Java Virtual Machine(Garbage collection)
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
How & why-memory-efficient?
Handling Exceptions In C &amp; C++ [Part B] Ver 2
NET Systems Programming Learned the Hard Way.pptx
Ad

More from Carol McDonald (20)

PDF
Introduction to machine learning with GPUs
PDF
Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...
PDF
Analyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DB
PDF
Analysis of Popular Uber Locations using Apache APIs: Spark Machine Learning...
PDF
Predicting Flight Delays with Spark Machine Learning
PDF
Structured Streaming Data Pipeline Using Kafka, Spark, and MapR-DB
PDF
Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...
PDF
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...
PDF
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...
PDF
How Big Data is Reducing Costs and Improving Outcomes in Health Care
PDF
Demystifying AI, Machine Learning and Deep Learning
PDF
Spark graphx
PDF
Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...
PDF
Streaming patterns revolutionary architectures
PDF
Spark machine learning predicting customer churn
PDF
Fast Cars, Big Data How Streaming can help Formula 1
PDF
Applying Machine Learning to Live Patient Data
PDF
Streaming Patterns Revolutionary Architectures with the Kafka API
PPTX
Apache Spark Machine Learning Decision Trees
PDF
Advanced Threat Detection on Streaming Data
Introduction to machine learning with GPUs
Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...
Analyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DB
Analysis of Popular Uber Locations using Apache APIs: Spark Machine Learning...
Predicting Flight Delays with Spark Machine Learning
Structured Streaming Data Pipeline Using Kafka, Spark, and MapR-DB
Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...
How Big Data is Reducing Costs and Improving Outcomes in Health Care
Demystifying AI, Machine Learning and Deep Learning
Spark graphx
Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...
Streaming patterns revolutionary architectures
Spark machine learning predicting customer churn
Fast Cars, Big Data How Streaming can help Formula 1
Applying Machine Learning to Live Patient Data
Streaming Patterns Revolutionary Architectures with the Kafka API
Apache Spark Machine Learning Decision Trees
Advanced Threat Detection on Streaming Data

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PDF
Modernizing your data center with Dell and AMD
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
KodekX | Application Modernization Development
PDF
Encapsulation theory and applications.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Electronic commerce courselecture one. Pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
Cloud computing and distributed systems.
Modernizing your data center with Dell and AMD
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
Encapsulation theory and applications.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Electronic commerce courselecture one. Pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Digital-Transformation-Roadmap-for-Companies.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Understanding_Digital_Forensics_Presentation.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
The Rise and Fall of 3GPP – Time for a Sabbatical?

Java Garbage Collection, Monitoring, and Tuning

  • 1. Java Garbage Collection Carol McDonald Java Architect Sun Microsystems, Inc.
  • 2. Speaker Carol cDonald: Java Architect at Sun Microsystems Before Sun, worked on software development of: Application to manage Loans for Big Banks (>10 million loans) Pharmaceutical Intranet ( Roche Switzerland) Telecom Network Mgmt ( Digital France) X.400 Email Server ( IBM Germany)
  • 4. Classic Memory Leak in C User does the memory management void service(int n, char** names) { for (int i = 0; i < n; i++) { char* buf = (char*) malloc (strlen(names[i])); strncpy(buf, names[i], strlen(names[i])); } // memory leaked here } User is responsible for calling free() User is vulnerable to dangling pointers and double frees.
  • 5. Garbage Collection Find and reclaim unreachable objects not reachable from the application roots: (thread stacks, static fields, registers.) Traces the heap starting at the roots Visits every live object Anything not visited is unreachable Therefore garbage Variety of approaches Algorithms: copying, mark-sweep, mark-compact, etc.
  • 6. Garbage Collection Garbage collection: Pros Increased reliability – no memory leaks, no dangling pointers Eliminates entire classes of (Pointer) bugs , no segmentation fault, no double frees Improved developer productivity True memory leaks are not possible possible for an object to be reachable but not used by the program unintentional object retention , Can cause OutOfMemoryError Happens less often than in C, and easier to track down Cons Pauses
  • 7. Statistics Most objects are very short lived 80-98% Old objects tend to live a long time avoid marking and sweeping the old
  • 8. Generational Garbage Collection Keep young and old objects separate In spaces called generations Different GC algorithms for each generation “ Use the right tool for the job”
  • 10. Incremental Garbage Collection Minor Garbage Collection (scavenge) When eden is “full” a minor gc is invoked Sweeps through eden and the current survivor space, removing the dead and moving the living to survivor space or old Ss0 and ss1 switch which is “current” A new tenuring age is calculated Major Garbage Collection When old is “full” All spaces are garbage collected including perm space All other activities in the jvm are suspended
  • 11. New Old Space Tuning 25-40% should be new space how much new space depends on App: Stateless Request centric Application with high morbidity rate needs more new space for scalability Stateful Workflow Application with more older objects needs more old space
  • 12. Garbage Collection Myths about garbage collection abound Myth: Allocation and garbage collection are slow In JDK 1.0 , they were slow (as was everything else) Memory management (allocation + collection) in Java is often significantly faster than in C Cost of new Object() is typically ten machine instructions It's just easier to see the collection cost because it happens all in one place Early performance advice suggested avoiding allocation Bad idea! Alternatives (like object pooling ) are often slower , more error prone , and less memory-efficient
  • 13. Object Allocation (1/2) Typically, object allocation is very cheap! 10 native instructions in the fast common case C/C++ has faster allocation? No! Reclamation of new objects is very cheap too! Young GCs in generationa l systems So Do not be afraid to allocate small objects for intermediate results Generational GCs love small, short-lived objects
  • 14. Object Allocation (2/2) We do not advise Needless allocation More frequent allocations will cause more frequent GCs We do advise Using short-lived immutable objects instead of long-lived mutable objects Using clearer, simpler code with more allocations instead of more obscure code with fewer allocations
  • 15. Large Objects Very large objects are: Expensive to allocate (maybe not through the fast path) Expensive to initialize (zeroing) Can cause performance issues Large objects of different sizes can cause fragmentation For non-compacting or partially-compacting GCs Avoid if you can And, yes, this is not always possible or desirable
  • 16. Object Pooling (1) Legacy of older VMs with terrible allocation performance Remember Generational GCs love short-lived, immutable objects… Unused objects in pools Are like a bad tax, the GC must process them Safety Reintroduce malloc/free mistakes Scalability Must allocate/de-allocate efficiently synchronized defeats the VM’s fast allocation mechanism
  • 17. Object Pooling (3/3) Exceptions Objects that are expensive to allocate and/or initialize Objects that represent scarce resources Examples Threads pools Database connection pools Use existing libraries wherever possible
  • 18. Memory Leaks? But, the GC is supposed to fix memory leaks! The GC will collect all unreachable objects But, it will not collect objects that are still reachable Memory leaks in garbage collected heaps Objects that are reachable but unused Unintentional object retention
  • 19. Memory Leak Types “Traditional” memory leaks Heap keeps growing , and growing, and growing … OutOfMemoryError “Temporary” memory leaks Heap usage is temporarily very high , then it decreases Bursts of frequent GCs
  • 20. Memory Leak Sources Objects in the wrong scope Lapsed listeners Exceptions change control flow Instances of inner classes Metadata mismanagement Use of finalizers/reference objects
  • 21. Objects in the Wrong Scope (1/2) Below, names really local to doIt() It will not be reclaimed while the instance of Foo is live class Foo { private String[] names ; public void doIt (int length) { if (names == null || names.length < length) names = new String[length]; populate(names); print(names); } }
  • 22. Objects in the Wrong Scope (2/2) Remember Generational GCs love short-lived objects class Foo { public void doIt(int length) { String[] names = new String[length]; populate(names); print(names); } }
  • 23. Memory Leak Sources Objects in the wrong scope Lapsed listeners Exceptions change control flow Instances of inner classes Metadata mismanagement Use of finalizers/reference objects
  • 24. Exceptions Change Control Flow (1/2) Beware Thrown exceptions can change control flow try { ImageReader reader = new ImageReader(); cancelButton.addActionListener(reader); reader.readImage(inputFile); cancelButton.removeActionListener(reader); } catch (IOException e) { // if thrown from readImage(), reader will not // be removed from cancelButton's listener set }
  • 25. Exceptions Change Control Flow (2/2) Always use finally blocks ImageReader reader = new ImageReader(); cancelButton.addActionListener(reader); try { reader.readImage(inputFile); } catch (IOException e) { ... } finally { cancelButton.removeActionListener(reader); }
  • 26. Memory Leak Sources Objects in the wrong scope Lapsed listeners Exceptions change control flow Instances of inner classes Metadata mismanagement Use of finalizers/reference objects
  • 27. Metadata Mismanagement (1/2) Sometimes, we want to: Keep track of object metadata In a separate map class ImageManager { private Map<Image,File> map = new HashMap<Image,File>(); public void add(Image image, File file) { ... } public void remove(Image image) { ... } Public File get(Image image) { ... } }
  • 28. Metadata Mismanagement (2/2) What happens if we forget to call remove (image)? never be removed from the map Very common source of memory leaks We want: purge the corresponding entry when the key is not reachable… That’s exactly what a WeakHashMap does purge the corresponding entry private Map<Image,File> map = new Weak HashMap<Image,File>();
  • 29. Some Memory Management Myths Myth: Explicitly nulling references helps GC Rarely helpful Unless you are managing your own memory Can be harmful to correctness or performance Myth: Calling System.gc() helps GC Triggers full collection – less efficient Can be a huge performance loss Myth: Avoid object allocation Allocation in Java is lightning fast Avoidance techniques (e.g., pooling ) are very tricky to get right
  • 30. Local Variable Nulling Local variable nulling i s n ot necessary The JIT can do liveness analysis void foo() { int[] array = new int[1024]; populate(array); print(array); // last use of array in method foo() array = null; // unnecessary! // array is no longer considered live by the GC ... }
  • 31. Some Memory Management Myths Myth: Finalizers are Java's idea of destructors Finalizers are rarely needed and very hard to use correctly! Should only be used for native resources Adds significant work to GC , has significant performance effect Instead, use finally blocks to release resources Resource r = acquireResource(); try { useResource(r); } finally { releaseResource(r); } Note resource acquisition is outside the try block Use for file handles, database connections, etc
  • 33. How “Smart Tuning” Works Provide good “ out of the box ” performance without hand tuning Determine type of machine JVM is running on configure Hotspot appropriately Server machine Larger heap, parallel garbage collector , and server compiler Client machine Same as 1.4.2 ( small heap , serial garbage collector, and client compiler
  • 34. “ Smart Tuning” Dynamically adjust Java HotSpot VM software environment at runtime Adaptive Heap Sizing policy Simple tuning options based on application requirements not JVM internals
  • 35. Effects of Tuning Tuned vs. Non-tuned JVM
  • 36. Hand Tuned vs. Smart Tuning
  • 38. Memory Leak Detection Tools Many tools to choose from “ Is there a memory leak”? Monitor VM’s heap usage with jconsole and jstat “ Which objects are filling up the heap?” Get a class histogram with jmap or -XX:+PrintClassHistogram and Ctrl-Break “ Why are these objects still reachable?” Get reachability analysis with jhat
  • 39. Monitoring, Management, Diagnostics GUI tools: JConsole, jhat, VisualGC (NetBeans), dynamic attach Command line tools: jps, jstat, jstack, jmap, jinfo Diagnostics: CTRL-Break handler, heap dump, better OutOfMemoryError and fatal error handling, JNI crashes Tracing/logging: VM tracing and HotSpot probes, DTrace integration http://blogs.sun.com/roller/page/dannycoward/20060310
  • 40. Monitoring and Management Attach on demand for jconsole : can connect to applications that did not start up with the JMX agent jstack : takes a 'photograph' of all the threads and what they are up to in their own stack frames jmap : takes a detailed 'photograph' of what's going on in memory at any one point in time jhat : forensic expert that will help you interpret the result of jmap
  • 42. NetBeans Profiler Low overhead profiling Attach to running applications CPU performance profiling Memory profiling Memory leak debugging Task based profiling Processing collected data offline http://www.netbeans.org/kb/55/profiler-tutorial.html
  • 45. VisualVM A new Integrated and Extensible Troubleshooting Tool for the Java Platform Integrates existing JDK Management, Monitoring and Troubleshooting tools and adds support for lightweight CPU and Memory profiling Extensible through VisualVM Plugins Center Production and development time tool Audience: developers, administrators, performance and sustaining engineers, etc. https://visualvm.dev.java.net
  • 46. VisualVM Features (1/3) Monitor local & remote Java applications Show configuration & environment Monitor performance, memory, classes...
  • 47. VisualVM Features (2/3) Monitor threads Profile performance & memory Take & display thread dumps
  • 48. VisualVM Features (3/3) Take & browse/analyze heap dumps Analyze core dumps Take & display application snapshots
  • 49. Plugins Sampler MBeans Browser Visual GC BTrace Buffer Monitor ME Snapshot Viewer GlassFish (+GFPM) OQL Editor TDA Plugin (3 rd p.) OSGi Plugin (3 rd p.) Message Queue (GF) Sun Grid Engine Inspect https://visualvm.dev.java.net/plugins.html
  • 51. For More Information (1/2) Memory management white paper http://java.sun.com/j2se/reference/whitepapers/ Destructors, Finalizers, and Synchronization http://portal.acm.org/citation.cfm?id=604153 Memory-retention due to finalization article http://www.devx.com/Java/Article/30192
  • 52. For More Information (2/2) FindBugs http://findbugs.sourceforge.net Heap analysis tools Monitoring and Management http://java.sun.com/developer/technicalArticles/J2SE/monitoring/ Troubleshooting guide http://java.sun.com/javase/6/webnotes/trouble/ JConsole http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
  • 53. Resources Performance, Monitoring and Management, Testing, and Debugging of Java Applications http://www.javapassion.com/javaperformance/ http://netbeans.org/kb/docs/java/profiler-intro.html http://www.netbeans.org/community/magazine/html/04/profiler.html
  • 54. Resources Performance, Monitoring and Management, Testing, and Debugging of Java Applications Monitoring and Management in 6.0 http://java.sun.com/developer/technicalArticles/J2SE/monitoring/ Troubleshooting guide http://java.sun.com/javase/6/webnotes/trouble/ JConsole http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
  • 55. Stay in Touch with Java SE http://java.sun.com/javase JDK 6 http://jdk6.dev.java.net/ http://jcp.org/en/jsr/detail?id=270 JDK 7 http://jdk7.dev.java.net/ http://jcp.org/en/jsr/detail?id=277
  • 56. Thank You! Carol McDonald Java Technology Architect Sun Microsystems, Inc.

Editor's Notes

  • #4: Lets look at some of the changes and new features in the Java Virtual Machine
  • #5: Why are we here? In C/C++ you do the memory management. You make the calls to malloc() and the calls to free(). Forget the calls to free() and you&apos;re leaking memory.. And, of course, you don&apos;t use the memory once it&apos;s been freed. And, you free memory exactly once.
  • #6: In a nutshell, a GC ... Our garbage collectors are generational, meaning we divide the heap into two regions and don&apos;t have to always collect the entire heap. When we only collect one of the regions we call it a minor collection. Minor collections are typically much faster than major collections and often collect enough memory so as to delay the more expensive major collection.
  • #7: So garbage collection is your friend. The source of some ugly bugs has removed. You spend more time on the interesting stuff. You don&apos;t have to think about memory management as much in your design. But there are some costs. Your going to have pauses in the application execution when a GC occurs. You don&apos;t know when a GC is going to occur and don&apos;t know how long it is going to take. Finalization depends on GC&apos;s. User&apos;s of you programs may want to choose among the different collectors to achieve a particular performance (e.g., better throughput or shorter pause times). Some tuning may be required.
  • #8: When Sun did the original work on the HotSpot development they did a lot of analysis of applications and how they behaved with respect to the VM (as we saw in the earlier slide showing the pie charts). Part of this analysis revealed some interesting data about the typical lifetime of objects. As we see here most objects are very short lived. Knowing this has a significant impact on the choice of algorithms used for GC and the design of the heap layout.
  • #9: Java does the memory management for you. The JVM finds the data that is still in use by the program. This data is referred to a reachable. Anything else is collected as garbage. You never have the equivalent of a dangling pointer. If you have a reference to data, it&apos;s there, it has not been collected as garbage. No free&apos;s obviously means no double frees. In principle you cannot have a true memory leak but there are things that you can do that are as bad in practice. Basically, you have a reference to data that is never going to be used again. This is more accurately described as unintential object retention but is often just called a memory leak. If your program has such a memory leak, you&apos;ll ...
  • #10: When we first allocate object, we treat it as Eden space it is stack based allocation, we allocation chunk of memory where we maintain a pointer to the beginning of it, and move the pointer along, putting the object in that space. No search for free list, very efficient. When Eden space is full, we do GC on this, we stamp the valid objects, if the object is valid we copy it from Eden to semi spaces “from space”, GC pause is directly proportional to total size of live objects, so this done very efficiently, ... do a copy into then “to space”, that&apos;s what we call Tenuring by doing this we are maturing objects. Most of the objects in Eden space are very young and short lived, and in 2 semi spaces are a little bit long lived. We actually tune how long the objects are going to stay in that young generation. We then copy all those valid objects from semi space to old generation. Again use simple stack based allocation to allocate the objects. For old generation, we have a different GC algorithm, may be incremental, mark-sweep compact, we have choices what we do that. Also another space is called permanent space, it&apos;s used for classes information. You don&apos;t allocate or put things in those objects, the VM will actually use it for classes information. Default sizes: 64 KB for semi space is not very large, so we will talk how you can change that. Survivor ration is eden and 2 semi spaces, changing the value is going to impact the performance ??? Young generation fits for copy collect while old is more for the others
  • #11: The point here is to make it so that the garbage collection proces is not as disruptive to your application. So the garbage collector works at the same time as your application , short stop do a little work then go back, so that you dont see one long pause. Has some overhead that lowers throughput a little. The young generatons collections are short already so there is no need to put that extra overhead on the young generation We only do incremental of the old generation
  • #13: Before going further let me tell you about memory management on a modern Java platform. Allocation is definitely not slow. It was slow in ... Garbage collection has gotten much, much faster than in the early days but a collection does still happen all at the same time so it&apos;s noticeable. We don&apos;t use reference counting. That&apos;s notable because reference counting does slow down the execution of the program. Because early performance was an issue, there&apos;s some lingering advice on how to get better performance. Much of that is out dated. And some of the bad advice actually leads to memory leaks.
  • #14: Memory allocation is fast , really cheap You don&apos;t have to keep track of the remembered set, tracking pointers from old to young, does not have to be done for younger objects. Short lived objects can be reclaimed very fast
  • #19: Okay, so we don&apos;t have true memory leak, right? But we can hold onto objects that are never going to be used again. You can find plenty of examples of such objects ... In the best case such objects cause more work for the garbage collector. In the worst case you can get an out-of-memory exception because of them. In the next few slides we&apos;ll look at three examples of these types of memory leaks.
  • #22: In this example an object that stays around longer than it is needed. “ byteArray” is part of “LeakyChecksum” so will live as long as the LeakyChecksum object live. It is. however only. needed during the invocation of geFileChecksum. Now maybe this is ok, but realize that byteArray is going to be as large as the largest file ever read, the garbage collector has to look at it at each collection, and the space would be used more profitably for the allocation of other objects.
  • #28: This third example of a memory leak is less easy to workaround. You have an object and you want to associate some information with that object but you cannot put the information in the object itself. In this case you have a socket and want to associate a user id with the socket. A natural solution is to create a map between the socket and the user id as here in the SocketManager. Here the example uses a HashMap w
  • #29: Here&apos;s the example with a fix using the WeakHasMap. WeakHashMap give you the direct connection between the key and the metadata that you need here. Don&apos;t replace all your HashMaps with WeakHashMaps. Reference processing does cost during GC and it would be a waste to always use it.
  • #30: Have a explicit reason if you are going to null a reference. Mostly it doesn&apos;t help. Occasionally it&apos;s exactly the wrong thing to do. A System.gc() will trigger an full collections. In tuning the GC we often try hard to minimize full collections. Understand why you are doing System.gc(). Allocation is fast so just use it. Object pooling has costs in terms of filling up the heap so, again, understand what you are doing.
  • #32: You are not guaranteed that a finalizer will ever run so, if you use them, you need design for that contigency. Regarding finalization we mostly hear from people who are trying to manage a scarce native resource which is probably the wrong thing to do. Try to use finally block first. That&apos;s the simplest and most deterministic.
  • #33: Lets look at some of the changes and new features in the Java Virtual Machine
  • #34: The big goal of “smart tuning” sometimes referred to as ergonomics, was good out-of-the-box performance for server applications. From the early days the VM has been tuned to run well with desktop applications because the overwelling majority of executions were for desktop applications. That hurts when customers run benchmarks for large server applications because that is often done without tuning the VM. In tiger we look at the machine we&apos;re running on and try to make some smarter choices. We&apos;ve also added a simplified way of tuning garbage collection.
  • #36: This slide shows the effects of tuning on 4 benchmarks. This is without “Smart tuning”. Bigger is better. The 1.4.2 untuned VM is in blue and the hand tuned tiger VM is in red. Tuning can make a big difference. Business logic – specjbb2000 Bytecodes – specjvm98 i/o – jetstream Scientific – scimark2
  • #37: This is tiger tuned versus out-of-the-box performance on the same benchmarks. The blue is the out-of-the-box performance for tiger and the red again is the hand tuned tiger VM. Smart tuning has made tiger out-of-the-box performance is much closer to the tuned performance.
  • #38: Lets look at some of the changes and new features in the Java Virtual Machine
  • #45: Lets look at some of the changes and new features in the Java Virtual Machine