SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
Java Garbage Collection Study


  Mark Volkmann and Brian Gilstrap
       Object Computing, Inc.
             July 2008
Java GC
• Java objects are eligible for garbage collection (GC),
  which frees their memory
  and possibly associated resources,
  when they are no longer reachable
• Two stages of GC for an object
   •   finalization - runs finalize method on the object
   •   reclamation - reclaims memory used by the object

• In Java 5 & 6 there are four GC algorithms
  from which to choose
   • but one of those won’t be supported in the future,
       so we’ll just consider the three that will live on
   •   serial, throughput and concurrent low pause




                                                            2
GC Process
                  • Basic steps
                      •   object is determined to be unreachable
                      •   if object has a finalize method                   The JVM has a finalizer thread that is
                                                                            used for running finalize methods.
                          • object is added to a finalization queue          Long running finalize methods
                                                                            do not prevent a GC pass from completing
                          • at some point it’s finalize method is invoked   and do not freeze the application.
                              so the object can free associated resources

                       • object memory is reclaimed
                  •   Issues with finalize methods
                      •   makes every GC pass do more work
                      •   if a finalize method runs for a long time, it can
                          delay execution of finalize methods of other objects
                      •   may create new strong references to objects that had none,
These are                 preventing their GC
good reasons
to avoid using        •   run in a nondeterministic order
finalize
methods in            •   no guarantee they will be called; app. may exit first
safety-critical
code.
                                                                                                                       3
Kinds of Object References
• Strong references
                                             some           soft, weak or                   target or
    • the normal type        object phantom object “referent” object

•   Other reference types in java.lang.ref package
    • SoftReference
        • GC’ed any time after there are no strong references to the referent,
            but is typically retained until memory is low
        • can be used to implement caches of objects that can be recreated if needed
    •   WeakReference                                   For soft and weak references, the get method
                                                        returns null when the referent object has been GC’ed.
        • GC’ed any time after there are no strong or soft references to the referent
        • often used for “canonical mappings” where each object
            has a unique identifier (one-to-one), and in collections of “listeners”

    •   PhantomReference
        • GC’ed any time after there are no strong, soft or weak references to the referent
        • typically used in conjunction with a ReferenceQueue
            to manage cleanup of native resources associated with the object
            without using finalize methods (more on this later)

     Also see java.util.WeakHashMap.

                                                                                                                4
Alternative to Finalization
• Don’t write finalize method
  in a class whose objects have associated
  native resources that require cleanup
   •   call this class A

• Instead, do the following for each such class A
   •   create a new class, B, that extends one of the reference types
       • WeakReference, SoftReference or PhantomReference
   •   create one or more ReferenceQueue objects
   •    a B constructor that takes an A
       and passes that, along with a ReferenceQueue object,
       to the superclass constructor
   •   create a B object for each A object
   •   iteratively call remove on the ReferenceQueue
       • free resources associated with returned B object
       • often this is done in a separate thread   When there is an associated ReferenceQueue,
                                                   weak and soft reference are added to it before the
                                                   referent object has been finalized and reclaimed.
                                                   Phantom references are added to it after these occur.
                                                                                                           5
GC Metrics
• Different types of applications have
    different concerns related to GC
• Throughput
    • percentage of the total run time not spent performing GC
•   Pauses
    •   times when the application code stops running
        while GC is performed
    •   interested in the number of pauses,
        their average duration and their maximum duration

• Footprint
    • current heap size (amount of memory) being used
•   Promptness
    •   how quickly memory from objects no longer needed is freed



                                                                    6
Generational GC
• All of the GC algorithms used by Java are
  variations on the concept of generational GC
• Generational GC assumes that
   •   the most recently created objects are the ones
       that are most likely to become unreachable soon
       • for example, objects created in a method and only referenced
           by local variables that go out of scope when the method exits

   •   the longer an object remains reachable,
       the less likely it is to be eligible for GC soon (or ever)

• Objects are divided into “generations” or “spaces”
   •   Java categories these with the names
       “young”, “tenured” and “perm”
   •   objects can move from one space to another during a GC




                                                                           7
Object Spaces
• Hold objects of similar ages or generations
   •   “young” spaces hold recently created objects
       and can be GC’ed in a “minor” or “major” collection
   •   “tenured” space hold objects that
       have survived some number of minor collections
       and can be GC’ed only in a major collection
   •   “perm” space hold objects needed by the JVM, such as
       Class & Method objects, their byte code, and interned Strings
           • GC of this space results in classes being “unloaded”

• Size of each space
   •   determined by current heap size
       (which can change during runtime)
       and several tuning options


 eden            survivor 1 survivor 2 tenured                      perm
 (young)            (young)    (young)


                                                                           8
Young Spaces
• Eden space
  •   holds objects created after the last GC,
      except those that belong in the perm space
  •   during a minor collection, these objects are
      either GC’ed or moved to a survivor space

• Survivor spaces
  •   these spaces hold young objects
      that have survived at least one GC
  •   during a minor collection, these objects are
      either GC’ed or moved to the other survivor space

• Minor collections
  •   tend to be fast compared to major collections because
      only a subset of the objects need to be examined
  •   typically occur much more frequently than major collections



                                                                    9
GC Running Details
• Three approaches
                        • serial collector does this for minor and major collections
1. Stop the world       • throughput collector does this for major collections
   •   when a GC pass is started, it runs to completion
       before the application is allowed to run again


2. Incremental       none of the Java GC algorithms do this

   •   a GC pass can alternate between doing part of the work
       and letting the application run for a short time,
       until the GC pass is completed

                   • throughput collector does this for minor collections
3. Concurrent      • concurrent low pause collector does this for minor and major collections
   •   a GC pass runs concurrently with the application
       so the application is only briefly stopped



                                                                                           10
When Does GC Occur?
• Impacted by heap size
    •   from reference #1 (see last slide) ...
    •   “If a heap size is small, collection will be fast
        but the heap will fill up more quickly,
        thus requiring more frequent collections.”
    •   “Conversely, a large heap will take longer to fill up
        and thus collections will be less frequent,
        but they take longer.”

• Minor collections
    • occur when a young space approaches being full
•   Major collections
    •   occur when the tenured space approaches being full




                                                               11
GC Algorithms
                                                     options specified with -XX: are
•   Serial: -XX:+UseSerialGC                         turned on with + and off with -

    •   uses the same thread as the application
        for minor and major collections

• Throughput: -XX:+UseParallelGC
    •   uses multiple threads for minor, but not major, collections
        to reduce pause times
    •   good when multiple processors are available,
        the app. will have a large number of short-lived objects,
        and there isn’t a pause time constraint

• Concurrent Low Pause: -XX:+UseConcMarkSweepGC
    •   only works well when objects are created by multiple threads?
    •   uses multiple threads for minor and major collections
        to reduce pause times
    •   good when multiple processors are available,
        the app. will have a large number of long-lived objects,
        and there is a pause time constraint
                                                                                   12
Ergonomics
• Sun refers to automatic selection of default options
  based on hardware and OS characteristics
  as “ergonomics”
• A “server-class machine” has
   •   more than one processor
   •   at least 2GB of memory
   •   isn’t running Windows on a 32 bit processor




                                                         13
Ergonomics ...
• Server-class machine
    • optimized for overall performance
    • uses throughput collector
    • uses server runtime compiler
    • sets starting heap size = 1/64 of memory up to 1GB
    • sets maximum heap size = 1/4 of memory up to 1GB
•   Client-class machine
    •   optimized for fast startup and small memory usage
    •   targeted at interactive applications
    •   uses serial collector
    •   uses client runtime compiler
    •   starting and maximum heap size defaults?




                                                            14
GC Monitoring
• There are several options that cause
  the details of GC operations to be output
   • -verbose:gc
       • outputs a line of basic information about each collection
   •   -XX:+PrintGCTimeStamps
       • outputs a timestamp at the beginning of each line
   •   -XX:+PrintGCDetails
       • implies -verbose:gc
       • outputs additional information about each collection
   •   -Xloggc:gc.log
       • implies -verbose:gc and -XX:+PrintGCTimeStamps
       • directs GC output to a file instead of stdout

• Specifying the 3rd and 4th option gives all four


                                                                     15
Basic GC Tuning
                                          See http://java.sun.com/docs/hotspot/gc5.0/
•   Recommend approach                    ergo5.html, section 4 ”Tuning Strategy”

    •   set a few goals that are used to adjust the tuning options
    1. throughput goal -XX:GCTimeRatio=n
        • What percentage of the total run time should be spent
            doing application work as opposed to performing GC?

    2. maximum pause time goal -XX:MaxGCPauseMillis=n
        • What is the maximum number of milliseconds
            that the application should pause for a single GC?

    3. footprint goal
        • if the other goals have been met, reduce the heap size until
            one of the previous goals can no longer be met, then increase it




                                                                                        16
Advanced GC Tuning
•   -Xms=n (starting) and -Xmx=n (maximum) heap size
    • these affect the sizes of the object spaces
•   -XX:MinHeapFreeRatio=n, -XX:MaxHeapFreeRatio=n
    • bounds on ratio of unused/free space to space occupied by live objects
    • heap space grows and shrinks after each GC to maintain this,
        limited by the maximum heap size

•   -XX:NewSize=n, -XX:MaxNewSize=n
    • default and max young size (eden + survivor 1 + survivor 2)
•   -XX:NewRatio=n
    • ratio between young size and tenured size
•   -XX:SurvivorRatio=n
    • ratio between the size of each survivor space and eden
•   -XX:MaxPermSize=n
    • upper bound on perm size
•   -XX:TargetSurvivorRatio=n
    • target percentage of survivor space used after each GC

                                                                               17
Even More GC Tuning
•   -XX:+DisableExplicitGC
    • when on, calls to System.gc() do not result in a GC
    • off by default
•   -XX:+ScavengeBeforeFullGC
    • when on, perform a minor collection before each major collection
    • on by default
•   -XX:+UseGCOverheadLimit
    • when on, if 98% or more of the total time is spent performing GC,
        an OutOfMemoryError is thrown
    • on by default




                                                                          18
The Testing Framework

                                                        5
                       Ant build.xml                        ChartCreator.java

                                  1              4

      gc.properties        iterate.rb    *   gcloganalyzer.rb              JFreeChart


         2   *         3
                                                                  gc.csv     gc.png
ScriptRunner.java     GCExerciser.java         gc.log

                                                            time.txt
   script.xml




                                                                                        19
gc.properties
# Details about property to be varied.
property.name=gc.pause.max
display.name=Max Pause Goal
start.value=0
end.value=200
step.size=20


processor.bits = 64


# Heap size details.
heap.size.start = 64M
heap.size.max = 1G




                                         20
gc.properties ...
# Garbage collection algorithm
# UseSerialGC -> serial collector
# UseParallelGC -> throughput collector
# UseConcMarkSweepGC -> concurrent low pause collector
gc.algorithm.option=UseParallelGC


# Maximum Pause Time Goal
# This is the goal for the maximum number of milliseconds
# that the application will pause for GC.
gc.pause.max = 50


# Throughput Goal
# This is the goal for the ratio between
# the time spent performing GC and the application time.
# The percentage goal for GC will be 1 / (1 + gc.time.ratio).
# A value of 49 gives 2% GC or 98% throughput.
gc.time.ratio = 49




                                                                21
gc.properties ...
# The number of objects to be created.
object.count = 25
       No
# TheSc
           ne
        rip of
      size of the data in each object. 1MB

           tR the
object.size = 1000000
             un se
               ne
                  r a prop
# The number of milliseconds that a reference should be
                     nd e cannot be GCed.
# held to each object, so itrt
                       sc ies
                         rip
                             t.x are
object.time.to.live = 30000

                                ml us
                                  are ed i
# The number of milliseconds between object creations.
time.between.creations = 30           us f
                                        ed
                                           !
# The number of milliseconds to run
# after all the objects have been created.
time.to.run.after.creations = 1000




                                                          22
script.xml
• Here’s an example of a script file
   •   object elements create an object of a given size and lifetime
   •   work elements simulate doing work between object creations
   •   note the support for loops, including nested loops
  <script>
    <object size="1M" life="30"/>
    <work time="200"/>
    <loop times="3">
       <object size="2M" life="50"/>
       <work time="500"/>
       <loop times="2">
         <object size="3M" life="50"/>
         <work time="500"/>
       </loop>
    </loop>
  </script>



                                                                       23
iterate.rb
1. Obtains properties in gc.properties
2. Iterates property.name from start.value
   to end.value in steps of step.size
   and passes the value to the run method
3. The run method
   1. runs ScriptRunner.java which
      reads script.xml and processes the steps in it
      by invoking methods of GCExcercizer.java
      to produce gc.log and time.txt
   2. runs gcloganalyzer.rb which adds a line to gc.csv




                                                          24
GCExerciser.java
1. Obtains properties in gc.properties and
   properties specified on the “java” command line to override them
   • for iterating through a range of property values
2. Creates object.count objects
   that each have a size of object.size and are
   scheduled to live for object.time.to.live milliseconds
3. Each object is placed into a TreeSet that is sorted based on
   the time at which the object should be removed from the TreeSet
   •   makes the object eligible for GC
4. After each object is created, the TreeSet is repeatedly searched
   for objects that are ready to be removed until
   time.between.creations milliseconds have elapsed
5. After all the objects have been created, the TreeSet is repeatedly
   searched for object that are ready to be removed until
   time.to.run.after.creations milliseconds have elapsed
6. Write the total run time to time.txt


                                                                        25
ChartCreator.java
• Uses the open-source library JFreeChart
  to create a line graph showing the throughput
  for various values of a property that affects GC
• Example




                                                     26
Further Work
• Possibly consider the following modifications
  to the GC test framework
  • have the objects refer to a random number of other objects
  • have each object know about the objects that refer to it
      so it can ask them to release their references
  • use ScheduledExecutorService to initiate an object releasing itself
  • run multiple times with the same options and average the results
  • make each run much longer ... perhaps 10 minutes




                                                                          27
References
1. “Memory Management in the
   Java HotSpot Virtual Machine”
   •   http://java.sun.com/javase/technologies/hotspot/gc/
       memorymanagement_whitepaper.pdf
2. “Tuning Garbage Collection with
   the 5.0 Java Virtual Machine”
   • http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html
3. “Ergonomics in the 5.0 Java Virtual Machine”
    • http://java.sun.com/docs/hotspot/gc5.0/ergo5.html
4. “Garbage Collection in the
   Java HotSpot Virtual Machine”
   •   http://www.devx.com/Java/Article/21977/




                                                               28

Weitere ähnliche Inhalte

Was ist angesagt?

Combining the strength of erlang and Ruby
Combining the strength of erlang and RubyCombining the strength of erlang and Ruby
Combining the strength of erlang and RubyMartin Rehfeld
 
The economies of scaling software - Abdel Remani
The economies of scaling software - Abdel RemaniThe economies of scaling software - Abdel Remani
The economies of scaling software - Abdel Remanijaxconf
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...smallerror
 
Billions of hits: Scaling Twitter (Web 2.0 Expo, SF)
Billions of hits: Scaling Twitter (Web 2.0 Expo, SF)Billions of hits: Scaling Twitter (Web 2.0 Expo, SF)
Billions of hits: Scaling Twitter (Web 2.0 Expo, SF)John Adams
 
Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"Paolo Negri
 
A tour of Java and the JVM
A tour of Java and the JVMA tour of Java and the JVM
A tour of Java and the JVMAlex Birch
 
Design patterns
Design patternsDesign patterns
Design patternsAlok Guha
 
What Drove Wordnik Non-Relational?
What Drove Wordnik Non-Relational?What Drove Wordnik Non-Relational?
What Drove Wordnik Non-Relational?DATAVERSITY
 
Deep Learning Intro - Georgia Tech - CSE6242 - March 2015
Deep Learning Intro - Georgia Tech - CSE6242 - March 2015Deep Learning Intro - Georgia Tech - CSE6242 - March 2015
Deep Learning Intro - Georgia Tech - CSE6242 - March 2015Josh Patterson
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Andrei KUCHARAVY
 
John adams talk cloudy
John adams   talk cloudyJohn adams   talk cloudy
John adams talk cloudyJohn Adams
 
Developing in the Cloud
Developing in the CloudDeveloping in the Cloud
Developing in the CloudRyan Cuprak
 
Que nos espera a los ALM Dudes para el 2013?
Que nos espera a los ALM Dudes para el 2013?Que nos espera a los ALM Dudes para el 2013?
Que nos espera a los ALM Dudes para el 2013?Bruno Capuano
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the WildTomer Gabel
 

Was ist angesagt? (15)

Combining the strength of erlang and Ruby
Combining the strength of erlang and RubyCombining the strength of erlang and Ruby
Combining the strength of erlang and Ruby
 
The economies of scaling software - Abdel Remani
The economies of scaling software - Abdel RemaniThe economies of scaling software - Abdel Remani
The economies of scaling software - Abdel Remani
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
 
Scalable game-servers-tgc
Scalable game-servers-tgcScalable game-servers-tgc
Scalable game-servers-tgc
 
Billions of hits: Scaling Twitter (Web 2.0 Expo, SF)
Billions of hits: Scaling Twitter (Web 2.0 Expo, SF)Billions of hits: Scaling Twitter (Web 2.0 Expo, SF)
Billions of hits: Scaling Twitter (Web 2.0 Expo, SF)
 
Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"
 
A tour of Java and the JVM
A tour of Java and the JVMA tour of Java and the JVM
A tour of Java and the JVM
 
Design patterns
Design patternsDesign patterns
Design patterns
 
What Drove Wordnik Non-Relational?
What Drove Wordnik Non-Relational?What Drove Wordnik Non-Relational?
What Drove Wordnik Non-Relational?
 
Deep Learning Intro - Georgia Tech - CSE6242 - March 2015
Deep Learning Intro - Georgia Tech - CSE6242 - March 2015Deep Learning Intro - Georgia Tech - CSE6242 - March 2015
Deep Learning Intro - Georgia Tech - CSE6242 - March 2015
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1
 
John adams talk cloudy
John adams   talk cloudyJohn adams   talk cloudy
John adams talk cloudy
 
Developing in the Cloud
Developing in the CloudDeveloping in the Cloud
Developing in the Cloud
 
Que nos espera a los ALM Dudes para el 2013?
Que nos espera a los ALM Dudes para el 2013?Que nos espera a los ALM Dudes para el 2013?
Que nos espera a los ALM Dudes para el 2013?
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the Wild
 

Andere mochten auch

Java GC - Pause tuning
Java GC - Pause tuningJava GC - Pause tuning
Java GC - Pause tuningekino
 
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»Anna Shymchenko
 
Java Garbage Collection(GC)- Study
Java Garbage Collection(GC)- StudyJava Garbage Collection(GC)- Study
Java Garbage Collection(GC)- StudyDhanu Gupta
 
Николай Папирный Тема: "Java memory model для простых смертных"
Николай Папирный Тема: "Java memory model для простых смертных"Николай Папирный Тема: "Java memory model для простых смертных"
Николай Папирный Тема: "Java memory model для простых смертных"Ciklum Minsk
 
Java Memory Consistency Model - concepts and context
Java Memory Consistency Model - concepts and contextJava Memory Consistency Model - concepts and context
Java Memory Consistency Model - concepts and contextTomek Borek
 
Java gc and JVM optimization
Java gc  and JVM optimizationJava gc  and JVM optimization
Java gc and JVM optimizationRajan Jethva
 
What you need to know about GC
What you need to know about GCWhat you need to know about GC
What you need to know about GCKelum Senanayake
 
Tuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesTuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesSergey Podolsky
 
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 PresentationGC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 PresentationLudovic Poitou
 

Andere mochten auch (20)

[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
 
Java GC - Pause tuning
Java GC - Pause tuningJava GC - Pause tuning
Java GC - Pause tuning
 
Java Memory Model
Java Memory ModelJava Memory Model
Java Memory Model
 
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
 
Java Garbage Collection(GC)- Study
Java Garbage Collection(GC)- StudyJava Garbage Collection(GC)- Study
Java Garbage Collection(GC)- Study
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Николай Папирный Тема: "Java memory model для простых смертных"
Николай Папирный Тема: "Java memory model для простых смертных"Николай Папирный Тема: "Java memory model для простых смертных"
Николай Папирный Тема: "Java memory model для простых смертных"
 
Java Memory Model
Java Memory ModelJava Memory Model
Java Memory Model
 
Java Memory Model
Java Memory ModelJava Memory Model
Java Memory Model
 
Java Memory Consistency Model - concepts and context
Java Memory Consistency Model - concepts and contextJava Memory Consistency Model - concepts and context
Java Memory Consistency Model - concepts and context
 
Java gc and JVM optimization
Java gc  and JVM optimizationJava gc  and JVM optimization
Java gc and JVM optimization
 
What you need to know about GC
What you need to know about GCWhat you need to know about GC
What you need to know about GC
 
Java memory model
Java memory modelJava memory model
Java memory model
 
The Java Memory Model
The Java Memory ModelThe Java Memory Model
The Java Memory Model
 
Java GC, Off-heap workshop
Java GC, Off-heap workshopJava GC, Off-heap workshop
Java GC, Off-heap workshop
 
How long can you afford to Stop The World?
How long can you afford to Stop The World?How long can you afford to Stop The World?
How long can you afford to Stop The World?
 
JVM及其调优
JVM及其调优JVM及其调优
JVM及其调优
 
Tuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesTuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issues
 
The Java memory model made easy
The Java memory model made easyThe Java memory model made easy
The Java memory model made easy
 
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 PresentationGC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
 

Ähnlich wie Java gc

Java Garbage Collector and The Memory Model
Java Garbage Collector and The Memory ModelJava Garbage Collector and The Memory Model
Java Garbage Collector and The Memory ModelErnesto Arroyo Ron
 
Memory Management & Garbage Collection
Memory Management & Garbage CollectionMemory Management & Garbage Collection
Memory Management & Garbage CollectionAbhishek Sur
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves.NET UY Meetup
 
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, EtsyLessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, EtsyLucidworks
 
Lessons from Sharding Solr
Lessons from Sharding SolrLessons from Sharding Solr
Lessons from Sharding SolrGregg Donovan
 
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAn Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAbhishek Asthana
 
Building High-Throughput, Low-Latency Pipelines in Kafka
Building High-Throughput, Low-Latency Pipelines in KafkaBuilding High-Throughput, Low-Latency Pipelines in Kafka
Building High-Throughput, Low-Latency Pipelines in Kafkaconfluent
 
Java garbage collection & GC friendly coding
Java garbage collection  & GC friendly codingJava garbage collection  & GC friendly coding
Java garbage collection & GC friendly codingMd Ayub Ali Sarker
 
Low latency Java apps
Low latency Java appsLow latency Java apps
Low latency Java appsSimon Ritter
 
Performance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage CollectionPerformance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage CollectionHaribabu Nandyal Padmanaban
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraJon Haddad
 
Introducing Infinispan
Introducing InfinispanIntroducing Infinispan
Introducing InfinispanPT.JUG
 
Webinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in ProductionWebinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in ProductionDataStax Academy
 
Webinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in ProductionWebinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in ProductionDataStax Academy
 
Apache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM AlternativeApache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM AlternativeAndrus Adamchik
 

Ähnlich wie Java gc (20)

Java Garbage Collector and The Memory Model
Java Garbage Collector and The Memory ModelJava Garbage Collector and The Memory Model
Java Garbage Collector and The Memory Model
 
OOP in JS
OOP in JSOOP in JS
OOP in JS
 
Memory Management & Garbage Collection
Memory Management & Garbage CollectionMemory Management & Garbage Collection
Memory Management & Garbage Collection
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves
 
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, EtsyLessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
 
Lessons from Sharding Solr
Lessons from Sharding SolrLessons from Sharding Solr
Lessons from Sharding Solr
 
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAn Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
 
Building High-Throughput, Low-Latency Pipelines in Kafka
Building High-Throughput, Low-Latency Pipelines in KafkaBuilding High-Throughput, Low-Latency Pipelines in Kafka
Building High-Throughput, Low-Latency Pipelines in Kafka
 
Java garbage collection & GC friendly coding
Java garbage collection  & GC friendly codingJava garbage collection  & GC friendly coding
Java garbage collection & GC friendly coding
 
Garbage Collection .Net
Garbage Collection .NetGarbage Collection .Net
Garbage Collection .Net
 
Low latency Java apps
Low latency Java appsLow latency Java apps
Low latency Java apps
 
Performance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage CollectionPerformance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage Collection
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - Cassandra
 
Javasession10
Javasession10Javasession10
Javasession10
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Introducing Infinispan
Introducing InfinispanIntroducing Infinispan
Introducing Infinispan
 
Webinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in ProductionWebinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in Production
 
Webinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in ProductionWebinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in Production
 
Apache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM AlternativeApache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM Alternative
 

Kürzlich hochgeladen

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Kürzlich hochgeladen (20)

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

Java gc

  • 1. Java Garbage Collection Study Mark Volkmann and Brian Gilstrap Object Computing, Inc. July 2008
  • 2. Java GC • Java objects are eligible for garbage collection (GC), which frees their memory and possibly associated resources, when they are no longer reachable • Two stages of GC for an object • finalization - runs finalize method on the object • reclamation - reclaims memory used by the object • In Java 5 & 6 there are four GC algorithms from which to choose • but one of those won’t be supported in the future, so we’ll just consider the three that will live on • serial, throughput and concurrent low pause 2
  • 3. GC Process • Basic steps • object is determined to be unreachable • if object has a finalize method The JVM has a finalizer thread that is used for running finalize methods. • object is added to a finalization queue Long running finalize methods do not prevent a GC pass from completing • at some point it’s finalize method is invoked and do not freeze the application. so the object can free associated resources • object memory is reclaimed • Issues with finalize methods • makes every GC pass do more work • if a finalize method runs for a long time, it can delay execution of finalize methods of other objects • may create new strong references to objects that had none, These are preventing their GC good reasons to avoid using • run in a nondeterministic order finalize methods in • no guarantee they will be called; app. may exit first safety-critical code. 3
  • 4. Kinds of Object References • Strong references some soft, weak or target or • the normal type object phantom object “referent” object • Other reference types in java.lang.ref package • SoftReference • GC’ed any time after there are no strong references to the referent, but is typically retained until memory is low • can be used to implement caches of objects that can be recreated if needed • WeakReference For soft and weak references, the get method returns null when the referent object has been GC’ed. • GC’ed any time after there are no strong or soft references to the referent • often used for “canonical mappings” where each object has a unique identifier (one-to-one), and in collections of “listeners” • PhantomReference • GC’ed any time after there are no strong, soft or weak references to the referent • typically used in conjunction with a ReferenceQueue to manage cleanup of native resources associated with the object without using finalize methods (more on this later) Also see java.util.WeakHashMap. 4
  • 5. Alternative to Finalization • Don’t write finalize method in a class whose objects have associated native resources that require cleanup • call this class A • Instead, do the following for each such class A • create a new class, B, that extends one of the reference types • WeakReference, SoftReference or PhantomReference • create one or more ReferenceQueue objects • a B constructor that takes an A and passes that, along with a ReferenceQueue object, to the superclass constructor • create a B object for each A object • iteratively call remove on the ReferenceQueue • free resources associated with returned B object • often this is done in a separate thread When there is an associated ReferenceQueue, weak and soft reference are added to it before the referent object has been finalized and reclaimed. Phantom references are added to it after these occur. 5
  • 6. GC Metrics • Different types of applications have different concerns related to GC • Throughput • percentage of the total run time not spent performing GC • Pauses • times when the application code stops running while GC is performed • interested in the number of pauses, their average duration and their maximum duration • Footprint • current heap size (amount of memory) being used • Promptness • how quickly memory from objects no longer needed is freed 6
  • 7. Generational GC • All of the GC algorithms used by Java are variations on the concept of generational GC • Generational GC assumes that • the most recently created objects are the ones that are most likely to become unreachable soon • for example, objects created in a method and only referenced by local variables that go out of scope when the method exits • the longer an object remains reachable, the less likely it is to be eligible for GC soon (or ever) • Objects are divided into “generations” or “spaces” • Java categories these with the names “young”, “tenured” and “perm” • objects can move from one space to another during a GC 7
  • 8. Object Spaces • Hold objects of similar ages or generations • “young” spaces hold recently created objects and can be GC’ed in a “minor” or “major” collection • “tenured” space hold objects that have survived some number of minor collections and can be GC’ed only in a major collection • “perm” space hold objects needed by the JVM, such as Class & Method objects, their byte code, and interned Strings • GC of this space results in classes being “unloaded” • Size of each space • determined by current heap size (which can change during runtime) and several tuning options eden survivor 1 survivor 2 tenured perm (young) (young) (young) 8
  • 9. Young Spaces • Eden space • holds objects created after the last GC, except those that belong in the perm space • during a minor collection, these objects are either GC’ed or moved to a survivor space • Survivor spaces • these spaces hold young objects that have survived at least one GC • during a minor collection, these objects are either GC’ed or moved to the other survivor space • Minor collections • tend to be fast compared to major collections because only a subset of the objects need to be examined • typically occur much more frequently than major collections 9
  • 10. GC Running Details • Three approaches • serial collector does this for minor and major collections 1. Stop the world • throughput collector does this for major collections • when a GC pass is started, it runs to completion before the application is allowed to run again 2. Incremental none of the Java GC algorithms do this • a GC pass can alternate between doing part of the work and letting the application run for a short time, until the GC pass is completed • throughput collector does this for minor collections 3. Concurrent • concurrent low pause collector does this for minor and major collections • a GC pass runs concurrently with the application so the application is only briefly stopped 10
  • 11. When Does GC Occur? • Impacted by heap size • from reference #1 (see last slide) ... • “If a heap size is small, collection will be fast but the heap will fill up more quickly, thus requiring more frequent collections.” • “Conversely, a large heap will take longer to fill up and thus collections will be less frequent, but they take longer.” • Minor collections • occur when a young space approaches being full • Major collections • occur when the tenured space approaches being full 11
  • 12. GC Algorithms options specified with -XX: are • Serial: -XX:+UseSerialGC turned on with + and off with - • uses the same thread as the application for minor and major collections • Throughput: -XX:+UseParallelGC • uses multiple threads for minor, but not major, collections to reduce pause times • good when multiple processors are available, the app. will have a large number of short-lived objects, and there isn’t a pause time constraint • Concurrent Low Pause: -XX:+UseConcMarkSweepGC • only works well when objects are created by multiple threads? • uses multiple threads for minor and major collections to reduce pause times • good when multiple processors are available, the app. will have a large number of long-lived objects, and there is a pause time constraint 12
  • 13. Ergonomics • Sun refers to automatic selection of default options based on hardware and OS characteristics as “ergonomics” • A “server-class machine” has • more than one processor • at least 2GB of memory • isn’t running Windows on a 32 bit processor 13
  • 14. Ergonomics ... • Server-class machine • optimized for overall performance • uses throughput collector • uses server runtime compiler • sets starting heap size = 1/64 of memory up to 1GB • sets maximum heap size = 1/4 of memory up to 1GB • Client-class machine • optimized for fast startup and small memory usage • targeted at interactive applications • uses serial collector • uses client runtime compiler • starting and maximum heap size defaults? 14
  • 15. GC Monitoring • There are several options that cause the details of GC operations to be output • -verbose:gc • outputs a line of basic information about each collection • -XX:+PrintGCTimeStamps • outputs a timestamp at the beginning of each line • -XX:+PrintGCDetails • implies -verbose:gc • outputs additional information about each collection • -Xloggc:gc.log • implies -verbose:gc and -XX:+PrintGCTimeStamps • directs GC output to a file instead of stdout • Specifying the 3rd and 4th option gives all four 15
  • 16. Basic GC Tuning See http://java.sun.com/docs/hotspot/gc5.0/ • Recommend approach ergo5.html, section 4 ”Tuning Strategy” • set a few goals that are used to adjust the tuning options 1. throughput goal -XX:GCTimeRatio=n • What percentage of the total run time should be spent doing application work as opposed to performing GC? 2. maximum pause time goal -XX:MaxGCPauseMillis=n • What is the maximum number of milliseconds that the application should pause for a single GC? 3. footprint goal • if the other goals have been met, reduce the heap size until one of the previous goals can no longer be met, then increase it 16
  • 17. Advanced GC Tuning • -Xms=n (starting) and -Xmx=n (maximum) heap size • these affect the sizes of the object spaces • -XX:MinHeapFreeRatio=n, -XX:MaxHeapFreeRatio=n • bounds on ratio of unused/free space to space occupied by live objects • heap space grows and shrinks after each GC to maintain this, limited by the maximum heap size • -XX:NewSize=n, -XX:MaxNewSize=n • default and max young size (eden + survivor 1 + survivor 2) • -XX:NewRatio=n • ratio between young size and tenured size • -XX:SurvivorRatio=n • ratio between the size of each survivor space and eden • -XX:MaxPermSize=n • upper bound on perm size • -XX:TargetSurvivorRatio=n • target percentage of survivor space used after each GC 17
  • 18. Even More GC Tuning • -XX:+DisableExplicitGC • when on, calls to System.gc() do not result in a GC • off by default • -XX:+ScavengeBeforeFullGC • when on, perform a minor collection before each major collection • on by default • -XX:+UseGCOverheadLimit • when on, if 98% or more of the total time is spent performing GC, an OutOfMemoryError is thrown • on by default 18
  • 19. The Testing Framework 5 Ant build.xml ChartCreator.java 1 4 gc.properties iterate.rb * gcloganalyzer.rb JFreeChart 2 * 3 gc.csv gc.png ScriptRunner.java GCExerciser.java gc.log time.txt script.xml 19
  • 20. gc.properties # Details about property to be varied. property.name=gc.pause.max display.name=Max Pause Goal start.value=0 end.value=200 step.size=20 processor.bits = 64 # Heap size details. heap.size.start = 64M heap.size.max = 1G 20
  • 21. gc.properties ... # Garbage collection algorithm # UseSerialGC -> serial collector # UseParallelGC -> throughput collector # UseConcMarkSweepGC -> concurrent low pause collector gc.algorithm.option=UseParallelGC # Maximum Pause Time Goal # This is the goal for the maximum number of milliseconds # that the application will pause for GC. gc.pause.max = 50 # Throughput Goal # This is the goal for the ratio between # the time spent performing GC and the application time. # The percentage goal for GC will be 1 / (1 + gc.time.ratio). # A value of 49 gives 2% GC or 98% throughput. gc.time.ratio = 49 21
  • 22. gc.properties ... # The number of objects to be created. object.count = 25 No # TheSc ne rip of size of the data in each object. 1MB tR the object.size = 1000000 un se ne r a prop # The number of milliseconds that a reference should be nd e cannot be GCed. # held to each object, so itrt sc ies rip t.x are object.time.to.live = 30000 ml us are ed i # The number of milliseconds between object creations. time.between.creations = 30 us f ed ! # The number of milliseconds to run # after all the objects have been created. time.to.run.after.creations = 1000 22
  • 23. script.xml • Here’s an example of a script file • object elements create an object of a given size and lifetime • work elements simulate doing work between object creations • note the support for loops, including nested loops <script> <object size="1M" life="30"/> <work time="200"/> <loop times="3"> <object size="2M" life="50"/> <work time="500"/> <loop times="2"> <object size="3M" life="50"/> <work time="500"/> </loop> </loop> </script> 23
  • 24. iterate.rb 1. Obtains properties in gc.properties 2. Iterates property.name from start.value to end.value in steps of step.size and passes the value to the run method 3. The run method 1. runs ScriptRunner.java which reads script.xml and processes the steps in it by invoking methods of GCExcercizer.java to produce gc.log and time.txt 2. runs gcloganalyzer.rb which adds a line to gc.csv 24
  • 25. GCExerciser.java 1. Obtains properties in gc.properties and properties specified on the “java” command line to override them • for iterating through a range of property values 2. Creates object.count objects that each have a size of object.size and are scheduled to live for object.time.to.live milliseconds 3. Each object is placed into a TreeSet that is sorted based on the time at which the object should be removed from the TreeSet • makes the object eligible for GC 4. After each object is created, the TreeSet is repeatedly searched for objects that are ready to be removed until time.between.creations milliseconds have elapsed 5. After all the objects have been created, the TreeSet is repeatedly searched for object that are ready to be removed until time.to.run.after.creations milliseconds have elapsed 6. Write the total run time to time.txt 25
  • 26. ChartCreator.java • Uses the open-source library JFreeChart to create a line graph showing the throughput for various values of a property that affects GC • Example 26
  • 27. Further Work • Possibly consider the following modifications to the GC test framework • have the objects refer to a random number of other objects • have each object know about the objects that refer to it so it can ask them to release their references • use ScheduledExecutorService to initiate an object releasing itself • run multiple times with the same options and average the results • make each run much longer ... perhaps 10 minutes 27
  • 28. References 1. “Memory Management in the Java HotSpot Virtual Machine” • http://java.sun.com/javase/technologies/hotspot/gc/ memorymanagement_whitepaper.pdf 2. “Tuning Garbage Collection with the 5.0 Java Virtual Machine” • http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html 3. “Ergonomics in the 5.0 Java Virtual Machine” • http://java.sun.com/docs/hotspot/gc5.0/ergo5.html 4. “Garbage Collection in the Java HotSpot Virtual Machine” • http://www.devx.com/Java/Article/21977/ 28