Search code examples
smalltalkpharoseaside

Pharo Smalltalk - Is there a way to force deallocation (dealloc) on an Object (instance) (expire sessions etc) during Seaside development?


When you want to garbage collect a certain instance, what message can you send it?

I'm hacking away at a Seaside based app, and a lot of things ca go haywire, including my RootComponent having 10 instances (is this normal), and I'd like a fresh start (not a fresh image)

Later edit: After reading a bit, apparently the question is anathema in the Automatically Garbage Collected world, so let me rephrase.


Solution

  • There is no way to make the VM garbage collect a specific object. You can evaluate Smalltalk garbageCollect. to force garbage collection but that will simply trigger the garbage collection mechanism of the VM for all objects.

    There are usually two problems here (in my experience):

    1. garbage collection in the Pharo VM uses two different algorithms for two separate phases (I believe they are "mark-and-sweep" and "scavenge"). A single Smalltalk garbageCollect. might thus not be sufficient to actually remove the object. You will often see code like 3 timesRepeat: [ Smalltalk garbageCollect ]. which tries to ensure that an object gets collected. Note also that some special objects are protected from garbage collection (like nil, true, false etc.).
    2. in certain situations the garbage collection algorithm does not seem capable of resolving all obsolete objects. I've seen this happen with the Morphic event mechanism. There, an event is simply a closure in the context of a method. Even if the registered objects have been garbage collected, some events will hang around (if the responsible object didn't unregister them correctly), creating entire graphs of objects that can't be collected (since the root can't be collected). I suspect that the actual problem is some kind of circular dependency. Note that this is an exceptional case and that forced garbage collection works quite well in general.

    With that said, we've been using Seaside for close to a decade now and garbage collection is usually one of our least concerns. When we need something garbage collected / cleaned up, nilling the variables and forcing garbage collection (3 times) does the trick.

    Edit

    In Seaside 2.8 you can evaluate WARegistry clearAllHandlers., in Seaside 3 that is roughly equivalent to WAAdmin clearAll.. These statements will clean up active and expired sessions and trash most of the stuff you might be concerned about. Note: #clearAll will also clear configurations and registered entry points. Use #clearSessions if you only want to reset the sessions. Use #clearAll if you require a more thorough clean up.

    Edit 2

    Use myObject pointersTo. to find all the pointers pointing to a particular object. You can also right click on an entry in the GTInspector and click on "open pointers to".

    You might also want to check out PointerDetective, which tries to graphically represent the pointer finding process.