And there is also #sizeInMemory<br><br>sizeInMemory<br>    &quot;Answer the number of bytes consumed by this instance including object header.&quot;<br>    | contentBytes |<br><br>    contentBytes := Smalltalk wordSize. &quot;base header&quot;<br>
    contentBytes := contentBytes + (self class instSize * Smalltalk wordSize). &quot;instance vars&quot;<br><br>    self class isVariable ifTrue:[ | bytesPerElement | &quot;indexed elements&quot;<br>        bytesPerElement := self class isBytes ifTrue: [1] ifFalse: [4].<br>
        contentBytes := contentBytes + (self basicSize * bytesPerElement)<br>    ].<br><br>    contentBytes &gt; 255 ifTrue:    [ contentBytes := contentBytes +  (2 * Smalltalk wordSize) ]<br>                        ifFalse:    [ self class isCompact<br>
                                    ifFalse: [ contentBytes := contentBytes + Smalltalk wordSize]<br>                                ].<br>    ^contentBytes<br><br><br><br>which is also wrong for the cases of String. For example, &#39;aa&#39; sizeInMemory answers 6 but should be 8 because of alligment. <br>
If someone fixes this, I think #sizeInMemory would be correct.<br><br>Cheers <br><br><div class="gmail_quote">On Thu, Mar 15, 2012 at 6:53 PM, Eliot Miranda <span dir="ltr">&lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <br>Hi Niko,<div><br></div><div>    the header size calculation is also wrong.  If the byte size if the object body is &gt; 255 bytes there needs to be an additional header size word.  So something like</div>
<div><br></div><div>
       byteSizeOfBody := anObject class isBytes</div><div>                                       ifTrue: [....]</div><div>                                       ifFalse: [.....].</div><div><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px">       headerSize := </span>byteSizeOfBody<span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px"> &gt; 255</span></div>

<div><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px">                               ifTrue: [12]</span></div><div><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px">                               ifFalse:</span></div>

<div><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px">                                     [anObject class indexIfCompact &gt; 0<br>                                          ifTrue: [4]<br>

                                          ifFalse: [8]].<br></span><br><div class="gmail_quote">On Wed, Mar 14, 2012 at 1:05 PM,  <span dir="ltr">&lt;<a href="mailto:niko.schwarz@googlemail.com" target="_blank">niko.schwarz@googlemail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi guys,<br>
<br>
I haven&#39;t checked if this bug exists already, so please ignore this if it&#39;s been reported before.<br>
<br>
Bug in WAMemoryItem:<br>
<br>
sizeOfObject: anObject<br>
        | headerSize instanceSize variableSize |<br>
        headerSize := anObject class indexIfCompact &gt; 0<br>
                ifTrue: [ 4 ]<br>
                ifFalse: [ 8 ].<br>
        instanceSize := anObject class instSize.<br>
        variableSize := anObject class isBytes<br>
                ifTrue: [ anObject basicSize ]<br>
                ifFalse: [<br>
                        anObject class isWords<br>
                                ifTrue: [ Smalltalk wordSize * anObject basicSize ]<br>
                                ifFalse: [ Smalltalk wordSize * anObject basicSize // 2 ] ].<br>
        ^ headerSize + instanceSize + variableSize<br>
<br>
<br>
In this snippet, the units that are added together in the last line are of different units.<br>
<br>
instanceSize is in unit &quot;number of machine words&quot;, whereas variableSize and headerSize are in unit &quot;bytes&quot;.<br>
<br>
<br>
The method is fixed here:<br>
<br>
sizeOfObject: anObject<br>
        | headerSize instanceSize variableSize |<br>
        headerSize := anObject class indexIfCompact &gt; 0<br>
                ifTrue: [ 4 ]<br>
                ifFalse: [ 8 ].<br>
        instanceSize := anObject class instSize * Smalltalk wordSize.<br>
        variableSize := anObject class isBytes<br>
                ifTrue: [ anObject basicSize ]<br>
                ifFalse: [<br>
                        anObject class isWords<br>
                                ifTrue: [ Smalltalk wordSize * anObject basicSize ]<br>
                                ifFalse: [ Smalltalk wordSize * anObject basicSize // 2 ] ].<br>
        ^ headerSize + instanceSize + variableSize<br>
<br>
<br>
<br>
Example:<br>
<br>
 |m|<br>
dict := Dictionary new.<br>
m := WAMemory new. dict traverseWithMemory: m seen: IdentitySet new.<br>
m totalSize<br>
<br>
This will incorrectly spit out 10, but should return 16, on a 32 bit VM.<br>
<br>
Niko<br>
<font color="#888888"><br>
--<br>
<a href="http://scg.unibe.ch/staff/Schwarz" target="_blank">http://scg.unibe.ch/staff/Schwarz</a><br>
<a href="http://twitter.com/nes1983" target="_blank">twitter.com/nes1983</a><br>
Tel: <a href="tel:%2B41786126354" value="+41786126354" target="_blank">+41786126354</a><br>
<br>
<br>
</font></blockquote></div><br><br clear="all"><div><br></div>-- <br>best,<div>Eliot</div><br>
</div>
<br></blockquote></div><br><br clear="all"><br>-- <br>Mariano<br><a href="http://marianopeck.wordpress.com" target="_blank">http://marianopeck.wordpress.com</a><br><br>