Julia object Destructor or module Unload routine

365 views
Skip to first unread message

rafz...@gmail.com

unread,
Oct 22, 2015, 10:23:00 AM10/22/15
to julia-users
Hi Julia-Users
I know that Julia has a Garbage Collector that is being aware of unusable memory pointers. [ref] I think some other languages e.g. Java also do it automatically, and Java programmers rarely need to do it manually [ref], while writing such routines is very common in C++ [ref]. 
Is it possible to have something like Java finalize or C++ ~ in Julia?
This routine may be called 
   - by Julia REPL when a kill signal (Ctrl+D) is sent.
   - or workspace() is called.
   - or by Garbage Collector before removing a pointer.
type mytype
  mytype
()=new()
 
function ~mytype()
     close
(file)
     free
(memory)
     kill
(externals)
     
delete(temps)
     gc
()
 
end
  file
::File
  memory
::Pointer
  externals
::Any
  temps
::File
end
module mymodule
 
function unload()
     close
(file)
     free
(memory)
     kill
(externals)
     
delete(temps)
     gc
()
 
end
  file
::File
  memory
::Pointer
  externals
::Any
  temps
::File
end

Yichao Yu

unread,
Oct 22, 2015, 10:48:31 AM10/22/15
to Julia Users
On Thu, Oct 22, 2015 at 9:42 AM, <rafz...@gmail.com> wrote:
> Hi Julia-Users
> I know that Julia has a Garbage Collector that is being aware of unusable
> memory pointers. [ref] I think some other languages e.g. Java also do it
> automatically, and Java programmers rarely need to do it manually [ref],
> while writing such routines is very common in C++ [ref].
> Is it possible to have something like Java finalize or C++ ~ in Julia?
> This routine may be called
> - by Julia REPL when a kill signal (Ctrl+D) is sent.
> - or workspace() is called.
> - or by Garbage Collector before removing a pointer.

We do have finalizer
However, there's a few things to note.
* The finalizer is run by the GC. Don't call `gc()` in it (it will be
no-op). (Actually, don't ever call gc() unless you are absolutely sure
you know what you are doing and you need it)
* We will never have garentee on when, how or in which order the
finalizers are called, which I think is common in tracing GCs.
* Do not use the finalizer to manage resources you care about. The GC
doesn't (yet) have any notion of resources pressure other than the GC
memory usage so it may not collect your resources at the time you
want. You should explicitly free those resources (the `do` block,
try/finally, wrap one of these in macro etc)

Stefan Karpinski

unread,
Oct 22, 2015, 10:51:05 AM10/22/15
to Julia Users
There's also atexit:

  atexit(f)

  Register a zero-argument function f() to be called at process exit. atexit() hooks
  are called in last in first out (LIFO) order and run before object finalizers.

rafz...@gmail.com

unread,
Oct 22, 2015, 12:08:42 PM10/22/15
to julia-users
Thanks for response, I just forget `finalizer`, also thank for notify me the GC behavior.  

rafz...@gmail.com

unread,
Oct 22, 2015, 12:33:45 PM10/22/15
to julia-users
Interesting, although, I don't know how to do such hooking ... 
Reply all
Reply to author
Forward
0 new messages