Search code examples
memorymemory-leakspharo

How to identify "Space is low" cause


So sometimes you get "Space is low" dialog in Pharo. Is it possible to somehow identify what is taking that much space?


Solution

  • That's not an easy problem because in the process of gathering that information you are likely to exceed the maximum amount of space.

    There's SmalltalkImage>>lowSpaceThreshold that you could modify to get a bit more room for experimenting.

    What you also can do is look at SmalltalkImage>>memoryHogs. That class variable will be populated by the VM (I think) with the top memory consuming objects (if such an object can be identified).

    Another thing you can do if you're unable to pinpoint single instances of large objects, is to look at the number of all instances of all the classes. This takes a bit of space and you should be careful. But here's some code that would work (restricted to interesting packages):

    classes := Class allSubclasses select: [ :class | class name beginsWith: 'AB' ].
    (classes collect: [ :class | class -> class allInstances size ]) inspect.
    

    Then, there's also TBehavior>>instancesSizeInMemory and Object>>sizeInMemory. These can really crash your image if you run them on a graph (you'll have to implement that yourself) but they give you a good indication about which objects consume how much memory.

    You could adapt the code from above to the following to get the size of the instances:

    classes := Class allSubclasses select: [ :class | class name beginsWith: 'AB' ].
    (classes collect: [ :class | class -> class instancesSizeInMemory ]) inspect.
    

    Note: code examples are meant for Pharo4.