Garbage collection and symbols

I stumbled accross this link
http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#11
. This is about the only thing I can find on the subject. I want to find
out if symbols ever get freed during the lifetime of a ruby program.

On Oct 31, 11:19 am, Hendrik L. [email protected] wrote:

I stumbled accross this linkhttp://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-…
This is about the only thing I can find on the subject. I want to find
out if symbols ever get freed during the lifetime of a ruby program.

Really, that’s the only thing you can find on the subject?

Let me introduce you to my good friend “Google”:
http://www.google.com/search?q=ruby+symbols+garbage+collection
or
http://www.google.com/search?q=Are+symbols+ever+garbage+collected+in+ruby

Gavin K. wrote:

Really, that’s the only thing you can find on the subject?

Let me introduce you to my good friend “Google”:

lol

On Thursday 01 November 2007 12:09 pm, Tim P. wrote:

Symbols are never freed during the lifetime of a ruby program. Once
they are allocated in the symbol hash table, they are there forever.

(Emphasis added.)

Thanks, that helps me a lot (I guess I (sometimes) like to know what’s
going
on behind the scenes). And that helps me understand some of the stuff I
was
pondering over a few weeks ago during the discussion of (iirc)
references vs.
pointers.

Hmm, thinking of that, maybe you or someone else can confirm a few
points:

  • one difference between a C pointer and a Ruby reference that I’m
    “walking
    away with” is that the C pointer is stored in a location (and takes up
    space)
    where the programmer can access it (and can notice that it takes up
    space).
    In contrast, a Ruby variable (which is a reference) is not as easily
    accessible to the programmer, but it does take up space in the (Ruby’s)
    symbol hash table.

  • in Ruby’s symbol hash table, a symbol is stored (in the hash table)
    with
    all the characters that make up the symbol. For example, if I have a
    symbol :test, in Ruby’s hash table, the string “test” is stored. (I’m
    guessing as the key–I presume the value is something that somehow
    eventually
    is translated into a location in memory.)

Is there a way for the programmer to manipulate Ruby’s symbol hash table
within Ruby more directly than occurs when defining variables or
symbols?

Randy K.

On 11/1/07, Randy K. [email protected] wrote:

  • one difference between a C pointer and a Ruby reference that I’m “walking
    away with” is that the C pointer is stored in a location (and takes up space)
    where the programmer can access it (and can notice that it takes up space).
    In contrast, a Ruby variable (which is a reference) is not as easily
    accessible to the programmer, but it does take up space in the (Ruby’s)

symbol hash table.

Not quite.

The big differences.

Ruby variables always reference objects. Although the implementation
is ‘pointer-like’ References aren’t always implemented as pointers.
For example Fixnums (integers representable by 1-bit less than a
machine word) are represented by a manipulation of the integer value
(the MRI implementation represents an integer i as a reference
(i*2)+1.

Ruby variables, unlike C pointer variables can’t themselves be
referenced indirectly.

There’s no direct correspondence between variables and symbols.
Instance variables and class variables do use a symbol lookup in a
hash to find them by name, but local variables will iive on the stack,
and slots in Arrays’ Hashes and other primitive ruby objects are
nameless variables which, since they are variables hold references.

In MRI symbols are, or have been, used to represent instance/class
variable namss (as I mentioned), and also method names Methods are
found by looking up the name in a series of hash lookups, at least
notionally.

So besides creating symbols with a :symbol literal they may also get
defined when you define an instance variable, class variable, or
method with a previously unseen symbol.

Again this is somewhat implementation dependent.

  • in Ruby’s symbol hash table, a symbol is stored (in the hash
    table) with
    all the characters that make up the symbol. For example, if I have a
    symbol :test, in Ruby’s hash table, the string “test” is stored. (I’m
    guessing as the key–I presume the value is something that somehow eventually
    is translated into a location in memory.)

Again, this is implementation dependent. It’s not specified by the
language definition, even if there were one.

Is there a way for the programmer to manipulate Ruby’s symbol hash table
within Ruby more directly than occurs when defining variables or symbols?

In the way I understand you to mean, only by writing an extension in C.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Oct 31, 2007, at 11:19 AM, Hendrik L. wrote:

I stumbled accross this link
http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-
at-a-ruby-symbol#11
. This is about the only thing I can find on the subject. I want to
find
out if symbols ever get freed during the lifetime of a ruby program.

Symbols are never freed during the lifetime of a ruby program. Once
they are allocated in the symbol hash table, they are there forever.

If you want to bring your machine to a crawl, run this little ruby
script …

ruby -e ‘(2**24).times {|ii| “a#{ii}”.intern}’

Blessings,
TwP

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs