tags:

views:

264

answers:

5

hey Folks,

Is there any way in ruby to get the memory address of objects..

say..

(i = 5) is that possible to get the mem address of that object 5..

I have been trying to get this over some time..,

Any answer would be really appreciated...

thanks,

Regards levirg

+1  A: 

I don't know of a way of having the exact address, but maybe you're looking for something like the object_id method?

Extract from its documentation

Returns an integer identifier for obj.
The same number will be returned on all calls to id for a given object, and no two active objects will share an id


Example:

> 5.object_id
=> 11
> true.object_id
=> 2
Romuald Brunet
A: 
Jonas Elfström
it seems memprof wroks good for 64bit, mine is 32bit..... thanks
levirg
Thanks, I missed that in the readme for memprof. I'm also on 32-bit. See above for an explanation how Ruby stores Fixnum.
Jonas Elfström
that was great, I have learned something from that...thanks man..
levirg
A: 

What exactly are you trying to do?

Keep in mind that a Ruby object is not directly analogous to a variable in a language like C or C++. For example:

a = "foo"
b = a
b[2] = 'b'
b
  => "fob"
a
  => "fob"
a == b
  => true
a.object_id
  => 23924940
b.object_id
  => 23924940
a.object_id == b.object_id
  => true

Even through a and b are separate variables, they are references to the same underlying data and have the same object_id.

If you find yourself needing to take the address of a variable, there is probably an easier approach to whatever you are trying to do.

bta
I just wanted to know how referencing to objects work on ruby, so i thought address will be useful, to get things done..... if there is any better way pls help me...thanks
levirg
You shouldn't need to worry about referencing an object in Ruby. In contrast to other languages where you can pass an object by value or by reference, Ruby handles all the low-level aspects for you. All you should need to do is use the object and pass it around like you would if you were passing by value in C. For more information about the internals of Ruby, see http://www.ruby-doc.org/docs/ProgrammingRuby/language.html.
bta
A: 

Ruby Memory Validator does give you the memory address for the object.

Joe Damato's work (http://timetobleed.com/plugging-ruby-memory-leaks-heapstack-dump-patches-to-help-take-out-the-trash) and (http://timetobleed.com/memprof-a-ruby-level-memory-profiler) is based on the work Software Verification did to create a Ruby memory inspection API (http://www.softwareverify.com/ruby/customBuild/index.html).

Joe describes that on his blog. Therefore Joe's work should also return the appropriate addresses. I'm not fully up to speed with the latest version of Joe's work - he only told me about the first version, not the latest version, but nonetheless, if you are tracking memory allocations in the underpinnings of Ruby, you are tracking the addresses of the objects that hold whatever it is you are allocating.

That doesn't mean you can dereference the address and read the data value you expect to find at that address. Dereferencing the address will point you to the internals of a basic Ruby Object. Ruby objects are a basic object which then store additional data alongside, so knowing the actual address is not very useful unless you are writing a tool like Ruby Memory Validator or memprof.

How do I know the above about Ruby Memory Validator and the API we released? I designed Ruby Memory Validator. I also wrote the assembly language bits that intercept the Ruby calls that allocate the memory.

Stephen Kellett
A: 

Since you indicated (buried in a comment somewhere) that you're really just trying to understand how Ruby references things, I think things work as follows:

A VALUE in Ruby's C api represents an object (a nil, a FixNum or a Boolean) or a pointer to an Object. The VALUE contains a 3 bit tag indicating which of these it is, and contains the value (for the first 3) or a direct memory pointer (for an Object). There's no way to get at the VALUE directly in Ruby, (I'm not sure if the object_id is the same or different.)

Note that JRuby operates differently.

Ken Bloom