And there is also #sizeInMemory<br><br>sizeInMemory<br> "Answer the number of bytes consumed by this instance including object header."<br> | contentBytes |<br><br> contentBytes := Smalltalk wordSize. "base header"<br>
contentBytes := contentBytes + (self class instSize * Smalltalk wordSize). "instance vars"<br><br> self class isVariable ifTrue:[ | bytesPerElement | "indexed elements"<br> bytesPerElement := self class isBytes ifTrue: [1] ifFalse: [4].<br>
contentBytes := contentBytes + (self basicSize * bytesPerElement)<br> ].<br><br> contentBytes > 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, 'aa' 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"><<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>></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 > 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"> > 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 > 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"><<a href="mailto:niko.schwarz@googlemail.com" target="_blank">niko.schwarz@googlemail.com</a>></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't checked if this bug exists already, so please ignore this if it's been reported before.<br>
<br>
Bug in WAMemoryItem:<br>
<br>
sizeOfObject: anObject<br>
| headerSize instanceSize variableSize |<br>
headerSize := anObject class indexIfCompact > 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 "number of machine words", whereas variableSize and headerSize are in unit "bytes".<br>
<br>
<br>
The method is fixed here:<br>
<br>
sizeOfObject: anObject<br>
| headerSize instanceSize variableSize |<br>
headerSize := anObject class indexIfCompact > 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>