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

Grégory Vanuxem g.vanuxem at gmail.com
Sat May 4 19:55:20 PDT 2024


> Thanks. I think I get the basic idea: There is hybrid language FFI memory
management situation going on, and the terminate method for the non-Lisp
language is not being called when you wish it were.

I have to let Julia know it can reclaim memory about no longer referenced
variables. And I want to use the CCL GC to let me know when it is GCing the
memory related to some no longer bound variables. But it is the CCL:
terminate method that allows that. There are some other CL implementations,
at least SBCL, where I can give it directy a lambda form in such a way it
does my job:

            (let ((ret (make-instance 'jlref :id id
                    :type (|jl_string_eval_string|
                        (concatenate 'string "string(typeof(getindex(refs,\"
" id "\")))")))))
                    #+:sbcl (sb-ext:finalize ret (lambda ()
                        (sb-concurrency:enqueue index *jqueue*))) ret)


Here is a sample session with SBCL,
)juliad is a system command that display the output of the command executed
in Julia
)julia execute a command in Julia,
)lisp same thing but in CL subsystem
refs the indexed dictionary in Julia
*jqueue* a FIFO queue populated by the CL terminate command. Since Julia
does not support multiple thread access through its library libjulia.so it
is up to the main FriCAS (CL) thread to delete entries in Julia 'refs', so
on user input,I check if some entries can be deleted in Julia, if so, I
delete them. Notice, there is a code helper here, a printf in the C
wrapper.

FriCAS Computer Algebra System
Version: FriCAS 1.3.11-dev built with SBCL 2.4.4
    Timestamp: sam. 04 mai 2024 22:03:28 CEST
-----------------------------------------------------------------------------
Issue )copyright to view copyright notices.
Issue )summary for a summary of useful system commands.
Issue )quit to leave FriCAS and return to shell.
-----------------------------------------------------------------------------


(1) -> )juliad refs
IdDict{String, Any}()
(1) -> a:=matrix([[urand01(),urand01()],[urand01(),urand01()]])$JMATRIX
(JCFLOAT)

2×2 Matrix{Complex{BigFloat}}:
0.202264+0.215084im  0.837934+0.841468im
0.881676+0.53561im   0.643403+0.736287im
(1)  Julia Output
                           Type: JuliaMatrix(JuliaComplexFloat)
             Time: 0.05 (IN) + 1.72 (EV) + 2.48 (OT) = 4.26 sec <- include
the time taken to load the Julia library
(2) -> )julia display(refs)
IdDict{String, Any} with 7 entries:
"2873853940934582137" => 0.837934+0.841468im
"3013752598065164257" => 0.643403+0.736287im
"1799302827895858725" => 0.0+0.0im
"1193941380623146742" => 0.202264+0.215084im
"525699342094945562" => Complex{BigFloat}[0.202264+0.215084im 0.837934+
0.841468im; 0.881676+0.53561im 0.643403+0.736287im]
"2441597211547797803" => 1.0+0.0im
"2342493223442167775" => 0.881676+0.53561im

(2) -> jlRef a

(2)  #<JLREF  Matrix{Complex{BigFloat}} 525699342094945562 {100236FE33}>
                                              Type: SExpression
                                     Time: 0.03 (OT) = 0.03 sec
(3) -> )lisp *jqueue*

Value = #S(SB-CONCURRENCY:QUEUE
:HEAD #1=(SB-CONCURRENCY::.DUMMY.)
:TAIL #1#
:NAME "JuliaQueue")
(3) -> )clear all
All user variables and function definitions have been cleared.
(1) -> a

(1)  a
                                              Type: Variable(a)
                                                    Time: 0 sec
(2) -> )lisp *jqueue*

Value = #S(SB-CONCURRENCY:QUEUE
:HEAD #1=(SB-CONCURRENCY::.DUMMY.)
:TAIL #1#
:NAME "JuliaQueue")
(2) -> )lisp (reclaim)

Value = NIL
(2) -> )lisp *jqueue*

Value = #S(SB-CONCURRENCY:QUEUE
:HEAD (SB-CONCURRENCY::.DUMMY. "525699342094945562"
    "3013752598065164257" "1193941380623146742"
    "2873853940934582137" . #1=("2342493223442167775"))
:TAIL #1#
:NAME "JuliaQueue")
(2) -> a
Freeing... 525699342094945562
525699342094945562 freed
Freeing... 3013752598065164257
3013752598065164257 freed
Freeing... 1193941380623146742
1193941380623146742 freed
Freeing... 2873853940934582137
2873853940934582137 freed
Freeing... 2342493223442167775
2342493223442167775 freed

(2)  a
                                              Type: Variable(a)
                                     Time: 0.02 (IN) = 0.02 sec
(3) -> )julia display(refs)
IdDict{String, Any} with 2 entries:
"1799302827895858725" => 0.0+0.0im
"2441597211547797803" => 1.0+0.0im

(3) ->
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20240505/ca0fadca/attachment.htm>


More information about the Openmcl-devel mailing list