Yes, s+=i just calls the + function for bignums, which indeed creates a new
BigNum object each iteration.

[It would incidentally be great if we could overload += directly in Julia!!
I wouldn't mind if it was required to return an immutable object because I
could still return a new immutable object wrapping the old internals after
they have been updated. But this is an aside and not related to the issue
here.]

Note in particular that my mpn example creates a new Array every iteration
and yet it doesn't show the same behaviour.

The difference is surely either something to do with finalizers or to do
with the difference between an allocation Julia merely counts and an
allocation Julia actually garbage collects.

It's not limited to BigInt's anyway. All of our types in Nemo operate the
same way as GMP bignums. So this issue affects every single one of the
types coming from C libraries that we interface to. It seems to affect any
C objects that do allocations of their own, even when we go to the trouble
of informing Julia of these allocations via use of jl_gc_counted_malloc
instead of malloc.

To understand what is causing the problem, we need to understand the Julia
garbage collector better.

Bill.


On 15 March 2016 at 17:41, 'Tobias Knopp' via julia-users <
[email protected]> wrote:

> Seems to be an issue specific for BigNum. I would expect in the line
>   s += i
> new BigNum objects are created (and in turn allocated) and in turn the gc
> will kick in from time to time and run the finalizers of the BigNum objects
> that are not rooted anymore.
>
>
>
> Am Dienstag, 15. März 2016 17:20:37 UTC+1 schrieb Bill Hart:
>>
>> Just to clarify what I meant by "Julia can't be responsible for...." I
>> was talking about the fact that the garbage collector does a gc_collect
>> every 23mb on my machine. If memory is allocated on the C side without
>> calling jl_gc_counted_malloc to do so, Julia can't be expected to include
>> these allocations in its count as it tries to work out if 23mb have been
>> allocated yet (and hence a gc_collect should be initiated).
>>
>> So I just meant Julia can't be expected to know about the allocations
>> unless we inform it. We of course remain responsible for cleaning it up.
>>
>

Reply via email to