SlideShare a Scribd company logo
1 of 26
Download to read offline
Garbage Collection in Smalltalk
* By John M McIntosh
  - Corporate Smalltalk Consulting Ltd.
  - http://www.smalltalkconsulting.com
  - johnmci@smalltalkconsulting.com

                  *
                  * Maintainer of the Squeak Macintosh VM.
                  * Squeak TK4 VM support {Ask me later about TK4}
                  * Trip reports for OOPSLA, Camp Smalltalk, etc.
                                                                       For ESUG 2004
© copyright 1997-2004 John M McIntosh, all rights reserved. Page   1
Garbage Collection - a worldly view

* Internet group postings in the last year:
   - “I eventually 'killed' the thing by enumerating over it
     and using 'become' to transform it into a string.”
   - “What happens to Threads when they die? Do they go
     to heaven? Or does the garbage collector take them
     away?”
   - “Just a thought that there is a dead object seating (sic)
     in my memory listening to events and doing funny
     things without my knowledge is scary and can produce
     hard to debug behavior.”

© copyright 1997-2004 John M McIntosh, all rights reserved. Page   2
Reference Counting

* Each object has a counter to track references.
* As references to an object are made/deleted, this counter is
  incremented/decremented.
* When a reference count goes to *zero*, the object is
  <usually> deleted and any referenced children counters get
  decremented.
                                      1
                       1
       Root                                        0
                        1             2          B
                                                                                     cascade delete
                                                                                     to children
                                                                           deleted
                                                                       A                       1
© copyright 1997-2004 John M McIntosh, all rights reserved. Page   3                    C
Mark/Sweep
Roots                   Mark/Sweep & Compaction
                                              M
                                   F                                      A
                                                                                            C
                                                        M
                               M                 D                               B
                    E
                M
      G                                           M
                                          H                            • Mark accessible objects.
                                                                       • Sweep all objects, and now we realize,
                                                                       • unmarked objects A, B, and C are free

                                                                       Expensive optional compaction event
                                                                       clumps results together making free space
© copyright 1997-2004 John M McIntosh, all rights reserved. Page   4   one big block.
Copying - The Flip or Scavenge
                      FromSpace                                                          ToSpace
     Root                          E                                          Root
                                                                       Flip

                    A                            C                                   A         D   B

                                                                                     C
                 D                 B

*      When FromSpace is full we *flip* to ToSpace: Notice the change of Placement
        - Copy roots of the world to ToSpace.             and that E isn't copied.
        - Copy root accessible objects to ToSpace.
        - Copy objects accessible from root accessible objects to ToSpace.
        - Repeat above until done.
*      Cost is related to number of accessible objects in FromSpace.
© copyright 1997-2004 John M McIntosh, all rights reserved. Page   5
Generational Garbage Collector

* Separate objects by age into two or more regions.
         - For example: Tektronix 4406 Smalltalk used seven regions, 3 bits
           Pat Caudill (1945-2001)
* Allocate objects in new space, when full then copy
  accessible objects to old space. This is known as a
  scavenge event.
* Movement of objects to old space is called tenuring.
* Objects must have a high death rate and low old to young
  object references. (Eh?). . . Both very important issues, I'll
  explain in a few minutes.

© copyright 1997-2004 John M McIntosh, all rights reserved. Page   6
Generational Scavenge Event
                             New Space                            -> tenure ->            Old Space

Root                   A                                                  Root        A       B         C
                                                        C
                                                                                      D
                 E                                                                             E
                                          B


                                                                                 A B and C get copied via Root
 Remembered Table (RT)                                                           reference. E is copied via OldSpace
   InterGenerational References                                                  reference from D, which is
                                                                                 remember by being stored in the
                                                                                 Remember Table.
       © copyright 1997-2004 John M McIntosh, all rights reserved. Page   7
VM Failure at end of chart, Why?
 Log scale
Of RT entries




     © copyright 1997-2004 John M McIntosh, all rights reserved. Page   8
Squeak Memory Layout
                          Generational Mark/Sweep Compacting
                                                                                                  Actual address
                                                                                                  depends on mmap
                      VM code
                                                                                  Old               0x40000000
                                                                                                    startOfMemory

                                                                       +
   VM variables, plugins, DLLs
                                                                                 Young              0x40ABFDEF
                                                                                                    youngStart


               Malloc free space                                                                    0x40EBFDEF
                                                                                                    endOfMemory

                                                                             Virtual Memory
 Applies to Squeak VM that support                                         File mapped Block
 Virtual Memory file mapping and                                           used to store image.              G
 ability to grow/shrink image space                                        Grows and shrinks                 R
 (win32, some unix, mac os-9/os-x)                                             On demand                     O
                                                                                                             W
 Issues with non 32bit clean code                                                                            T
 limit object space to first 2GB of                                                                          H
 Address space.
© copyright 1997-2004 John M McIntosh, all rights reserved. Page   9                                 0x60000000
                                                                                                     End of VM Block
IBM VisualAge Memory Layout v5.5.1
                     Generational Copying (compacting optional)
        NewSpace                                             Segments (lots!)

    semi-space - A                                                              Fixed
                          262,144                                               Old     Code
                                                   OldSpace (RAM/ROM)                   Cache
    semi-space - B                                                              Space
                                                                        Text
                          262,144

  * Generational Copy Garbage Collector & Mark/Sweep.
  * Copy between semi-spaces until full
  * Then tenure some objects to current OldSpace segment
  * Object loader/dumper can allocate segments (supports ROM)
  * EsMemorySegment activeNewSpace
  * To set current NewSpace semi-spaces size. Default is 262,144 in size
  * abt -imyimage.im -mn###### (Start with ### byte in NewSpace)

© copyright 1997-2004 John M McIntosh, all rights reserved. Page   10
VisualWorks Memory Layout                                        V5.i2

           Generational Copying, + Incremental Mark/Sweep
                        (Compacting optional)
                                                                        semi-space A
                                            Eden                        semi-space B
                                                     LargeSpace
                                                                                       Stack
                                                     FixedSpace
 RT                                                  OldSpace                          Code
                                                                                       Cache
 ORT                                                 PermSpace
*      Allocate objects or headers in Eden (bodies in Eden, Large, or Fixed).
*      Full? Copy Eden and active semi-space survivors to empty semi-space.
*      When semi-space use exceeds threshold, tenure some objects to OldSpace.
*      Once in OldSpace, use a Incremental Mark/Sweep GC to find garbage.
© copyright 1997-2004 John M McIntosh, all rights reserved. Page   11
SurvivorSpace small, watch how objects get tenured




© copyright 1997-2004 John M McIntosh, all rights reserved. Page   12
Key to GC Events


           1              Compact GC event: Full mark/sweep/compact OldSpace
           2              Compacting decision has been made
           3              IGC justified, interruptible, full cycle via idle loop call
           4              Idle Loop Entered
           5              Low Space Action Entered via VM trigger
           6              Incremental GC, (work quotas) attempt to cleanup OldSpace
           7              Request grow; Grow if allowed
           8              LowSpace and we must grow, but first do aggressive GC work:
                          Finish IGC, do OldSpace Mark/Sweep GC, if required followup
                          with OldSpace Mark/Sweep/Compact
           9              Grow Memory required
           10             Grow Memory attempted, may fail, but usually is granted


© copyright 1997-2004 John M McIntosh, all rights reserved. Page   13
SurvivorSpace larger, less objects get tenured




© copyright 1997-2004 John M McIntosh, all rights reserved. Page   14
Beware trade-off is complex, bigger SurvivorSpace =? Poor Performance




© copyright 1997-2004 John M McIntosh, all rights reserved. Page   15
Impact of pre VW 5.x single free chain performance issue
           First unit of work takes 10x longer than 2nd unit of work. Why?


Object Table Data Entries
100,000 Logarithmic scale



10,000



1,000

                                                                            1st Unit of Work
 100
                                                                                                        2nd
                                                                                                        Unit of
                                                                                                        Work

          <-         Initialization process                         ->

                                                                Time Taken, (work units are same computation effort)
 © copyright 1997-2004 John M McIntosh, all rights reserved. Page   16
Incorrect IGC work loads, IGC never completes




© copyright 1997-2004 John M McIntosh, all rights reserved. Page   17
Better IGC work loads, IGC always completes




© copyright 1997-2004 John M McIntosh, all rights reserved. Page   18
Case 1: User interaction test case before changes
12




10                         10 Grow OldSpace



                           8 Finish IGC+Quick GC
8




6




4



          3 IGC justified
2                                                                                               OldSpace - Free Memory

                        2 Need to Compact
                        1 Compact event                  6 Compaction events + 9 Quick GC events
0
     0          50           100          150           200             250   300   350   400    450     500

     © copyright 1997-2004 John M McIntosh, all rights reserved. Page   19                      460 Seconds
Case 1: User interaction test case after changes,
                                                                                   IGCAcceleration factor changed
12                                10 Grow OldSpace                                 from 5 to 15. More GC work done in
                                                                                   idle loop processing so fewer GC events
10                               8 Finish IGC+Quick GC




8



6



4
           3 IGC justified

2                                        2 Need to Compact
                                         1 Compact event

                                                         3 Compaction events + 5 QuickGC events versus 6 and 9
0
                                                                                                        330 Seconds
     © copyright 1997-2004 John M McIntosh, all rights reserved. Page   20   Old Case was ~460 seconds, we save 130 secs
500-800MB server application, issues with algorithms




                                                                        Forced GC
                                                                        and object purging
                                                                        frees 400MB,
                                                                        an ugly solution
                                 Algorithm issues
                                 causes interesting
                                 growth pattern




© copyright 1997-2004 John M McIntosh, all rights reserved. Page   21
Old space is forced to shrink, then it grows!



                                                                                 Note
                                                                                 Allocation free
                                                                                 Chain
                                                                                 performance
                                                                                 Problem, shows as
                                                                                 gaps in the data




© copyright 1997-2004 John M McIntosh, all rights reserved. Page   22
Different VW 7.x 300 MB client application, note Mark/Sweep completions



                                                                        Gaps are CPU related




                                                                                               This looked OK,
                                                                                               not much room
                                                                                               to improve
                                                                                               things




                                  Lots of Tenuring here                 Less Tenuring here
© copyright 1997-2004 John M McIntosh, all rights reserved. Page   23
500 MB un-tuned vw7 server app, note IGC ceilings and excessive GC activity




  © copyright 1997-2004 John M McIntosh, all rights reserved. Page   24
Mission Critical VW 3.x 400-500MB application
                      Before & After, shows what serious tuning can do

   Before any tuning we saw:                                            After tuning effort:

   Event                                  Total                         Event               Total
   1 Compact                                       33                   3 Full ILIGC                  10
   2 Compact Need                                   6                   4 Idle Loop                  177
   3 Full ILIGC                                    46                   5 Low Space                 1224
   4 Idle Loop                                    260
                                                                        6 Run IGC                   1224
   5 Low Space                                   1921
   6 Run IGC                                     1793
   7 Request Grow                                 129                   Grand Total                 2635
   10 Grow                                        129
                                                                        Zero code tuning was done.
                                                                        Application improved a few % for
   Grand Total                                   4317
                                                                        each SUnit test case. Keyboard
                                                                        responsiveness became “snappy”.
                                                                        Got rid of mystery lock-ups, a core
                                                                        dump (or two), and reduced the
© copyright 1997-2004 John M McIntosh, all rights reserved. Page   25   number of GC cursor events.
Garbage Collection in Smalltalk
* By John M McIntosh
  - Corporate Smalltalk Consulting Ltd.
  - http://www.smalltalkconsulting.com
  - johnmci@smalltalkconsulting.com

                  * Maintainer of the Squeak Macintosh VM.
                  * TK4 VM support {Ask me later about TK4}
                  * Trip reports for OOPSLA, Camp Smalltalk, etc.

                                                                        For ESUG 2004
© copyright 1997-2004 John M McIntosh, all rights reserved. Page   26

More Related Content

More from ESUG

Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
ESUG
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
ESUG
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
ESUG
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
ESUG
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
ESUG
 
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution ModesThreaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
ESUG
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
ESUG
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
ESUG
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
ESUG
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
ESUG
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
ESUG
 
gt4atproto, A Programmable Environment for Social Media
gt4atproto, A Programmable Environment for Social Mediagt4atproto, A Programmable Environment for Social Media
gt4atproto, A Programmable Environment for Social Media
ESUG
 

More from ESUG (20)

Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
 
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution ModesThreaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
 
Garbage Collector Tuning
Garbage Collector TuningGarbage Collector Tuning
Garbage Collector Tuning
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
 
Websockets for Fencing Score
Websockets for Fencing ScoreWebsockets for Fencing Score
Websockets for Fencing Score
 
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScriptShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
 
Advanced Object- Oriented Design Mooc
Advanced Object- Oriented Design MoocAdvanced Object- Oriented Design Mooc
Advanced Object- Oriented Design Mooc
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
 
BioSmalltalk
BioSmalltalkBioSmalltalk
BioSmalltalk
 
gt4atproto, A Programmable Environment for Social Media
gt4atproto, A Programmable Environment for Social Mediagt4atproto, A Programmable Environment for Social Media
gt4atproto, A Programmable Environment for Social Media
 
Roassal3 update
Roassal3 updateRoassal3 update
Roassal3 update
 
VASER Control: Smart Energy
VASER Control: Smart EnergyVASER Control: Smart Energy
VASER Control: Smart Energy
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Garbage Collection in Smalltalk

  • 1. Garbage Collection in Smalltalk * By John M McIntosh - Corporate Smalltalk Consulting Ltd. - http://www.smalltalkconsulting.com - johnmci@smalltalkconsulting.com * * Maintainer of the Squeak Macintosh VM. * Squeak TK4 VM support {Ask me later about TK4} * Trip reports for OOPSLA, Camp Smalltalk, etc. For ESUG 2004 © copyright 1997-2004 John M McIntosh, all rights reserved. Page 1
  • 2. Garbage Collection - a worldly view * Internet group postings in the last year: - “I eventually 'killed' the thing by enumerating over it and using 'become' to transform it into a string.” - “What happens to Threads when they die? Do they go to heaven? Or does the garbage collector take them away?” - “Just a thought that there is a dead object seating (sic) in my memory listening to events and doing funny things without my knowledge is scary and can produce hard to debug behavior.” © copyright 1997-2004 John M McIntosh, all rights reserved. Page 2
  • 3. Reference Counting * Each object has a counter to track references. * As references to an object are made/deleted, this counter is incremented/decremented. * When a reference count goes to *zero*, the object is <usually> deleted and any referenced children counters get decremented. 1 1 Root 0 1 2 B cascade delete to children deleted A 1 © copyright 1997-2004 John M McIntosh, all rights reserved. Page 3 C
  • 4. Mark/Sweep Roots Mark/Sweep & Compaction M F A C M M D B E M G M H • Mark accessible objects. • Sweep all objects, and now we realize, • unmarked objects A, B, and C are free Expensive optional compaction event clumps results together making free space © copyright 1997-2004 John M McIntosh, all rights reserved. Page 4 one big block.
  • 5. Copying - The Flip or Scavenge FromSpace ToSpace Root E Root Flip A C A D B C D B * When FromSpace is full we *flip* to ToSpace: Notice the change of Placement - Copy roots of the world to ToSpace. and that E isn't copied. - Copy root accessible objects to ToSpace. - Copy objects accessible from root accessible objects to ToSpace. - Repeat above until done. * Cost is related to number of accessible objects in FromSpace. © copyright 1997-2004 John M McIntosh, all rights reserved. Page 5
  • 6. Generational Garbage Collector * Separate objects by age into two or more regions. - For example: Tektronix 4406 Smalltalk used seven regions, 3 bits Pat Caudill (1945-2001) * Allocate objects in new space, when full then copy accessible objects to old space. This is known as a scavenge event. * Movement of objects to old space is called tenuring. * Objects must have a high death rate and low old to young object references. (Eh?). . . Both very important issues, I'll explain in a few minutes. © copyright 1997-2004 John M McIntosh, all rights reserved. Page 6
  • 7. Generational Scavenge Event New Space -> tenure -> Old Space Root A Root A B C C D E E B A B and C get copied via Root Remembered Table (RT) reference. E is copied via OldSpace InterGenerational References reference from D, which is remember by being stored in the Remember Table. © copyright 1997-2004 John M McIntosh, all rights reserved. Page 7
  • 8. VM Failure at end of chart, Why? Log scale Of RT entries © copyright 1997-2004 John M McIntosh, all rights reserved. Page 8
  • 9. Squeak Memory Layout Generational Mark/Sweep Compacting Actual address depends on mmap VM code Old 0x40000000 startOfMemory + VM variables, plugins, DLLs Young 0x40ABFDEF youngStart Malloc free space 0x40EBFDEF endOfMemory Virtual Memory Applies to Squeak VM that support File mapped Block Virtual Memory file mapping and used to store image. G ability to grow/shrink image space Grows and shrinks R (win32, some unix, mac os-9/os-x) On demand O W Issues with non 32bit clean code T limit object space to first 2GB of H Address space. © copyright 1997-2004 John M McIntosh, all rights reserved. Page 9 0x60000000 End of VM Block
  • 10. IBM VisualAge Memory Layout v5.5.1 Generational Copying (compacting optional) NewSpace Segments (lots!) semi-space - A Fixed 262,144 Old Code OldSpace (RAM/ROM) Cache semi-space - B Space Text 262,144 * Generational Copy Garbage Collector & Mark/Sweep. * Copy between semi-spaces until full * Then tenure some objects to current OldSpace segment * Object loader/dumper can allocate segments (supports ROM) * EsMemorySegment activeNewSpace * To set current NewSpace semi-spaces size. Default is 262,144 in size * abt -imyimage.im -mn###### (Start with ### byte in NewSpace) © copyright 1997-2004 John M McIntosh, all rights reserved. Page 10
  • 11. VisualWorks Memory Layout V5.i2 Generational Copying, + Incremental Mark/Sweep (Compacting optional) semi-space A Eden semi-space B LargeSpace Stack FixedSpace RT OldSpace Code Cache ORT PermSpace * Allocate objects or headers in Eden (bodies in Eden, Large, or Fixed). * Full? Copy Eden and active semi-space survivors to empty semi-space. * When semi-space use exceeds threshold, tenure some objects to OldSpace. * Once in OldSpace, use a Incremental Mark/Sweep GC to find garbage. © copyright 1997-2004 John M McIntosh, all rights reserved. Page 11
  • 12. SurvivorSpace small, watch how objects get tenured © copyright 1997-2004 John M McIntosh, all rights reserved. Page 12
  • 13. Key to GC Events 1 Compact GC event: Full mark/sweep/compact OldSpace 2 Compacting decision has been made 3 IGC justified, interruptible, full cycle via idle loop call 4 Idle Loop Entered 5 Low Space Action Entered via VM trigger 6 Incremental GC, (work quotas) attempt to cleanup OldSpace 7 Request grow; Grow if allowed 8 LowSpace and we must grow, but first do aggressive GC work: Finish IGC, do OldSpace Mark/Sweep GC, if required followup with OldSpace Mark/Sweep/Compact 9 Grow Memory required 10 Grow Memory attempted, may fail, but usually is granted © copyright 1997-2004 John M McIntosh, all rights reserved. Page 13
  • 14. SurvivorSpace larger, less objects get tenured © copyright 1997-2004 John M McIntosh, all rights reserved. Page 14
  • 15. Beware trade-off is complex, bigger SurvivorSpace =? Poor Performance © copyright 1997-2004 John M McIntosh, all rights reserved. Page 15
  • 16. Impact of pre VW 5.x single free chain performance issue First unit of work takes 10x longer than 2nd unit of work. Why? Object Table Data Entries 100,000 Logarithmic scale 10,000 1,000 1st Unit of Work 100 2nd Unit of Work <- Initialization process -> Time Taken, (work units are same computation effort) © copyright 1997-2004 John M McIntosh, all rights reserved. Page 16
  • 17. Incorrect IGC work loads, IGC never completes © copyright 1997-2004 John M McIntosh, all rights reserved. Page 17
  • 18. Better IGC work loads, IGC always completes © copyright 1997-2004 John M McIntosh, all rights reserved. Page 18
  • 19. Case 1: User interaction test case before changes 12 10 10 Grow OldSpace 8 Finish IGC+Quick GC 8 6 4 3 IGC justified 2 OldSpace - Free Memory 2 Need to Compact 1 Compact event 6 Compaction events + 9 Quick GC events 0 0 50 100 150 200 250 300 350 400 450 500 © copyright 1997-2004 John M McIntosh, all rights reserved. Page 19 460 Seconds
  • 20. Case 1: User interaction test case after changes, IGCAcceleration factor changed 12 10 Grow OldSpace from 5 to 15. More GC work done in idle loop processing so fewer GC events 10 8 Finish IGC+Quick GC 8 6 4 3 IGC justified 2 2 Need to Compact 1 Compact event 3 Compaction events + 5 QuickGC events versus 6 and 9 0 330 Seconds © copyright 1997-2004 John M McIntosh, all rights reserved. Page 20 Old Case was ~460 seconds, we save 130 secs
  • 21. 500-800MB server application, issues with algorithms Forced GC and object purging frees 400MB, an ugly solution Algorithm issues causes interesting growth pattern © copyright 1997-2004 John M McIntosh, all rights reserved. Page 21
  • 22. Old space is forced to shrink, then it grows! Note Allocation free Chain performance Problem, shows as gaps in the data © copyright 1997-2004 John M McIntosh, all rights reserved. Page 22
  • 23. Different VW 7.x 300 MB client application, note Mark/Sweep completions Gaps are CPU related This looked OK, not much room to improve things Lots of Tenuring here Less Tenuring here © copyright 1997-2004 John M McIntosh, all rights reserved. Page 23
  • 24. 500 MB un-tuned vw7 server app, note IGC ceilings and excessive GC activity © copyright 1997-2004 John M McIntosh, all rights reserved. Page 24
  • 25. Mission Critical VW 3.x 400-500MB application Before & After, shows what serious tuning can do Before any tuning we saw: After tuning effort: Event Total Event Total 1 Compact 33 3 Full ILIGC 10 2 Compact Need 6 4 Idle Loop 177 3 Full ILIGC 46 5 Low Space 1224 4 Idle Loop 260 6 Run IGC 1224 5 Low Space 1921 6 Run IGC 1793 7 Request Grow 129 Grand Total 2635 10 Grow 129 Zero code tuning was done. Application improved a few % for Grand Total 4317 each SUnit test case. Keyboard responsiveness became “snappy”. Got rid of mystery lock-ups, a core dump (or two), and reduced the © copyright 1997-2004 John M McIntosh, all rights reserved. Page 25 number of GC cursor events.
  • 26. Garbage Collection in Smalltalk * By John M McIntosh - Corporate Smalltalk Consulting Ltd. - http://www.smalltalkconsulting.com - johnmci@smalltalkconsulting.com * Maintainer of the Squeak Macintosh VM. * TK4 VM support {Ask me later about TK4} * Trip reports for OOPSLA, Camp Smalltalk, etc. For ESUG 2004 © copyright 1997-2004 John M McIntosh, all rights reserved. Page 26