[Pharo-project] Size of objects
Hello, For an experimentation, I need to know the size in bytes of any object. Is there a way to obtain this information in Pharo ? Fréd -- Frédéric Pluquet Université Libre de Bruxelles (ULB) Assistant http://www.ulb.ac.be/di/fpluquet
Hi Frédéric Nice to see you on the Pharo mailing list! Just the other day I needed this too and I implemented the following method: Object>>sizeInMemory "Returns the number of bytes used by this object in memory (including its header)" | headerSize instanceSize | headerSize := (self class indexIfCompact > 0 ifTrue: [ 4 ] ifFalse: [ 8 ]). instanceSize := (self class isVariable ifFalse: [ self class instSize * Smalltalk wordSize ] ifTrue: [ (self basicSize * (self class isBytes ifTrue: [ 1 ] ifFalse: [ Smalltalk wordSize ])) ]). ^ headerSize + instanceSize BTW, I think that would be a meaningful addition. Could somebody quickly check the method to verify that I didn't do any miscalculation? Cheers, Adrian On Oct 21, 2008, at 17:58 , Frederic Pluquet wrote:
Hello,
For an experimentation, I need to know the size in bytes of any object. Is there a way to obtain this information in Pharo ?
Fréd
-- Frédéric Pluquet Université Libre de Bruxelles (ULB) Assistant http://www.ulb.ac.be/di/fpluquet _______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
BTW, I think that would be a meaningful addition. Could somebody quickly check the method to verify that I didn't do any miscalculation?
The only problem I see is that variable objects might also have inst-vars. So you need to count the self class instSize * Smalltalk wordSize part in any case. Lukas -- Lukas Renggli http://www.lukas-renggli.ch
may be compare with the vw version I remember that lomg time ago in book of the guy doing june there was some code to do that http://www.sra.co.jp/people/aoki/ in freedocument then Smalltalk Idioms in English then chapter 3 Here let me introduce a program that finds the size in bytes of an arbitrary object. Finding the size of an object is the quickest route toward understanding the internal structure of objects. "Calculate the size in bytes of an arbitrary object." | byteSizeOfObject | byteSizeOfObject := [:anObject | | bytesInOTE bytesInOOP aClass indexableFieldSize instVarFieldSize byteSize | bytesInOTE := ObjectMemory current bytesPerOTE. bytesInOOP := ObjectMemory current bytesPerOOP. aClass := anObject class. 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 + (bytesInOOP - 1) bitAnd: bytesInOOP negated] ifFalse: [indexableFieldSize := 0. aClass hasImmediateInstances ifTrue: [bytesInOTE := 0]]]. byteSize := bytesInOTE + instVarFieldSize + indexableFieldSize. byteSize yourself]. ^byteSizeOfObject value: (OrderedCollection new: 10) have a look at the book. now in VW numOopsNumBytes "Answer an Array containing the number of objects in the system and the number of bytes they occupy. This method is implementation dependent." "ObjectMemory current numOopsNumBytes" | nobjects nbytes class bytesInHeader bytesInOOP | nobjects := nbytes := 0. bytesInHeader := self bytesPerOTE. bytesInOOP := self bytesPerOOP. self class allObjectsDo: [:object | nobjects := nobjects + 1. nbytes := nbytes + bytesInHeader. (class := object class) isBits ifTrue: [nbytes := nbytes + (object basicSize + (bytesInOOP - 1) bitAnd: bytesInOOP negated)] ifFalse: [nbytes := nbytes + (class instSize * bytesInOOP). class isVariable ifTrue: [nbytes := nbytes + (object basicSize * bytesInOOP)]]]. ^Array with: nobjects with: nbytes On Oct 21, 2008, at 6:15 PM, Adrian Lienhard wrote:
Hi Frédéric
Nice to see you on the Pharo mailing list!
Just the other day I needed this too and I implemented the following method:
Object>>sizeInMemory "Returns the number of bytes used by this object in memory (including its header)"
| headerSize instanceSize | headerSize := (self class indexIfCompact > 0 ifTrue: [ 4 ] ifFalse: [ 8 ]). instanceSize := (self class isVariable ifFalse: [ self class instSize * Smalltalk wordSize ] ifTrue: [ (self basicSize * (self class isBytes ifTrue: [ 1 ] ifFalse: [ Smalltalk wordSize ])) ]). ^ headerSize + instanceSize
BTW, I think that would be a meaningful addition. Could somebody quickly check the method to verify that I didn't do any miscalculation?
Cheers, Adrian
On Oct 21, 2008, at 17:58 , Frederic Pluquet wrote:
Hello,
For an experimentation, I need to know the size in bytes of any object. Is there a way to obtain this information in Pharo ?
Fréd
-- Frédéric Pluquet Université Libre de Bruxelles (ULB) Assistant http://www.ulb.ac.be/di/fpluquet _______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
participants (4)
-
Adrian Lienhard -
Frederic Pluquet -
Lukas Renggli -
Stéphane Ducasse