Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WAMemoryItem>>sizeOfObject #717

Closed
GoogleCodeExporter opened this issue Mar 25, 2015 · 3 comments
Closed

WAMemoryItem>>sizeOfObject #717

GoogleCodeExporter opened this issue Mar 25, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

I haven't checked if this bug exists already, so please ignore this if it's 
been reported before.

Bug in WAMemoryItem:

sizeOfObject: anObject
| headerSize instanceSize variableSize |
headerSize := anObject class indexIfCompact > 0
ifTrue: [ 4 ]
ifFalse: [ 8 ].
instanceSize := anObject class instSize.
variableSize := anObject class isBytes
ifTrue: [ anObject basicSize ]
ifFalse: [
anObject class isWords
ifTrue: [ Smalltalk wordSize * anObject basicSize ]
ifFalse: [ Smalltalk wordSize * anObject basicSize // 2 ] ].
^ headerSize + instanceSize + variableSize


In this snippet, the units that are added together in the last line are of 
different units.

instanceSize is in unit "number of machine words", whereas variableSize and 
headerSize are in unit "bytes".


The method is fixed here:

sizeOfObject: anObject
| headerSize instanceSize variableSize |
headerSize := anObject class indexIfCompact > 0
ifTrue: [ 4 ]
ifFalse: [ 8 ].
instanceSize := anObject class instSize * Smalltalk wordSize.
variableSize := anObject class isBytes
ifTrue: [ anObject basicSize ]
ifFalse: [
anObject class isWords
ifTrue: [ Smalltalk wordSize * anObject basicSize ]
ifFalse: [ Smalltalk wordSize * anObject basicSize // 2 ] ].
^ headerSize + instanceSize + variableSize



Example: 

|m|
dict := Dictionary new.
m := WAMemory new. dict traverseWithMemory: m seen: IdentitySet new. 
m totalSize

This will incorrectly spit out 10, but should return 16, on a 32 bit VM.

Niko

Original issue reported on code.google.com by renggli on 15 Mar 2012 at 7:27

@GoogleCodeExporter
Copy link
Author

Hi Niko,

    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

       byteSizeOfBody := anObject class isBytes
                                       ifTrue: [....]
                                       ifFalse: [.....].
       headerSize := byteSizeOfBody > 255
                               ifTrue: [12]
                               ifFalse:
                                     [anObject class indexIfCompact > 0
                                          ifTrue: [4]
                                          ifFalse: [8]].

best,
Eliot

Original comment by renggli on 15 Mar 2012 at 5:57

  • Added labels: ****
  • Removed labels: ****

@GoogleCodeExporter
Copy link
Author

And there is also #sizeInMemory

sizeInMemory
    "Answer the number of bytes consumed by this instance including object header."
    | contentBytes |

    contentBytes := Smalltalk wordSize. "base header"
    contentBytes := contentBytes + (self class instSize * Smalltalk wordSize). "instance vars"

    self class isVariable ifTrue:[ | bytesPerElement | "indexed elements"
        bytesPerElement := self class isBytes ifTrue: [1] ifFalse: [4].
        contentBytes := contentBytes + (self basicSize * bytesPerElement)
    ].

    contentBytes > 255 ifTrue:    [ contentBytes := contentBytes +  (2 * Smalltalk wordSize) ]
                        ifFalse:    [ self class isCompact
                                    ifFalse: [ contentBytes := contentBytes + Smalltalk wordSize]
                                ].
    ^contentBytes



which is also wrong for the cases of String. For example, 'aa' sizeInMemory 
answers 6 but should be 8 because of alligment.
If someone fixes this, I think #sizeInMemory would be correct.

Cheers 

Original comment by [email protected] on 2 Apr 2012 at 6:15

  • Changed state: Started
  • Added labels: Version-Seaside3.1
  • Removed labels: ****

@GoogleCodeExporter
Copy link
Author

Name: Seaside-Pharo-Development-pmm.60
Author: pmm
Time: 2 April 2012, 8:18:26 pm
UUID: 87b0c775-29d8-4219-acdf-6e30016f4608
Ancestors: Seaside-Pharo-Development-pmm.59

- Issue 717:    WAMemoryItem>>sizeOfObject
- http://code.google.com/p/seaside/issues/detail?id=717
- use #sizeInMemory, way easier and does the same

Name: Seaside-Tests-Pharo-Development-pmm.22
Author: pmm
Time: 2 April 2012, 8:18:54 pm
UUID: c7a4b49e-5eb0-4b74-9ff8-f74a73c24495
Ancestors: Seaside-Tests-Pharo-Development-lr.21

- Issue 717:    WAMemoryItem>>sizeOfObject
- http://code.google.com/p/seaside/issues/detail?id=717
- use #sizeInMemory, way easier and does the same

Original comment by [email protected] on 2 Apr 2012 at 6:19

  • Changed state: Fixed
  • Added labels: Priority-Low
  • Removed labels: Priority-Medium

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant