Garbage collection and symbols

P

Phrogz

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
 
T

tim.pease

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
 
R

rhkramer

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 Kramer
 
R

rick.denatale

* 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)

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Forum statistics

Threads
474,270
Messages
2,571,341
Members
48,029
Latest member
Anchorman2022

Latest Threads

Top