Search code examples
smalltalk

Smalltalk - How to remove everything in memory?


I'm using Pharo 3.0

Is there a way to remove all objects from memory? I've tried closing and re-opening my image, but it appears that everything is still in memory.

For example, I had: General Ledger --> Spending Manager and both setup as a singleton:

GeneralLedger>>new
    instance isNil ifTrue: [instance := super new].
    ^ instance

SpendingManager>>new
    instance isNil ifTrue: [instance := super new].
    ^ instance

and messed up somewhere along the way when instantiating Spending Manager - i.e., I returned the instance of General Ledger and not the instance of Spending Manager I guess?

Now when I load my image and try to do CTRL+P on: spnder:=SpendingManager new. I get:

spnder:=SpendingManager new. a GeneralLedger

I've even removed the inheritance from SpendingManager so that it now inherits from Object, but this still happens.


Solution

  • How did you declare instance?

    In such case it should be declared like this:

    YourSUperClass classInstanceVariables: 'instance'.
    

    Maybe you did it correctly, but it's unclear from your post (and the hierarchy between your two classes is unclear too).

    Your main problem is the usage of super new.
    When you create an instance of the subclass for the first time, it will send super new which will:

    • either return an already initialized instance of superclass

    • or register an instance of the subclass as the superclass singleton

    You should use instance := self basicNew initialize instead.

    Also you can write the whole thing

    ^instance ifNil: [instance := self basicNew initialize]
    

    The other possibility is just to NOT redefine new in subclass, the new from superclass will just work.

    And last thing, to remove the initialization, just inspect the class and modify the 'instance' class instance variable directly from the Editor (select this field, type nil and accept).