[Squeak] How to get the size of any object (sorry too long)

NISHIHARA Satoshi nishis at urban.ne.jp
Mon Nov 9 20:08:34 UTC 1998


At 07:43 09/22/1998 -0800, Maloney wrote:
>Squeak has variable-size object headers, unlike VW. They
>can be one (common case), two, or three words (4, 8, or 12 bytes).
>
>You might look at the code for counting objects and their
>memory space in SystemDictionary.
>
>        -- John

Thank you for advice, 
Looking at the methods #printSpaceAnalysis:on: and #spaceUsed
    Browser fullOnClass: SystemDictionary selector: #printSpaceAnalysis:on:.
    Browser fullOnClass: ClassDescription selector: #spaceUsed.
but more questions appear ... ;-)

As first,
Q1)
byteSize of anObject is total of
  object table entry size, 
  object header size, 
  instVarFieldSize, 
  indexableFieldSize

byteSize:= bytesInOTE + objectHeadersSize + instVarFieldSize +
indexableFieldSize.

mmm... Is this right?

case: right
Q1.1)
object table entry size is 8 bytes? or 12 bytes?

Q1.2)
If anObject is a instance of BlockContext, 
must add the size of home method or not?

Q1.3)
aSmallInteger or aCharacter is immediate object.
So consider as its bytesize = 0.
Is this right?

Q1.4)
getting the byteSize of aMethod, must add methodHeaderSize (4 bytes)
So its total is methodHeaderSize + bytesize of aMethod self + size of literals.
Is this right?


code fragments are below.

get the size of anObject

    aClass := anObject class.
    "baseHeaderSize = 4"
    baseHeaderSize := self baseHeaderSize.
    objectHeadersSize := aClass indexIfCompact > 0 
                ifTrue: [baseHeaderSize] 
                ifFalse: 
                    [((self sizeInWordsOf: anObject) + 1 ) > 16r3F
                        ifTrue: [baseHeaderSize * 3]
                        ifFalse: [baseHeaderSize * 2]].
    "really?"
    bytesInOTE := 8 "? or 12?".
    bytesInOOP := aClass isBytes ifTrue: [1] ifFalse: [4].
    aClass isPointers
        ifTrue: 
            [instVarFieldSize := aClass instSize * bytesInOOP.
            aClass isVariable
                ifTrue: 
                    [indexableFieldSize := anObject basicSize * bytesInOOP]
                ifFalse: 
                    [indexableFieldSize := 0]]
        ifFalse: 
            [instVarFieldSize := 0.
            aClass isVariable
                ifTrue: 
                    [indexableFieldSize := anObject basicSize + (4 - 1)
bitAnd: 4 negated]
                ifFalse: 
                    ["never fall down here"
                    indexableFieldSize := 0]].
    byteSize := bytesInOTE + objectHeadersSize + instVarFieldSize +
indexableFieldSize.


get the size of aMethod

    methodHeaderSize := 4.
    byteSize := self byteSizeOfObject: aMethod.
    byteSize := byteSize + methodHeaderSize.
    aMethod
        literals do: 
            [:literal | 
            size := self weightForLiteral: literal.
            byteSize := byteSize + size].
    ^byteSize


get the size of aLiteral

    (literalClass isBits and: [(aLiteral isKindOf: Symbol) not])
        ifTrue: [^self byteSizeOfObject: aLiteral].
    literalClass == Array 
        ifTrue: 
            ["total size of its elements"
            ^self weightForLiteralArray: aLiteral].
-------------------------------------------
$B@>86Ao;N(B (NISHIHARA Satoshi)
e-mail: nishis at urban.ne.jp
      : tcc00164 at nifty.ne.jp
URL: http://www.urban.ne.jp/home/nishis/
-------------------------------------------





More information about the Squeak-dev mailing list