sizeof(Class)/sizeof(Object)

Discussion in 'Ruby' started by Nikolai Weibull, Dec 31, 2004.

  1. I would like to figure out how much memory a certain class or object
    will/is consuming. Is there an easy way? Is there a hard way?
    The reason I ask is because I want to keep several hundred thousand
    instances of an object much like the following one:

    class A
    def initialize
    @a = @b = @c = @d = nil
    @e = 1
    end
    end

    nikolai

    --
    ::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
    ::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
    ::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
    main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
     
    Nikolai Weibull, Dec 31, 2004
    #1
    1. Advertising

  2. Nikolai Weibull wrote:
    > I would like to figure out how much memory a certain class or object
    > will/is consuming. Is there an easy way? Is there a hard way?
    > The reason I ask is because I want to keep several hundred thousand
    > instances of an object much like the following one:
    >
    > class A
    > def initialize
    > @a = @b = @c = @d = nil
    > @e = 1
    > end
    > end
    >


    Instance variables are stored in a hash table associated with each
    object. See rb_ivar_set() in variable.c. So basically each instance
    will take up the size of struct RObject - which is 20 bytes on a 32bit
    machine, plus the size of the hash table (see st.h), plus something
    else I am probably missing. As far as I know st.h doesn't provide any
    means of calcing the number of bytes used by the hash.
    Is there a way to do this?

    Also, objects with no instance variables don't create a hash table to
    store instance variables...

    -Charlie
     
    Charles Mills, Dec 31, 2004
    #2
    1. Advertising

  3. "Charles Mills" <> schrieb im Newsbeitrag
    news:...
    > Nikolai Weibull wrote:
    >> I would like to figure out how much memory a certain class or object
    >> will/is consuming. Is there an easy way? Is there a hard way?
    >> The reason I ask is because I want to keep several hundred thousand
    >> instances of an object much like the following one:
    >>
    >> class A
    >> def initialize
    >> @a = @b = @c = @d = nil
    >> @e = 1
    >> end
    >> end
    >>

    >
    > Instance variables are stored in a hash table associated with each
    > object. See rb_ivar_set() in variable.c. So basically each instance
    > will take up the size of struct RObject - which is 20 bytes on a 32bit
    > machine, plus the size of the hash table (see st.h), plus something
    > else I am probably missing. As far as I know st.h doesn't provide any
    > means of calcing the number of bytes used by the hash.
    > Is there a way to do this?
    >
    > Also, objects with no instance variables don't create a hash table to
    > store instance variables...


    So if there was only one instance var an optimization could be to store them
    in a hash in the instance's class. This could work for multiple ivars as
    well but then there is the array overhead. Hm...

    class Foo
    @ivar = {}

    def initialize
    cl = self.class
    ObjectSpace.define_finalizer(self) {|id| cl.remove id}
    end

    def bar=(x) self.class.set_val(id, x) end
    def bar; self.class.get_val(id) end

    def self.set_val(id, val)
    @ivar[id] = val
    end

    def self.get_val(id)
    @ivar[id]
    end

    def self.remove(id)
    puts "removing #{id}"
    @ivar.delete id
    end
    end

    Just some thoughts in code...

    robert
     
    Robert Klemme, Dec 31, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,395
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    655
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,483
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    574
    Joe Smith
    May 22, 2006
  5. GRenard
    Replies:
    2
    Views:
    443
    Salt_Peter
    Nov 3, 2006
Loading...

Share This Page