26
© copyright 1997-2004 John M McIntosh, all rights reserved. Page 1 Garbage Collection in Smalltalk * By John M McIntosh - Corporate Smalltalk Consulting Ltd. - http://www.smalltalkconsulting.com - [email protected] * * 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

Garbage Collection in Smalltalk

  • Upload
    esug

  • View
    796

  • Download
    2

Embed Size (px)

DESCRIPTION

Garbage Collection in Smalltalk, John M McIntosh (ESUG 2004, Koethen)

Citation preview

Page 1: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 1

Garbage Collection in Smalltalk

* By John M McIntosh

- Corporate Smalltalk Consulting Ltd.

- http://www.smalltalkconsulting.com

- [email protected]

** 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

Page 2: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 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.”

Page 3: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 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.

2

Root 1

1

10

deleted

1

cascade deleteto children

A

B

C

Page 4: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 4

Mark/SweepMark/Sweep & CompactionRoots

MM

M

M

• Mark accessible objects.• Sweep all objects, and now we realize,• unmarked objects A, B, and C are free

Expensive optional compaction eventclumps results together making free spaceone big block.

M

A

B

C

DE

F

HG

Page 5: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 5

Copying - The Flip or Scavenge

* When FromSpace is full we *flip* to ToSpace:- Copy roots of the world to ToSpace.- 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.

Root Root

A

B

C

D

A D B

C

FromSpace ToSpace

Flip

Notice the change of Placementand that E isn't copied.

E

Page 6: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 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.

Page 7: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 7

Generational Scavenge Event

RootA

B

C

E

A

D

B CRoot

Remembered Table (RT)

New Space

E

Old Space

A B and C get copied via Root reference. E is copied via OldSpace reference from D, which is remember by being stored in the Remember Table.

-> tenure ->

InterGenerational References

Page 8: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 8

VM Failure at end of chart, Why?Log scale

Of RT entries

Page 9: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 9

Squeak Memory LayoutGenerational Mark/Sweep Compacting

VM code

VM variables, plugins, DLLs

0x40000000startOfMemory

0x40ABFDEFyoungStart

0x40EBFDEFendOfMemory

Virtual MemoryFile mapped Blockused to store image.Grows and shrinks

On demand

0x60000000End of VM Block

Applies to Squeak VM that supportVirtual Memory file mapping andability to grow/shrink image space(win32, some unix, mac os-9/os-x)Issues with non 32bit clean code limit object space to first 2GB of Address space.

+

Malloc free space

GROWTH

Actual addressdepends on mmap

Old

Young

Page 10: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 10

IBM VisualAge Memory Layout v5.5.1Generational Copying (compacting optional)

semi-space - A

semi-space - B

NewSpace

OldSpace (RAM/ROM)FixedOldSpace

* 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)

CodeCache

262,144

262,144

Segments (lots!)

Text

Page 11: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 11

VisualWorks Memory Layout V5.i2

Generational Copying, + Incremental Mark/Sweep (Compacting optional)

* 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.

semi-space A

semi-space BEden

LargeSpace

RT OldSpace

PermSpaceORT

FixedSpaceStack

CodeCache

Page 12: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 12

SurvivorSpace small, watch how objects get tenured

Page 13: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 13

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 followupwith OldSpace Mark/Sweep/Compact

9 Grow Memory required

10 Grow Memory attempted, may fail, but usually is granted

Key to GC Events

Page 14: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 14

SurvivorSpace larger, less objects get tenured

Page 15: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 15

Beware trade-off is complex, bigger SurvivorSpace =? Poor Performance

Page 16: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 16

100,000 Logarithmic scale

Object Table Data Entries

1st Unit of Work

2ndUnit ofWork

Time Taken, (work units are same computation effort)

10,000

1,000

<- Initialization process ->

Impact of pre VW 5.x single free chain performance issueFirst unit of work takes 10x longer than 2nd unit of work. Why?

100

Page 17: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 17

Incorrect IGC work loads, IGC never completes

Page 18: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 18

Better IGC work loads, IGC always completes

Page 19: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 19

0

2

4

6

8

10

12

0 50 100 150 200 250 300 350 400 450 500

Case 1: User interaction test case before changes

6 Compaction events + 9 Quick GC events

10 Grow OldSpace

8 Finish IGC+Quick GC

2 Need to Compact1 Compact event

3 IGC justified

OldSpace - Free Memory

460 Seconds

Page 20: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 20

0

2

4

6

8

10

12

3 Compaction events + 5 QuickGC events versus 6 and 9

IGCAcceleration factor changed from 5 to 15. More GC work done inidle loop processing so fewer GC events

10 Grow OldSpace

8 Finish IGC+Quick GC

Case 1: User interaction test case after changes,

Old Case was ~460 seconds, we save 130 secs

2 Need to Compact1 Compact event

3 IGC justified

330 Seconds

Page 21: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 21

Algorithm issuescauses interestinggrowth pattern

500-800MB server application, issues with algorithms

Forced GCand object purgingfrees 400MB, an ugly solution

Page 22: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 22

Note Allocation freeChain performanceProblem, shows as gaps in the data

Old space is forced to shrink, then it grows!

Page 23: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 23

Different VW 7.x 300 MB client application, note Mark/Sweep completions

This looked OK, not much room to improve things

Gaps are CPU related

Lots of Tenuring here Less Tenuring here

Page 24: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 24

500 MB un-tuned vw7 server app, note IGC ceilings and excessive GC activity

Page 25: Garbage Collection in Smalltalk

© copyright 1997-2004 John M McIntosh, all rights reserved. Page 25

Before any tuning we saw:

Event Total1 Compact 332 Compact Need 63 Full ILIGC 464 Idle Loop 2605 Low Space 19216 Run IGC 17937 Request Grow 12910 Grow 129

Grand Total 4317

After tuning effort:

Event Total3 Full ILIGC 104 Idle Loop 1775 Low Space 12246 Run IGC 1224

Grand Total 2635

Mission Critical VW 3.x 400-500MB applicationBefore & After, shows what serious tuning can do

Zero code tuning was done.Application improved a few % for each SUnit test case. Keyboard responsiveness became “snappy”. Got rid of mystery lock-ups, a core dump (or two), and reduced the number of GC cursor events.

Page 26: Garbage Collection in Smalltalk

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

Garbage Collection in Smalltalk

* By John M McIntosh

- Corporate Smalltalk Consulting Ltd.

- http://www.smalltalkconsulting.com

- [email protected]

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

For ESUG 2004