Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 293
Michael Ohlrogge Avatar asked Jul 10 '16 13:07

Michael Ohlrogge


People also ask

Why do we need finalize?

The purpose of a finalize() method can be overridden for an object to include the cleanup code or to dispose of the system resources that can be done before the object is garbage collected. If we are overriding the finalize() method then it's our responsibility to call the finalize() method explicitly.

How many times can finalize method is called?

It is invoked only once during the execution of a program. Following are some notable points about the finalize method. Since this method belongs the Object class, which is the super class of all the classes in java, you can override it from any class.


1 Answers

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.

like image 70
Fengyang Wang Avatar answered Oct 04 '22 01:10

Fengyang Wang