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

What's hot (20)

PDF
Understanding Java Garbage Collection
Azul Systems Inc.
 
PDF
Performance and predictability (1)
RichardWarburton
 
PDF
How long can you afford to Stop The World?
Java Usergroup Berlin-Brandenburg
 
PDF
Performance and predictability
RichardWarburton
 
PDF
Collections forceawakens
RichardWarburton
 
PPT
Basic Garbage Collection Techniques
An Khuong
 
PDF
JCConf 2020 - New Java Features Released in 2020
Joseph Kuo
 
PPT
Java Performance Tuning
Minh Hoang
 
PDF
Java Performance Tuning
Atthakorn Chanthong
 
PDF
Jvm profiling under the hood
RichardWarburton
 
PDF
Java collections the force awakens
RichardWarburton
 
PPT
Jvm Performance Tunning
guest1f2740
 
PPT
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
Emery Berger
 
PPTX
HotSpot JVM Tuning
Gilad Garon
 
PDF
Why GC is eating all my CPU?
Roman Elizarov
 
PDF
Concurrency Concepts in Java
Doug Hawkins
 
PDF
[JavaOne 2011] Models for Concurrent Programming
Tobias Lindaaker
 
PDF
Exploiting Concurrency with Dynamic Languages
Tobias Lindaaker
 
PPTX
Seeing with Python presented at PyCon AU 2014
Mark Rees
 
PPTX
Garbage collection
Somya Bagai
 
Understanding Java Garbage Collection
Azul Systems Inc.
 
Performance and predictability (1)
RichardWarburton
 
How long can you afford to Stop The World?
Java Usergroup Berlin-Brandenburg
 
Performance and predictability
RichardWarburton
 
Collections forceawakens
RichardWarburton
 
Basic Garbage Collection Techniques
An Khuong
 
JCConf 2020 - New Java Features Released in 2020
Joseph Kuo
 
Java Performance Tuning
Minh Hoang
 
Java Performance Tuning
Atthakorn Chanthong
 
Jvm profiling under the hood
RichardWarburton
 
Java collections the force awakens
RichardWarburton
 
Jvm Performance Tunning
guest1f2740
 
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
Emery Berger
 
HotSpot JVM Tuning
Gilad Garon
 
Why GC is eating all my CPU?
Roman Elizarov
 
Concurrency Concepts in Java
Doug Hawkins
 
[JavaOne 2011] Models for Concurrent Programming
Tobias Lindaaker
 
Exploiting Concurrency with Dynamic Languages
Tobias Lindaaker
 
Seeing with Python presented at PyCon AU 2014
Mark Rees
 
Garbage collection
Somya Bagai
 

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

PDF
TechGIG_Memory leaks in_java_webnair_26th_july_2012
Ashish Bhasin
 
PPT
Java programing considering performance
Roger Xia
 
PPTX
Garbage collection
Anand Srinivasan
 
PDF
Jvm is-your-friend
ColdFusionConference
 
PDF
The JVM is your friend
Kai Koenig
 
PPT
Java Performance Monitoring & Tuning
Muhammed Shakir
 
PDF
A New Age of JVM Garbage Collectors (Clojure Conj 2019)
Alexander Yakushev
 
PPTX
Memory Management in the Java Virtual Machine(Garbage collection)
Prashanth Kumar
 
ODP
Garbage collection
Mudit Gupta
 
PPT
Gc Atomic
bufanliu
 
PDF
Java Garbage Collector and The Memory Model
Ernesto Arroyo Ron
 
DOCX
Garbage collector complete information
Mubarak Hussain
 
PDF
Memory Management with Java and C++
Mohammad Shaker
 
PPTX
Javasession10
Rajeev Kumar
 
DOC
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
sidg75
 
ODP
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 
PPTX
Java garbage collection & GC friendly coding
Md Ayub Ali Sarker
 
PPTX
An efficient memory system for java Garbage Collection
Rohit Deshpande
 
PDF
[BGOUG] Java GC - Friend or Foe
SAP HANA Cloud Platform
 
PPTX
Intro to Garbage Collection
Monica Beckwith
 
TechGIG_Memory leaks in_java_webnair_26th_july_2012
Ashish Bhasin
 
Java programing considering performance
Roger Xia
 
Garbage collection
Anand Srinivasan
 
Jvm is-your-friend
ColdFusionConference
 
The JVM is your friend
Kai Koenig
 
Java Performance Monitoring & Tuning
Muhammed Shakir
 
A New Age of JVM Garbage Collectors (Clojure Conj 2019)
Alexander Yakushev
 
Memory Management in the Java Virtual Machine(Garbage collection)
Prashanth Kumar
 
Garbage collection
Mudit Gupta
 
Gc Atomic
bufanliu
 
Java Garbage Collector and The Memory Model
Ernesto Arroyo Ron
 
Garbage collector complete information
Mubarak Hussain
 
Memory Management with Java and C++
Mohammad Shaker
 
Javasession10
Rajeev Kumar
 
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
sidg75
 
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 
Java garbage collection & GC friendly coding
Md Ayub Ali Sarker
 
An efficient memory system for java Garbage Collection
Rohit Deshpande
 
[BGOUG] Java GC - Friend or Foe
SAP HANA Cloud Platform
 
Intro to Garbage Collection
Monica Beckwith
 
Ad

More from Carol McDonald (20)

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

Recently uploaded (20)

PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 

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