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. <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. </div><div= ><br></div><div>Keep thinking good thoughts. <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 <242050= [email protected]> 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> (defclass garbage-counter () ((n :initarg :n)))</span>= <br><span></span><br><span> (defmethod initialize-instance :after</spa= n><br><span> ((x garbage-counter) &rest in= itargs)</span><br><span> (declare (ignore initargs))</span= ><br><span> (ccl:terminate-when-unreachable x))</span><br>= <span></span><br><span> (let ((s *terminal-io*))</span><br><span> &nbs= p; (defmethod ccl:terminate ((x garbage-counter))</span><br><span= > (let ((n (1+ (slot-value x 'n))))</span><br>= <span> (make-instance 'garbage-cou= nter :n n)</span><br><span> (forma= t s "~&garbage-counter: ~s~%" n)</span><br><span> &nbs= p; (finish-output s))))</span><br><span></span><br><span>Us= age example:</span><br><span></span><br><span> ? (progn (make-instance= 'garbage-counter :n 0) t)</span><br><span> T</span><br><span> ?= (ccl:gc)</span><br><span> NIL</span><br><span> ?</span><br><spa= n> garbage-counter: 1</span><br><span> (ccl:gc)</span><br><span>= NIL</span><br><span> ?</span><br><span> 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><transcript.= txt></div></div></blockquote></div></div></div></body></html>= --Apple-Mail-B1A42935-4DCF-432F-BB57-15784267BCE4--