# Understanding finalizers

**URL:** <https://discourse.julialang.org/t/understanding-finalizers/4201>\
**Category:** General Usage\
**Created:** [June 10, 2017, 8:22pm UTC](https://discourse.julialang.org/t/understanding-finalizers/4201 "2017-06-10T20:22:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![LewisHein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewishein/32/5690_2.png) [@LewisHein](https://discourse.julialang.org/u/LewisHein)\
**Post date:** [June 10, 2017, 8:22pm UTC](https://discourse.julialang.org/t/understanding-finalizers/4201/1 "2017-06-10T20:22:53Z")

</div>

Hi all,

I am trying to understand how Julia decides when to call finalizers. I am using MathGL.jl for some plotting, and Julia doesn’t run the required finalizers for MGLGraphs until the end of the script, which makes it run out of memory and be killed when the amount of work becomes non-trivial.

The MathGL wrapper is huge, so I have a toy example below:

```julia
# File: memhungry.jl
type MemHungry
bite::Array{Int, 1}
end

function MemHungry(bitesize::Int)
    info("A big bite of memory is now gone...")
    m = MemHungry(Array{Int, 1}(bitesize))
    finalizer(m, memregurgitate)

    return m
end

function memregurgitate(m::MemHungry)
    info("Wow, those blocks of memory sure tasted bad!")
    info("Regurgitating.... Please wait.")
    sleep(1)
    info("Regurgitated $(length(m.bite)*8) bytes")
end

import Base.show
function show(io::IO, m::MemHungry)
    for i in 1:length(m.bite)
        info("Crunch, crunch crrunch!")
    end
end

function munchmem(mealsize::Int)
    m = MemHungry(mealsize)
    info(m)
# finalize(m)
end

for i in 1:3
    munchmem(i)
end

```

`julia memhungry.jl` results in

```julia
INFO: A big bite of memory is now gone...
INFO: Crunch, crunch crrunch!
INFO: 
INFO: A big bite of memory is now gone...
INFO: Crunch, crunch crrunch!
INFO: Crunch, crunch crrunch!
INFO: 
INFO: A big bite of memory is now gone...
INFO: Crunch, crunch crrunch!
INFO: Crunch, crunch crrunch!
INFO: Crunch, crunch crrunch!
INFO: 
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF

```

If I uncomment the call to `finalize(m)` in `munchmem`, the output is

```julia
INFO: A big bite of memory is now gone...
INFO: MemHungry([0])
INFO: Wow, those blocks of memory sure tasted bad!
INFO: Regurgitating.... Please wait.
INFO: Regurgitated 8 bytes
INFO: A big bite of memory is now gone...
INFO: MemHungry([0,0])
INFO: Wow, those blocks of memory sure tasted bad!
INFO: Regurgitating.... Please wait.
INFO: Regurgitated 16 bytes
INFO: A big bite of memory is now gone...
INFO: MemHungry([0,0,0])
INFO: Wow, those blocks of memory sure tasted bad!
INFO: Regurgitating.... Please wait.
INFO: Regurgitated 24 bytes

```

So my question: Why does Julia have to be prodded like this to finalize things, even when (as with my plotting) memory is running low? Is there a way to avoid the prodding?

Thanks,

Lewis
