Subject: Re: Interned Symbols and GC
From: Erik Naggum <erik@naggum.no>
Date: 2000/02/08
Newsgroups: comp.lang.lisp
Message-ID: <3159028659830535@naggum.no>

* Christian Lebiere <cl@andrew.cmu.edu>
| For better or worse, our application generates lots of interned symbols.

  well, if you don't use the Lisp reader to find symbols by name, there are
  always a number of available solutions better than using interned
  symbols.  if, say, you only use symbols as unique keys, but whose name
  does not actually matter, a fresh cons cell like (nil . nil) may be used
  as long as you keep the key around someplace else.  if the name matters,
  you can also use a string.

| MCL seems to garbage-collect interned symbols after a while when they are
| no longer referenced (which hasn't caused a problem so far), while
| Allegro CL seems to keep them around forever (which causes it to bog down
| and crash if given enough time).
| 
| I have a couple of questions:
| 
| 0) Is the above description accurate?

  no.  an interned symbol is ipso facto referenced -- by the very package
  in which it is interned!

  moreover, whether a symbol is "useless" or not is not determined by what
  references the symbol, but by what the symbol references -- quite the
  opposite of your concern.  the whole point with an interned symbol is
  that it holds onto some values and that there might be a reference to
  those values in the future through the symbol by its name read from an
  outside source, so _clearly_ we can't chuck interned symbols -- it would
  defeat the fundamental purpose of symbols.  (aggressive short-sightedness
  is required to label this fact "unfortunate", as some evidently do.)
  
| 1) What is the correct behavior, if there is one (CLTL2 has little to say)?

  since MCL is clearly in the wrong as you have described it, I'm inclined
  to think that something else is at work.

  btw, CLtL2 is no longer _the_ language reference.  use the HyperSpec (or
  the standard).
 
| 2) Is there a way to get rid of interned symbols that are no longer
| needed that is safer and more efficient than to explicitely unintern them
| individually?

  DELETE-PACKAGE gets rid of the last reference to interned symbols that
  have no references, for all symbols in that package.   you could always
  copy the symbols you need to a new package, and do your own copying
  garbage collection that way.

  iterators over symbols are allowed to unintern the symbol it looks at.
  you may want to use DO-SYMBOLS over the symbols in a package to get rid
  of the ones you don't need.


  it sounds as if what you _really_ want is a weak hash table that you
  index with _some_ unique key, as outlined at the start of this message.

#:Erik