Skip to main content
Engineering LibreTexts

16.8: SpaceTally for Memory Consumption per Class

  • Page ID
    46428
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    It is often important to know the amount of instances and the memory consumption of a given class. The class SpaceTally offers this functionality.

    The expression SpaceTally new printSpaceAnalysis runs over all the classes of the system and gathers for each of them its code size, the amount of instances and the total memory space taken by the instances. The result is sorted along the total memory space taken by instances and is stored in a file named STspace.text, located next to the Pharo image.

    It is not surprising to see that strings, compiled methods and bitmaps represents the largest part of the Pharo memory. The proportion of the compiled code, string and bitmap may be found in other platforms for diverse applications.

    SpaceTally’s output is structured as follows:

    Class                    code space     #instances   inst space    percent    inst average size
    ByteString                     2053         109613      9133154      31.20                83.32
    Bitmap                         3653            379      6122156      20.90             16153.45
    CompiledMethod                20067          51579      3307151      11.30                64.12
    Array                          2535          85560      3071680      10.50                35.90
    ByteSymbol                     1086          35746       914367       3.10                25.58
    ...
    

    Each line represents the memory analysis of a Pharo class. Classes are ordered along the space they occupy. The class ByteString describes strings. It is frequent to have strings to consume one third of the memory. Code space gives the amount of bytes used by the class and its metaclass. It does not include the space used by class variables. The value is given by the method Behavior>>spaceUsed.

    The # instances column gives the amount of instances. It is the result of Behavior>>instanceCount. The inst space column is the amount of bytes consumed by all the instances, including the object header. It is the result of Behavior>>instancesSizeInMemory. The percentage of the memory occupation is given by the column percent and the last column gives the average size of instances.

    Running SpaceTally on all classes takes a few minutes. SpaceTally may also be executed on a reduced set of classes to increase the analysis time. Consider:

    ((SpaceTally new spaceTally: (Array with: TextMorph with: Point))
        asSortedCollection: [:a :b | a spaceForInstances > b spaceForInstances])
    

    The method SpaceTally>>spaceTally: analyzes the memory consumed by each classes of its argument. It returns a list of instance of SpaceTallyItem.


    This page titled 16.8: SpaceTally for Memory Consumption per Class is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Alexandre Bergel, Damien Cassou, Stéphane Ducasse, Jannik Laval (Square Bracket Associates) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.