On Tue, 21 May 2002, Erik Terpstra wrote:

> Yohanes Santoso wrote:
> > Erik Terpstra <erik / solidcode.net> writes:
> > 
> >>Is there some method that tells me how much memory a certain object
> >>consumes?
> >>
> >>For example the size of: ['Hello world!', 345, [2, 9.43553]]
> > 
> > String: 4 long + 1 pointer + the length of your string in bytes * 1 char
> > Fixnum: 2 long + 1 int
> > Array:  4 long + 1 pointer + the length of your array * 1 pointer 
> > 
> > This is all my guess from scanning ruby.h. Perhaps wrong, but should
> > be around the ballpark.
> 
> Hmmm thanks, but I'm affraid that doesn't really help me when I want to 
> know the memory consumption of a DOM tree for example.
> 
> In lower level languages you usually know if your program is efficient 
> with available memory because you allocate it yourself.
> 
> I find it very hard to make memory-efficient programs in Ruby when they 
> become larger and more complex.
> 
> Any thoughts?

Basically, if you are that concerned about memory efficiency, a few
comments would be:

  1. perhaps you don't "get" Ruby.  It was designed in such a way that
     you don't need to worry about allocating and deallocating memory.
     Ruby does both for you automatically, freeing you to worry about
     the real problem you are trying to solve.

  2. If your concern is a valid one within the problem domain you are
     trying to solve, then Ruby is probably not the best language to
     use for that problem.

  3. If it takes you more than an hour or so to solve a memory efficiency
     problem, you might want to ponder on the fact that at standard
     contract programming rates, you could buy several more megabytes
     of memory with what a contract programmer's time is worth.

  4. While, in the large, you do want to select an algorithm that makes
     the best use of memory, you don't want to sweat the small stuff too
     much.  Efficiency is doing a job right, but effectiveness is doing
     the right job.  First, think about it a bit and make sure that 
     focusing on memory efficiency is an effective use or your time.

Dennis