[Openmcl-devel] Garbage collector - ccl:terminate (memory stuff)

Tim McNerney mc at media.mit.edu
Mon May 20 06:10:10 PDT 2024


It is interesting to contemplate using a terminate method to create a (mythologically reincarnated) “baby Phoenix” object that never has any pointers to it, thus guaranteed it will “burn” (get reclaimed) upon the very next GC. 

It is unlikely that this use case has been well tested. More to the point: it is unwise, if not explicitly disallowed, to cons in a terminate method. When the GC is running, one must assume to be in a situation where there is zero available memory. (This prudent restriction is more likely to be a stumbling block when inserting debugging statements in memory management code.)

Curious… what does Common Lisp in general have to say about the GC calling terminate methods? I must confess that while intriguing, I haven’t thought about this corner of storage management engineering since the 80’s. 

Keep thinking good thoughts. 

--Tim

> On May 20, 2024, at 05:19, Robert Munyer <2420506348 at munyer.com> wrote:
> 
> While I was waking up, I had a strange thought (possibly left
> over from a dream) about a way to implement a counter that lives
> in "unreachable" heapspace, and is repeatedly destroyed by the
> garbage collector, but continues counting anyway.
> 
> Code:
> 
>  (defclass garbage-counter () ((n :initarg :n)))
> 
>  (defmethod initialize-instance :after
>      ((x garbage-counter) &rest initargs)
>    (declare (ignore initargs))
>    (ccl:terminate-when-unreachable x))
> 
>  (let ((s *terminal-io*))
>    (defmethod ccl:terminate ((x garbage-counter))
>      (let ((n (1+ (slot-value x 'n))))
>        (make-instance 'garbage-counter :n n)
>        (format s "~&garbage-counter: ~s~%" n)
>        (finish-output s))))
> 
> Usage example:
> 
>  ? (progn (make-instance 'garbage-counter :n 0) t)
>  T
>  ? (ccl:gc)
>  NIL
>  ?
>  garbage-counter: 1
>  (ccl:gc)
>  NIL
>  ?
>  garbage-counter: 2
> 
> Greg, I had no difficulty getting this to work in FriCAS's "BOOT"
> package; it worked on the first try.  I'll attach a transcript,
> in case seeing exactly what I did might be useful to you.
> 
> <transcript.txt>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20240520/fc6fc175/attachment.htm>


More information about the Openmcl-devel mailing list