On Thu, Oct 22, 2015 at 9:42 AM,  <[email protected]> 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)

> 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
>

Reply via email to