[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: destructing objects

>	Here is my first question for this list, mind my ignorance!  I
>have just started tooling around with MCL 2.0xxx and more specifically
>CLOS.  Is there a way to destruct a object instanciated by
>	My conceptualization of how memory is managed in lisp is not
>the greatest.  If I set bind some symbol to some value (set a 3) then
>say (setf a 4) what happens to the memory allocated for the 3?  Is it
>overwritten or at garbace collection time is ir reclaimed?
>Any information on literature regarding CLOS ould also be very helpful
>to me.  I take that back any literature or source code at a learning
>level would be appreciated!
>Thanks in advance,
>John Louch

Well, welcome to the wonderful world of lisp! Asking how to destroy an
object in Lisp is like asking how to set the dip switches and jumper
pins when you install a new video card into your mac - hey, it just 
takes care of itself! 

I know it can be a bit unnerving, especially if you're used to 
devoting most of your attention to memory  management problems.
But don't worry, be happy; spend your copious free time worrying 
about the other aspects of your program. That's what high-level 
languages are for.

Now, for a more serious answer: Objects are eligible for collection
when there are no longer any references to them.  For example,
let's say you make an object, and then wipe out the only reference
in the world to it:

  (defclass pizza () ((toppings :initarg :toppings)))

  (setf a (make-instance 'pizza :toppings '(pepperoni)))   
  (setf a (make-instance 'pizza :toppings '(cheese mushroom)))

The pepperoni pizza becomes orphaned when you make the cheese-'shroom
one. Since nobody points to it, sooner or later the garbage collector 
will come around and reclaim it. 

How does the GC know when something is ready to be collected? 
And WHEN does it come along and make a pickup? The short answer is 
that garbage is collected in a background task in as unobtrusive a 
way as possible, according to an accounting scheme that tries to 
be thorough about finding everything that isn't referenced by 
something else.

As for books, several good ones on Lisp and CLOS are listed in
Appendix F of the MCL manual.