Re: Garbage collector - ccl:terminate (memory stuff)

Tim McNerney <[email protected]> Mon, 20 May 2024 09:10:10 -0400
Newsgroups gmane.lisp.openmcl.devel
Message-ID <[email protected]>
--Apple-Mail-B1A42935-4DCF-432F-BB57-15784267BCE4
Content-Type: text/plain;
	charset=utf-8
Content-Transfer-Encoding: quoted-printable

It is interesting to contemplate using a terminate method to create a (mytho=
logically reincarnated) =E2=80=9Cbaby Phoenix=E2=80=9D object that never has=
 any pointers to it, thus guaranteed it will =E2=80=9Cburn=E2=80=9D (get rec=
laimed) upon the very next GC.=20

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

Curious=E2=80=A6 what does Common Lisp in general have to say about the GC c=
alling terminate methods? I must confess that while intriguing, I haven=E2=80=
=99t thought about this corner of storage management engineering since the 8=
0=E2=80=99s.=20

Keep thinking good thoughts.=20

--Tim

> On May 20, 2024, at 05:19, Robert Munyer <[email protected]> wrote:
>=20
> =EF=BB=BFWhile 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.
>=20
> Code:
>=20
>  (defclass garbage-counter () ((n :initarg :n)))
>=20
>  (defmethod initialize-instance :after
>      ((x garbage-counter) &rest initargs)
>    (declare (ignore initargs))
>    (ccl:terminate-when-unreachable x))
>=20
>  (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))))
>=20
> Usage example:
>=20
>  ? (progn (make-instance 'garbage-counter :n 0) t)
>  T
>  ? (ccl:gc)
>  NIL
>  ?
>  garbage-counter: 1
>  (ccl:gc)
>  NIL
>  ?
>  garbage-counter: 2
>=20
> 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.
>=20
> <transcript.txt>

--Apple-Mail-B1A42935-4DCF-432F-BB57-15784267BCE4
Content-Type: text/html;
	charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=3D=
utf-8"></head><body dir=3D"auto">It is interesting to contemplate using a te=
rminate method to create a (mythologically reincarnated) =E2=80=9Cbaby Phoen=
ix=E2=80=9D object that never has any pointers to it, thus guaranteed it wil=
l =E2=80=9Cburn=E2=80=9D (get reclaimed) upon the very next GC.&nbsp;<div><b=
r></div><div>It is unlikely that this use case has been well tested. More to=
 the point: it is unwise, if not explicitly disallowed, to <i>cons</i> in a t=
erminate method. When the GC is running, one must assume to be in a situatio=
n where there is <i>zero</i> available memory. (This prudent restriction is m=
ore likely to be a stumbling block when inserting debugging statements in me=
mory management code.)<div><br></div><div>Curious=E2=80=A6 what does Common L=
isp <i>in general</i> have to say about the GC calling terminate methods? I m=
ust confess that while intriguing, I haven=E2=80=99t thought about this corn=
er of storage management engineering since the 80=E2=80=99s.&nbsp;</div><div=
><br></div><div>Keep thinking good thoughts.&nbsp;<br><div><br id=3D"lineBre=
akAtBeginningOfSignature"><div dir=3D"ltr">--Tim</div><div dir=3D"ltr"><br><=
blockquote type=3D"cite">On May 20, 2024, at 05:19, Robert Munyer &lt;242050=
[email protected]&gt; wrote:<br><br></blockquote></div><blockquote type=3D"cit=
e"><div dir=3D"ltr">=EF=BB=BF<span>While I was waking up, I had a strange th=
ought (possibly left</span><br><span>over from a dream) about a way to imple=
ment a counter that lives</span><br><span>in "unreachable" heapspace, and is=
 repeatedly destroyed by the</span><br><span>garbage collector, but continue=
s counting anyway.</span><br><span></span><br><span>Code:</span><br><span></=
span><br><span> &nbsp;(defclass garbage-counter () ((n :initarg :n)))</span>=
<br><span></span><br><span> &nbsp;(defmethod initialize-instance :after</spa=
n><br><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((x garbage-counter) &amp;rest in=
itargs)</span><br><span> &nbsp;&nbsp;&nbsp;(declare (ignore initargs))</span=
><br><span> &nbsp;&nbsp;&nbsp;(ccl:terminate-when-unreachable x))</span><br>=
<span></span><br><span> &nbsp;(let ((s *terminal-io*))</span><br><span> &nbs=
p;&nbsp;&nbsp;(defmethod ccl:terminate ((x garbage-counter))</span><br><span=
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((n (1+ (slot-value x 'n))))</span><br>=
<span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(make-instance 'garbage-cou=
nter :n n)</span><br><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(forma=
t s "~&amp;garbage-counter: ~s~%" n)</span><br><span> &nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;(finish-output s))))</span><br><span></span><br><span>Us=
age example:</span><br><span></span><br><span> &nbsp;? (progn (make-instance=
 'garbage-counter :n 0) t)</span><br><span> &nbsp;T</span><br><span> &nbsp;?=
 (ccl:gc)</span><br><span> &nbsp;NIL</span><br><span> &nbsp;?</span><br><spa=
n> &nbsp;garbage-counter: 1</span><br><span> &nbsp;(ccl:gc)</span><br><span>=
 &nbsp;NIL</span><br><span> &nbsp;?</span><br><span> &nbsp;garbage-counter: 2=
</span><br><span></span><br><span>Greg, I had no difficulty getting this to w=
ork in FriCAS's "BOOT"</span><br><span>package; it worked on the first try. &=
nbsp;I'll attach a transcript,</span><br><span>in case seeing exactly what I=
 did might be useful to you.</span><br><span></span><br><div>&lt;transcript.=
txt&gt;</div></div></blockquote></div></div></div></body></html>=

--Apple-Mail-B1A42935-4DCF-432F-BB57-15784267BCE4--