jw3126 / Setfield.jl

Update deeply nested immutable structs.
Other
167 stars 17 forks source link

Garbage collection for objects created with `@set` #167

Closed kangboli closed 2 years ago

kangboli commented 2 years ago

I'm having difficulty releasing the memory of objects created with @set.

For the following code, both arrays are expected to be garbage collected, but I always end up with one copy left in my memory if I htop my memory usage.

a = rand(10000, 10000)
b = @set a[1] = 1
a = nothing
b = nothing
GC.gc(true)

Any thoughts? Thanks.

jw3126 commented 2 years ago

Note that julia does not automatically give memory back to the os after gc. So htop can't tell you wether a particular object gets collected or not. What you probably can do is look at the raw pointer of a + b and allocate new arrays and see if space filled up by them gets populated by pointers of new arrays.

jw3126 commented 2 years ago
using Setfield

memrange(x) = UInt(pointer(x)):UInt(pointer(x)+(length(x)-1)*sizeof(eltype(x)))

n = 10
rs =[]
for i in 1:n
    a = rand(10000, 10000)
    b = @set a[1] = 1
    push!(rs, memrange(a))
    push!(rs, memrange(b))
    a = nothing
    b = nothing
    GC.gc(true)
end
# for nicer printing
offset = minimum(first, rs)
rs = map(rs) do r
    Int(first(r) - offset):Int(last(r) - offset)
end
display(rs)
20-element Vector{UnitRange{Int64}}:
 816787456:1616787448
 16785408:816785400
 800002048:1600002040
 0:799999992
 800002048:1600002040
 0:799999992
 800002048:1600002040
 0:799999992
 800002048:1600002040
 0:799999992
 800002048:1600002040
 0:799999992
 800002048:1600002040
 0:799999992
 800002048:1600002040
 0:799999992
 800002048:1600002040
 0:799999992
 800002048:1600002040
 0:799999992

So on my machine memory gets always reused in the next iteration.

kangboli commented 2 years ago

Thanks a lot for explaining even though this doesn't seem to be an issue with Setfield.

I suppose I should be using the profiler instead to check memory usage, although it would be nice to monitor things on the go.

jw3126 commented 2 years ago

I suppose I should be using the profiler instead to check memory usage, although it would be nice to monitor things on the go.

Yeah, I find debugging memory issues in julia pretty tricky. It would be really nice if there was better tooling.