[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Garbage collection and symbols

Hendrik Louw

10/31/2007 5:20:00 PM

I stumbled accross this link
http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby...
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.
--
Posted via http://www.ruby-....

5 Answers

Phrogz

11/1/2007 3:25:00 AM

0

On Oct 31, 11:19 am, Hendrik Louw <hfl...@gmail.com> wrote:
> I stumbled accross this linkhttp://www.randomhacks.net/articles/2007/01/20/13-ways-of-l......
> 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+...
or
http://www.google.com/search?q=Are+symbols+ever+garbage+collect...

7stud --

11/1/2007 7:04:00 AM

0

Gavin Kistner wrote:
> Really, that's the only thing you can find on the subject?
>
> Let me introduce you to my good friend "Google":

lol
--
Posted via http://www.ruby-....

Tim Pease

11/1/2007 4:10:00 PM

0

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

> I stumbled accross this link
> http://www.randomhacks.net/articles/2007/01/20/13-ways-o...
> 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


Randy Kramer

11/1/2007 7:07:00 PM

0

On Thursday 01 November 2007 12:09 pm, Tim Pease 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 Kramer

Rick DeNatale

11/1/2007 9:38:00 PM

0

On 11/1/07, Randy Kramer <rhkramer@gmail.com> 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.denh...