Open vchuravy opened 1 week ago
I am tempted to add a deprecation for finalizers attached to Array, similar to other immutable objects that disallow finalizers
Ref https://github.com/JuliaLang/julia/issues/54128#issuecomment-2069846970 where the map stdlib hit this.
It seems like using a finalizer on a memory object to free memory causes the GC to perform significantly worse compared to registering the finalizer directly on the Array - both in 1.10 where this would still be the correct approach and in julia 1.11 where this could cause segfaults.
I've tested this somewhat crudely by setting up a loop that repeatedly allocates memory through a external C-Library (with 10^8 iterations), attaching the free
method of that library to the finalizer, and then watching the memory consumption. (Ref the discussion on cesmix-mit/LAMMPS.jl#51 for further details)
when attaching the finalizer on the array directly, my memory consumption looks like this (both in 1.10 and 1.11):
When registering the finalizer on the memory obect:
and I had to call GC.gc()
twice to fully free the allocated memory:
I'm running on Windows 10 with an AMD prozessor
This probably needs documentation. I just came across someone who wrote the following code (paraphrased):
Before the change to Memory the lifetime of the memory buffer was tight to Array so the finalizer there had the intended effect. Now with Memory we are attaching the finalizer to the "wrong" object. I did not see this documented as a change, but it probably ought to be.