# Garbage collection and symbols

**URL:** <https://www.ruby-forum.com/t/garbage-collection-and-symbols/118858>\
**Category:** Ruby\
**Created:** [October 31, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/garbage-collection-and-symbols/118858 "2007-10-31T18:19:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![impi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7993a0/32.png) [@impi](https://www.ruby-forum.com/u/impi)\
**Post date:** [October 31, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/garbage-collection-and-symbols/118858/1 "2007-10-31T18:19:53Z")

</div>

I stumbled accross this link

> **[13 Ways of Looking at a Ruby Symbol | Random Hacks](http://www.randomhacks.net.s3-website-us-east-1.amazonaws.com/2007/01/20/13-ways-of-looking-at-a-ruby-symbol/)**
>
> Random code snippets, projects and musings about software from Eric Kidd, a developer and occasional entrepreneur.

. 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.

---

<div class="post-metadata">

**Author:** ![impi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7993a0/32.png) [@impi](https://www.ruby-forum.com/u/impi)\
**Post date:** [November 1, 2007, 4:25am UTC](https://www.ruby-forum.com/t/garbage-collection-and-symbols/118858/2 "2007-11-01T04:25:31Z")

</div>

On Oct 31, 11:19 am, Hendrik L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.google.com/search?q=ruby+symbols+garbage+collection)  
or  
[http://www.google.com/search?q=Are+symbols+ever+garbage+collected+in+ruby](http://www.google.com/search?q=Are+symbols+ever+garbage+collected+in+ruby)

---

<div class="post-metadata">

**Author:** ![impi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7993a0/32.png) [@impi](https://www.ruby-forum.com/u/impi)\
**Post date:** [November 1, 2007, 8:04am UTC](https://www.ruby-forum.com/t/garbage-collection-and-symbols/118858/3 "2007-11-01T08:04:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![impi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7993a0/32.png) [@impi](https://www.ruby-forum.com/u/impi)\
**Post date:** [November 1, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/garbage-collection-and-symbols/118858/4 "2007-11-01T20:14:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![impi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7993a0/32.png) [@impi](https://www.ruby-forum.com/u/impi)\
**Post date:** [November 1, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/garbage-collection-and-symbols/118858/5 "2007-11-01T22:44:57Z")

</div>

On 11/1/07, Randy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![impi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7993a0/32.png) [@impi](https://www.ruby-forum.com/u/impi)\
**Post date:** [November 1, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/garbage-collection-and-symbols/118858/6 "2007-11-01T17:10:44Z")

</div>

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-](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
