gpujuliajulia-gpu

What does it mean to "finalize" in Julia?


I am currently working with the CUDArt package. The GitHub documentation includes the following snippet of code when loading a ptx module containing a custom CUDA C kernel:

md = CuModule("mycudamodule.ptx", false)  # false means it will not be automatically finalized

(comment in original)

I am trying to understand what exactly this false option for finalizing means and when I would / would not want to use it. I came across this post on SO (What is the right way to write a module finalize method in Julia?). It quotes from Julia documentation as:

finalizer(x, function)

Register a function f(x) to be called when there are no program-accessible references to x. The behavior of this function is unpredictable if x is of a bits type.

I don't really understand what this means though, or even whether the finalizing here is the same as that referred to in the CUDArt example. For example, it doesn't make sense to me to try to call a function on an argument x when that argument isn't accessible to the program - how could this even be possible? Thus, I would appreciate any help in clarifying:

  1. What it means to "finalize" in Julia and
  2. When I would/would not want to use it in the context of importing .ptx modules with CUDArt

Solution

  • I can't speak for CUDArt, but here is what finalize means in Julia: when the garbage collector detects that the program can no longer access the object, then it will run the finalizer, and then collect (free) the object. Note that the garbage collector can still access the object, even though the program cannot.

    Here is an example:

    julia> type X
               a
           end
    julia> j = X(1)  # create new X(1) object, accessible as j
    julia> finalizer(j, println)  # print the object when it's finalized
    julia> gc()      # suggest garbage collection; nothing happens
    julia> j = 0     # now the original object is no longer accessible by the program
    julia> gc()      # suggest carbage collection
    X(1)             # object was collected... and finalizer was run
    

    This is useful so that external resources (such as file handles or malloced memory) are freed if an object is collected.