Squeak SmalltalkJoker Squeak Smalltalk : System : prevnext Large Collection Automatic Finalization

I've seen this problem myself. It is easiest to see what happens when 
you have a process browser open and turn on the cpu watcher - this 
will show that the finalization process takes a huge amount of 
resources.

But why? Most likely (this was the case I have experienced) you have 
created some weak collection with what I consider "automatic 
finalization", e.g., WeakRegistry and friends register themselves to 
get notified when a weak references got freed. If this registry grows 
very large it can take significant amounts of time to do the 
finalization and if your code is then weak reference heavy you may 
spend a lot of time in finalization.

Here is an example illustration the problem:

| hog proc |
hog := WeakIdentityKeyDictionary new.
CompiledMethod allInstancesDo:[:cm| hog at: cm put: 42.0].
Smalltalk at: #CPUHog put: hog.
WeakArray addWeakDependent: hog.

"keep the finalization process busy"
proc := [
ww := WeakArray new: 1.
ww at: 1 put: Object new.
Smalltalk garbageCollectMost.
] forkAt: Processor userBackgroundPriority.

Now watch the CPU usage of the finalization process in the process 
browser (on my system it's in the 10-20% range).

The solution? Don't do it. Don't use automatic finalization for large 
collections. Do it manually every now and then, for example, having a 
timer process which looks over the elements (this might get triggered 
by a signal from the finalization process btw).

Cheers,

 - Andreas