SlideShare a Scribd company logo
1 of 58
Download to read offline
Garbage Collection
   for Dummies
    Overview HotSpot GC




                       photo#1

                    Copyright © 2012 Akira Koyasu. Some rights reserved.
Agenda
JVM & GC
  heap & GC fundamentals

Collectors in HotSpot JVM
  HotSpot Collectors explained

Ergonomics & Tuning
  the start point of GC tuning

               2       Copyright © 2012 Akira Koyasu. Some rights reserved.
JVM & GC

heap in JVM

Garbage Collection

stop the world

Generational GC



                  3   Copyright © 2012 Akira Koyasu. Some rights reserved.
heap in JVM
Which objects are allocated in heap?


public static void main(String[] args) {
	 int i = 1234;
	 long[] longs = new long[16];
	 String str = "Hello!!";
	 Simple s = new Simple();
}

                    4       Copyright © 2012 Akira Koyasu. Some rights reserved.
javap -c




   5   Copyright © 2012 Akira Koyasu. Some rights reserved.
javap -c
$ javap -c study.gc.bootstrap.Heap
Compiled from "Heap.java"
public class study.gc.bootstrap.Heap {
    ...
  public static void main(java.lang.String[]);
    Code:
        0: sipush       1234
        3: istore_1
        4: bipush       16
        6: newarray       long
        8: astore_2
        9: ldc          #16        // String Hello!!
      11: astore_3
      12: new           #18        // class study/gc/Simple
      15: dup
      16: invokespecial #20        // Method study/gc/Simple."<init>":()V
      19: astore        4
      21: return
}

                                      5              Copyright © 2012 Akira Koyasu. Some rights reserved.
Garbage Collection

“Our resource is limited.
  So garbages should be
collected and recycled as
    soon as possible”




            6     Copyright © 2012 Akira Koyasu. Some rights reserved.
Dead or Alive
           at the end of each method,
             which objects are dead?

void main() {
	 sub();
}
void sub() {                                Greeter
	 Greeter g = new Greeter();
	 Message msg;
	 msg = new Message("Morning!");            Message
	 g.doGreet(msg);
	 msg = new Message("Hello!");
	 g.doGreet(msg);                           Message
}

                               7   Copyright © 2012 Akira Koyasu. Some rights reserved.
Dead or Alive


Unreferenced objects are dead

   reassignment

   scope out




               8       Copyright © 2012 Akira Koyasu. Some rights reserved.
Fragment, Compaction,
     Allocation



 A   B   B   B   B   C                        A




                 9   Copyright © 2012 Akira Koyasu. Some rights reserved.
Fragment, Compaction,
     Allocation

       fragment


 A      B         C                        A




            9     Copyright © 2012 Akira Koyasu. Some rights reserved.
Fragment, Compaction,
     Allocation



 A      B       C                        A




            9   Copyright © 2012 Akira Koyasu. Some rights reserved.
Fragment, Compaction,
     Allocation

      Compaction


 A      B          C                        A




            9      Copyright © 2012 Akira Koyasu. Some rights reserved.
Fragment, Compaction,
     Allocation



 A   B   C       A




             9       Copyright © 2012 Akira Koyasu. Some rights reserved.
Fragment, Compaction,
     Allocation

         Allocation


 A   B     C       A




               9       Copyright © 2012 Akira Koyasu. Some rights reserved.
Fragment, Compaction,
     Allocation



 A   B   C       A                       C




             9       Copyright © 2012 Akira Koyasu. Some rights reserved.
Live object
   identification

                                Postman
Reference counting
                                      Bag
Traverse marking
from root
                                           Message

                                  Message


                   10   Copyright © 2012 Akira Koyasu. Some rights reserved.
stop the world

 In traversing,            Postman
the Postman get
a new message!!                  Bag

                                      Message

                             Message


              11   Copyright © 2012 Akira Koyasu. Some rights reserved.
stop the world

 In traversing,            Postman
the Postman get
a new message!!                  Bag

                                      Message

                             Message


              11   Copyright © 2012 Akira Koyasu. Some rights reserved.
stop the world

 In traversing,                  Postman
the Postman get
a new message!!                        Bag

                                            Message

                   Message         Message


              11         Copyright © 2012 Akira Koyasu. Some rights reserved.
Generational GC

the weak generational hypothesis:
    “Most objects die young”

young -> frequently collected

old -> seldomly collected



               12     Copyright © 2012 Akira Koyasu. Some rights reserved.
Partitioned into
       generations
            Eden          A
Young {
          Survivor   B


                              C
     Old



                     13           Copyright © 2012 Akira Koyasu. Some rights reserved.
Collectors in
     HotSpot JVM


throughput & low-latency

5 Collectors




                14         Copyright © 2012 Akira Koyasu. Some rights reserved.
throughput &
   low-latency

throughput:
  more tasks in unit time

low-latency:
  fast response



              15      Copyright © 2012 Akira Koyasu. Some rights reserved.
5 collectors

Serial Collector

Parallel Collector

Parallel Compacting Collector

Concurrent Mark Sweep Collector

Garbage First Collctor (1.7_u4)


                   16   Copyright © 2012 Akira Koyasu. Some rights reserved.
Serial Collector
          for young generation
copying
                                                Eden
          From          To

                                                Survivor


                                                Old


                   12
                   17        Copyright © 2012 Akira Koyasu. Some rights reserved.
Serial Collector
             for old generation

mark-sweep




                     12
                     18     Copyright © 2012 Akira Koyasu. Some rights reserved.
Parallel Collector
            for young generation

        use a parallel version of
     the serial collector algorithm



                                               Parallel
 Serial                                        Collector
Collector




                     19     Copyright © 2012 Akira Koyasu. Some rights reserved.
Parallel Compacting
     Collector
      for old generation




 R1    R2     R3     R4                      R5




              20     Copyright © 2012 Akira Koyasu. Some rights reserved.
Parallel Compacting
     Collector
      for old generation




 R1    R2     R3     R4                      R5

              T1                             T2




              20     Copyright © 2012 Akira Koyasu. Some rights reserved.
Parallel Compacting
     Collector
      for old generation




 R1    R2     R3     R4                      R5

              T1                             T2




              20     Copyright © 2012 Akira Koyasu. Some rights reserved.
Parallel Compacting
     Collector
      for old generation




 R1    R2     R3     R4                      R5

              T1                             T2




              20     Copyright © 2012 Akira Koyasu. Some rights reserved.
Parallel Compacting
     Collector
      for old generation




 R1    R2     R3     R4                      R5

                     T1                      T2




              20     Copyright © 2012 Akira Koyasu. Some rights reserved.
Parallel Compacting
     Collector
      for old generation




 R1    R2     R3     R4                      R5

                     T1                      T2




              20     Copyright © 2012 Akira Koyasu. Some rights reserved.
Concurrent Mark
  Sweep Collector
         for old generation

non-compacting
Phases
   initial mark (STW)
   concurrent mark
   remark (STW)
   concurrent sweep


                  21    Copyright © 2012 Akira Koyasu. Some rights reserved.
Concurrent Mark
      Sweep Collector
          for old generation
                                   Postman
   Bag is updated in
concurrent marking phase
                                         Bag

new Message is marked
                                              Message
   in remark phase
                                     Message


                    22     Copyright © 2012 Akira Koyasu. Some rights reserved.
Concurrent Mark
      Sweep Collector
          for old generation
                                   Postman
   Bag is updated in
concurrent marking phase
                                         Bag

new Message is marked
                                              Message
   in remark phase
                                     Message


                    22     Copyright © 2012 Akira Koyasu. Some rights reserved.
Concurrent Mark
      Sweep Collector
          for old generation
                                       Postman
   Bag is updated in
concurrent marking phase
                                             Bag

new Message is marked
                                                  Message
   in remark phase
                         Message         Message


                    22         Copyright © 2012 Akira Koyasu. Some rights reserved.
Concurrent Mark
      Sweep Collector
          for old generation
                                       Postman
   Bag is updated in
concurrent marking phase
                                             Bag

new Message is marked
                                                  Message
   in remark phase
                         Message         Message


                    22         Copyright © 2012 Akira Koyasu. Some rights reserved.
Garbage First
       Collector
   for young & old generation

heap is devided into              Y
regions
                       Y                        O
some regions are
young,
                                  Y                            O
some other regions
are old
                                  O             O


                 23        Copyright © 2012 Akira Koyasu. Some rights reserved.
Garbage First
          Collector
     for young & old generation



  collection regions
   chosen based on
   time prediction
(some garbages remain)



                    24   Copyright © 2012 Akira Koyasu. Some rights reserved.
Garbage First
          Collector
     for young & old generation



  collection regions
   chosen based on
   time prediction
(some garbages remain)



                    24   Copyright © 2012 Akira Koyasu. Some rights reserved.
Garbage First
          Collector
     for young & old generation



  collection regions
   chosen based on
   time prediction
(some garbages remain)



                    24   Copyright © 2012 Akira Koyasu. Some rights reserved.
select collector

    collector                      option
      Serial                  -XX:+UseSerialGC

     Parallel                -XX:+UseParallelGC

Parallel Compacting         -XX:+UseParallelOldGC

        CMS                -XX:+UseConcMarkSweepGC

        G1                      -XX:+UseG1GC


                      25          Copyright © 2012 Akira Koyasu. Some rights reserved.
Ergonomics & Tuning


Ergonomics

JVM options

command line tools




                26   Copyright © 2012 Akira Koyasu. Some rights reserved.
GC tuning


the first step to tune GC is
     to   do nothing



             27    Copyright © 2012 Akira Koyasu. Some rights reserved.
Ergonomics

    Behavior-based options

latency: -XX:MaxGCPauseMillis=n

throughput: -XX:GCTimeRatio=99




                28      Copyright © 2012 Akira Koyasu. Some rights reserved.
JVM options

-Xmxn

-Xmsn

-XX:ParallelGCThreads=n

-XX:+PrintFlagsFinal



             29        Copyright © 2012 Akira Koyasu. Some rights reserved.
command line tools

      jps
      jstat
      jmap
      jhat


        30    Copyright © 2012 Akira Koyasu. Some rights reserved.
jstat




  31    Copyright © 2012 Akira Koyasu. Some rights reserved.
jstat
$ jps -l
23437 study.gc.bootstrap.Heap
23489 sun.tools.jps.Jps

$ jstat   -gcutil -h20 23437 1s
  S0       S1     E      O      P     YGC        YGCT     FGC       FGCT          GCT
 50.00     0.00 94.64    0.56 12.63         32    0.054         0    0.000         0.054
  0.00     0.00 12.08    0.56 12.63         35    0.054         0    0.000         0.054
 50.00     0.00   0.00   0.56 12.63         38    0.090         0    0.000         0.090
  0.00     0.00   0.00   0.56 12.63         40    0.092         0    0.000         0.092
  0.00    25.00 74.50    0.56 12.63         41    0.092         0    0.000         0.092
  0.00     0.00 22.15    0.56 12.63         43    0.093         0    0.000         0.093
  0.00    25.00 10.07    0.56 12.63         45    0.093         0    0.000         0.093
  0.00    25.00   0.00   0.56 12.63         47    0.094         0    0.000         0.094
  0.00    25.00   0.00   0.56 12.63         49    0.096         0    0.000         0.096
  0.00     0.00 24.16    0.56 12.63         51    0.098         0    0.000         0.098
  0.00     0.00 42.28    0.56 12.63         53    0.099         0    0.000         0.099
  0.00     0.00 44.29    0.56 12.63         55    0.100         0    0.000         0.100
...



                                       31                  Copyright © 2012 Akira Koyasu. Some rights reserved.
jmap




 32    Copyright © 2012 Akira Koyasu. Some rights reserved.
jmap
$ jmap -histo:live 23437

 num     #instances         #bytes class name
----------------------------------------------
    1:         5822         797008 <methodKlass>
    2:         5822         699712 <constMethodKlass>
    3:          393         456488 <constantPoolKlass>
    4:          360         286400 <constantPoolCacheKlass>
    5:          393         283512 <instanceKlassKlass>
    6:         1094          98656 [C
    7:          511          97864 [B
    8:          452          55128 java.lang.Class
    9:          632          40536 [[I
  10:           589          37440 [S
...

$ jmap -dump:live,format=b,file=heap.dump 23437
Dumping heap to heap.dump ...
Heap dump file created



                              32              Copyright © 2012 Akira Koyasu. Some rights reserved.
jhat




 33    Copyright © 2012 Akira Koyasu. Some rights reserved.
jhat

$ jhat heap.dump
Reading from heap.dump...
Dump file created Thu Dec 27 15:44:54
Snapshot read, resolving...
Resolving 5895 objects...
Chasing references, expect 1 dots.
Eliminating duplicate references.
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.




                                        33   Copyright © 2012 Akira Koyasu. Some rights reserved.
jhat

$ jhat heap.dump
Reading from heap.dump...
Dump file created Thu Dec 27 15:44:54
Snapshot read, resolving...
Resolving 5895 objects...
Chasing references, expect 1 dots.
Eliminating duplicate references.
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.




                                        33   Copyright © 2012 Akira Koyasu. Some rights reserved.
References
Memory Management in the Java HotSpot Virtual Machine (Sun
Microsystems, April 2006)

Garbage-First Garbage Collection (Sun Microsystems, October 2004)

A Generational Mostly-concurrent Garbage Collector (Sun
Microsystems, June 2000)

Garbage-First Collector 
http://docs.oracle.com/javase/7/docs/technotes/guides/vm/G1.html

Parallel Compaction 
http://docs.oracle.com/javase/7/docs/technotes/guides/vm/par-
compaction-6.html

Concurrent Mark Sweep Collector Enhancements 
http://docs.oracle.com/javase/7/docs/technotes/guides/vm/
cms-6.html

Storage Management 
http://openjdk.java.net/groups/hotspot/docs/StorageManagement.html



                             34            Copyright © 2012 Akira Koyasu. Some rights reserved.
Notes

This work is licensed under the
Creative Commons Attribution-
NonCommercial 3.0 Unported License. To
view a copy of this license, visit
http://creativecommons.org/licenses/by-
nc/3.0/.

photo#1   http://www.flickr.com/photos/yto/5896836025/



                          35          Copyright © 2012 Akira Koyasu. Some rights reserved.

More Related Content

More from 輝 子安

Protractor under the hood
Protractor under the hoodProtractor under the hood
Protractor under the hood輝 子安
 
そろそろLambda(CI/CD編)
そろそろLambda(CI/CD編)そろそろLambda(CI/CD編)
そろそろLambda(CI/CD編)輝 子安
 
Dockerで構成するWebサービス 〜EmotionTechの場合〜
Dockerで構成するWebサービス 〜EmotionTechの場合〜Dockerで構成するWebサービス 〜EmotionTechの場合〜
Dockerで構成するWebサービス 〜EmotionTechの場合〜輝 子安
 
Workshop: Docker on Elastic Beanstalk
Workshop: Docker on Elastic BeanstalkWorkshop: Docker on Elastic Beanstalk
Workshop: Docker on Elastic Beanstalk輝 子安
 
PHP conference 2013 ja report
PHP conference 2013 ja reportPHP conference 2013 ja report
PHP conference 2013 ja report輝 子安
 
JavaOne Guide for the Petite Bourgeoisie
JavaOne Guide for the Petite BourgeoisieJavaOne Guide for the Petite Bourgeoisie
JavaOne Guide for the Petite Bourgeoisie輝 子安
 
Java, Up to Date Sources
Java, Up to Date SourcesJava, Up to Date Sources
Java, Up to Date Sources輝 子安
 
Hello, Guava ! samples
Hello, Guava ! samplesHello, Guava ! samples
Hello, Guava ! samples輝 子安
 
Hello, Guava !
Hello, Guava !Hello, Guava !
Hello, Guava !輝 子安
 
Tokyo Cabinet & Tokyo Tyrant
Tokyo Cabinet & Tokyo TyrantTokyo Cabinet & Tokyo Tyrant
Tokyo Cabinet & Tokyo Tyrant輝 子安
 

More from 輝 子安 (10)

Protractor under the hood
Protractor under the hoodProtractor under the hood
Protractor under the hood
 
そろそろLambda(CI/CD編)
そろそろLambda(CI/CD編)そろそろLambda(CI/CD編)
そろそろLambda(CI/CD編)
 
Dockerで構成するWebサービス 〜EmotionTechの場合〜
Dockerで構成するWebサービス 〜EmotionTechの場合〜Dockerで構成するWebサービス 〜EmotionTechの場合〜
Dockerで構成するWebサービス 〜EmotionTechの場合〜
 
Workshop: Docker on Elastic Beanstalk
Workshop: Docker on Elastic BeanstalkWorkshop: Docker on Elastic Beanstalk
Workshop: Docker on Elastic Beanstalk
 
PHP conference 2013 ja report
PHP conference 2013 ja reportPHP conference 2013 ja report
PHP conference 2013 ja report
 
JavaOne Guide for the Petite Bourgeoisie
JavaOne Guide for the Petite BourgeoisieJavaOne Guide for the Petite Bourgeoisie
JavaOne Guide for the Petite Bourgeoisie
 
Java, Up to Date Sources
Java, Up to Date SourcesJava, Up to Date Sources
Java, Up to Date Sources
 
Hello, Guava ! samples
Hello, Guava ! samplesHello, Guava ! samples
Hello, Guava ! samples
 
Hello, Guava !
Hello, Guava !Hello, Guava !
Hello, Guava !
 
Tokyo Cabinet & Tokyo Tyrant
Tokyo Cabinet & Tokyo TyrantTokyo Cabinet & Tokyo Tyrant
Tokyo Cabinet & Tokyo Tyrant
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

Garbage Collection for Dummies

  • 1. Garbage Collection for Dummies Overview HotSpot GC photo#1 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 2. Agenda JVM & GC heap & GC fundamentals Collectors in HotSpot JVM HotSpot Collectors explained Ergonomics & Tuning the start point of GC tuning 2 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 3. JVM & GC heap in JVM Garbage Collection stop the world Generational GC 3 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 4. heap in JVM Which objects are allocated in heap? public static void main(String[] args) { int i = 1234; long[] longs = new long[16]; String str = "Hello!!"; Simple s = new Simple(); } 4 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 5. javap -c 5 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 6. javap -c $ javap -c study.gc.bootstrap.Heap Compiled from "Heap.java" public class study.gc.bootstrap.Heap { ... public static void main(java.lang.String[]); Code: 0: sipush 1234 3: istore_1 4: bipush 16 6: newarray long 8: astore_2 9: ldc #16 // String Hello!! 11: astore_3 12: new #18 // class study/gc/Simple 15: dup 16: invokespecial #20 // Method study/gc/Simple."<init>":()V 19: astore 4 21: return } 5 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 7. Garbage Collection “Our resource is limited. So garbages should be collected and recycled as soon as possible” 6 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 8. Dead or Alive at the end of each method, which objects are dead? void main() { sub(); } void sub() { Greeter Greeter g = new Greeter(); Message msg; msg = new Message("Morning!"); Message g.doGreet(msg); msg = new Message("Hello!"); g.doGreet(msg); Message } 7 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 9. Dead or Alive Unreferenced objects are dead reassignment scope out 8 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 10. Fragment, Compaction, Allocation A B B B B C A 9 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 11. Fragment, Compaction, Allocation fragment A B C A 9 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 12. Fragment, Compaction, Allocation A B C A 9 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 13. Fragment, Compaction, Allocation Compaction A B C A 9 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 14. Fragment, Compaction, Allocation A B C A 9 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 15. Fragment, Compaction, Allocation Allocation A B C A 9 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 16. Fragment, Compaction, Allocation A B C A C 9 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 17. Live object identification Postman Reference counting Bag Traverse marking from root Message Message 10 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 18. stop the world In traversing, Postman the Postman get a new message!! Bag Message Message 11 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 19. stop the world In traversing, Postman the Postman get a new message!! Bag Message Message 11 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 20. stop the world In traversing, Postman the Postman get a new message!! Bag Message Message Message 11 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 21. Generational GC the weak generational hypothesis: “Most objects die young” young -> frequently collected old -> seldomly collected 12 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 22. Partitioned into generations Eden A Young { Survivor B C Old 13 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 23. Collectors in HotSpot JVM throughput & low-latency 5 Collectors 14 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 24. throughput & low-latency throughput: more tasks in unit time low-latency: fast response 15 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 25. 5 collectors Serial Collector Parallel Collector Parallel Compacting Collector Concurrent Mark Sweep Collector Garbage First Collctor (1.7_u4) 16 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 26. Serial Collector for young generation copying Eden From To Survivor Old 12 17 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 27. Serial Collector for old generation mark-sweep 12 18 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 28. Parallel Collector for young generation use a parallel version of the serial collector algorithm Parallel Serial Collector Collector 19 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 29. Parallel Compacting Collector for old generation R1 R2 R3 R4 R5 20 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 30. Parallel Compacting Collector for old generation R1 R2 R3 R4 R5 T1 T2 20 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 31. Parallel Compacting Collector for old generation R1 R2 R3 R4 R5 T1 T2 20 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 32. Parallel Compacting Collector for old generation R1 R2 R3 R4 R5 T1 T2 20 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 33. Parallel Compacting Collector for old generation R1 R2 R3 R4 R5 T1 T2 20 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 34. Parallel Compacting Collector for old generation R1 R2 R3 R4 R5 T1 T2 20 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 35. Concurrent Mark Sweep Collector for old generation non-compacting Phases initial mark (STW) concurrent mark remark (STW) concurrent sweep 21 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 36. Concurrent Mark Sweep Collector for old generation Postman Bag is updated in concurrent marking phase Bag new Message is marked Message in remark phase Message 22 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 37. Concurrent Mark Sweep Collector for old generation Postman Bag is updated in concurrent marking phase Bag new Message is marked Message in remark phase Message 22 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 38. Concurrent Mark Sweep Collector for old generation Postman Bag is updated in concurrent marking phase Bag new Message is marked Message in remark phase Message Message 22 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 39. Concurrent Mark Sweep Collector for old generation Postman Bag is updated in concurrent marking phase Bag new Message is marked Message in remark phase Message Message 22 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 40. Garbage First Collector for young & old generation heap is devided into Y regions Y O some regions are young, Y O some other regions are old O O 23 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 41. Garbage First Collector for young & old generation collection regions chosen based on time prediction (some garbages remain) 24 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 42. Garbage First Collector for young & old generation collection regions chosen based on time prediction (some garbages remain) 24 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 43. Garbage First Collector for young & old generation collection regions chosen based on time prediction (some garbages remain) 24 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 44. select collector collector option Serial -XX:+UseSerialGC Parallel -XX:+UseParallelGC Parallel Compacting -XX:+UseParallelOldGC CMS -XX:+UseConcMarkSweepGC G1 -XX:+UseG1GC 25 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 45. Ergonomics & Tuning Ergonomics JVM options command line tools 26 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 46. GC tuning the first step to tune GC is to do nothing 27 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 47. Ergonomics Behavior-based options latency: -XX:MaxGCPauseMillis=n throughput: -XX:GCTimeRatio=99 28 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 48. JVM options -Xmxn -Xmsn -XX:ParallelGCThreads=n -XX:+PrintFlagsFinal 29 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 49. command line tools jps jstat jmap jhat 30 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 50. jstat 31 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 51. jstat $ jps -l 23437 study.gc.bootstrap.Heap 23489 sun.tools.jps.Jps $ jstat -gcutil -h20 23437 1s S0 S1 E O P YGC YGCT FGC FGCT GCT 50.00 0.00 94.64 0.56 12.63 32 0.054 0 0.000 0.054 0.00 0.00 12.08 0.56 12.63 35 0.054 0 0.000 0.054 50.00 0.00 0.00 0.56 12.63 38 0.090 0 0.000 0.090 0.00 0.00 0.00 0.56 12.63 40 0.092 0 0.000 0.092 0.00 25.00 74.50 0.56 12.63 41 0.092 0 0.000 0.092 0.00 0.00 22.15 0.56 12.63 43 0.093 0 0.000 0.093 0.00 25.00 10.07 0.56 12.63 45 0.093 0 0.000 0.093 0.00 25.00 0.00 0.56 12.63 47 0.094 0 0.000 0.094 0.00 25.00 0.00 0.56 12.63 49 0.096 0 0.000 0.096 0.00 0.00 24.16 0.56 12.63 51 0.098 0 0.000 0.098 0.00 0.00 42.28 0.56 12.63 53 0.099 0 0.000 0.099 0.00 0.00 44.29 0.56 12.63 55 0.100 0 0.000 0.100 ... 31 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 52. jmap 32 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 53. jmap $ jmap -histo:live 23437 num #instances #bytes class name ---------------------------------------------- 1: 5822 797008 <methodKlass> 2: 5822 699712 <constMethodKlass> 3: 393 456488 <constantPoolKlass> 4: 360 286400 <constantPoolCacheKlass> 5: 393 283512 <instanceKlassKlass> 6: 1094 98656 [C 7: 511 97864 [B 8: 452 55128 java.lang.Class 9: 632 40536 [[I 10: 589 37440 [S ... $ jmap -dump:live,format=b,file=heap.dump 23437 Dumping heap to heap.dump ... Heap dump file created 32 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 54. jhat 33 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 55. jhat $ jhat heap.dump Reading from heap.dump... Dump file created Thu Dec 27 15:44:54 Snapshot read, resolving... Resolving 5895 objects... Chasing references, expect 1 dots. Eliminating duplicate references. Snapshot resolved. Started HTTP server on port 7000 Server is ready. 33 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 56. jhat $ jhat heap.dump Reading from heap.dump... Dump file created Thu Dec 27 15:44:54 Snapshot read, resolving... Resolving 5895 objects... Chasing references, expect 1 dots. Eliminating duplicate references. Snapshot resolved. Started HTTP server on port 7000 Server is ready. 33 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 57. References Memory Management in the Java HotSpot Virtual Machine (Sun Microsystems, April 2006) Garbage-First Garbage Collection (Sun Microsystems, October 2004) A Generational Mostly-concurrent Garbage Collector (Sun Microsystems, June 2000) Garbage-First Collector  http://docs.oracle.com/javase/7/docs/technotes/guides/vm/G1.html Parallel Compaction  http://docs.oracle.com/javase/7/docs/technotes/guides/vm/par- compaction-6.html Concurrent Mark Sweep Collector Enhancements  http://docs.oracle.com/javase/7/docs/technotes/guides/vm/ cms-6.html Storage Management  http://openjdk.java.net/groups/hotspot/docs/StorageManagement.html 34 Copyright © 2012 Akira Koyasu. Some rights reserved.
  • 58. Notes This work is licensed under the Creative Commons Attribution- NonCommercial 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by- nc/3.0/. photo#1 http://www.flickr.com/photos/yto/5896836025/ 35 Copyright © 2012 Akira Koyasu. Some rights reserved.