Subject: Re: Memory Allocation in Lisp?
From: rpw3@rpw3.org (Rob Warnock)
Date: Sat, 10 Jul 2004 03:20:24 -0500
Newsgroups: comp.lang.lisp
Message-ID: <d5mdnSYzpq1VO3Ld4p2dnA@speakeasy.net>
neo88 <solo88@truevine.net> wrote:
+---------------
| rpw3@rpw3.org (Rob Warnock) wrote in message
| >     > (defmethod make-cleanup-thunk ((thing string))
| > 	(lambda ()
| > 	  (format t "The ~s has been cleaned up!~%" thing)))
| >     #<Standard-Method MAKE-CLEANUP-THUNK (STRING) {484C68A5}>
| >     > (let* ((x "Thing needing cleaning")
| > 	     (y (list x)))
| > 	(finalize y (make-cleanup-thunk x))
| > 	'done)
| >     DONE
| >     > (gc)
| >  NIL
| >     > (gc)
| >     The "Thing needing cleaning" has been cleaned up!
| >     NIL
| >     >
|
| So, finialze and make-cleanup-thunk both call the GC?
+---------------

Not sure what you mean by "call the GC". FINALIZE certainly leaves
some information *around* that the GC will use the next time it runs
(indeed, *every* time it runs until the finalized object is garbage),
but it certainly doesn't "call" the GC nor cause it to run.

And MAKE-CLEANUP-THUNK doesn't "call" the GC nor cause it to run, either.
It just does whatever to THING your application needed it to do when Y
became garbage.

+---------------
| Is there any other way to call the GC that I should know about?
+---------------

Rather than talking about "calling" the GC, it's probably better to
talk about under what circumstances the GC will run, basically for
only two or three reasons (depending on the style of GC): (1) When
the procedure named "GC" is called; (2) When an allocation is attempted
for which there is insufficient ready free memory available; (3) When
an allocation is attempted which causes some programmed trigger to
fire. As an example of the latter, CMUCL will run the GC whenever
EXT:*BYTES-CONSED-BETWEEN-GCS* bytes of memory have been allocated
since the last GC (default in CMUCL-18e is 12MB).

Except for debugging, tutorials (as above), and a few special cases
[e.g., preparing to dump a memory image], the procedure named "GC"
is never called in normal use, so reasons #2 & #3 are the main ones.


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607