# Memory allocation during assignment and modification of Float64 variables

**URL:** https://discourse.julialang.org/t/memory-allocation-during-assignment-and-modification-of-float64-variables/117856
**Category:** Performance
**Tags:** memory-allocation
**Created:** [August 5, 2024, 7:36pm UTC](https://discourse.julialang.org/t/memory-allocation-during-assignment-and-modification-of-float64-variables/117856 "2024-08-05T19:36:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Adam\_Mendl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_mendl/32/51897_2.png) [@Adam\_Mendl](https://discourse.julialang.org/u/Adam_Mendl)
#### Post date: [August 5, 2024, 7:36pm UTC](https://discourse.julialang.org/t/memory-allocation-during-assignment-and-modification-of-float64-variables/117856/1 "2024-08-05T19:36:39Z")

</div>

Hello, I have come across really puzzling (at least for me) output of `julia --track-allocation=user`. After playing with my code and outputs of benchmarking tools, I have ended up with the following snippet of `*.mem` file, which shows the main bottleneck in both the memory allocation and speed.

```julia
      160 values = hcubature([0.,0.,0.,0.,0.],[2*pi,pi,rlimit,2*pi,pi],norm=norm,rtol=rtol,atol=atol,maxevals=10000) do point
    45192 l = zero(Int64)
        0 integral1,err1 = quadgk(0.,point[3] - singularitybound,rtol=rtolinner,atol=atolinner) do x
    34164 l+=1
        0 value = one(Float64)
        0 value = value
        0 helper::Float64 = real(f(a,b,c,d,Z,point[1],point[2],point[3],point[4],point[5],x))
  2460372 value *= helper
        - # println(typeof(helper))
        - # println(typeof(value))
        0 helper2::Float64 = real(point[3]^2*sin(point[2])*x^2*sin(point[5]))
  2460372 value *= helper2
        - # println(typeof(value))
        0 value
        - end
        0 integral2,err2 = quadgk(point[3]+singularitybound,rlimit,rtol=rtolinner,atol=atolinner) do x
   480708 l+=1
  4363680 value = f(a,b,c,d,Z,point[1],point[2],point[3],point[4],point[5],x)*point[3]^2*sin(point[2])*x^2*sin(point[5])
        0 value
        - end
   366500 println("l: $(l)")
   120480 return integral1 + integral2
        - end

```

This code is part of a function and there are no global variables used. `f` is of type `Function` and variables `a`, `b`, `c` and `d` are of type `NTuple{3,Int64}`. The function `f` does not allocate anything.

Do you understand why is seems that Julia allocates memory when modifying variables of types `Int64` and `Float64`? This output just doesn’t seem right to me, but I am relatively new to Julia. I am almost sure that there is no type instability.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [August 5, 2024, 8:43pm UTC](https://discourse.julialang.org/t/memory-allocation-during-assignment-and-modification-of-float64-variables/117856/2 "2024-08-05T20:43:07Z")

</div>

This might be some artifact from compilation. Did you run the function once and then reset the counter as described in the docs below?  
[https://docs.julialang.org/en/v1/manual/profile/#Line-by-Line-Allocation-Tracking](https://docs.julialang.org/en/v1/manual/profile/#Line-by-Line-Allocation-Tracking)

Other than that there is the infamous case where [captured variables lead to type instabilities](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured) but this doesn’t seem to be the case here unless you have a variable named `value` outside of the snippet you showed.

---

<div class="post-metadata">

### Author: ![Adam\_Mendl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_mendl/32/51897_2.png) [@Adam\_Mendl](https://discourse.julialang.org/u/Adam_Mendl)
#### Post date: [August 6, 2024, 7:43am UTC](https://discourse.julialang.org/t/memory-allocation-during-assignment-and-modification-of-float64-variables/117856/3 "2024-08-06T07:43:42Z")

</div>

> [@abraemer](#):
>
> This might be some artifact from compilation. Did you run the function once and then reset the counter as described in the docs below?  
> [Profiling · The Julia Language](https://docs.julialang.org/en/v1/manual/profile/#Line-by-Line-Allocation-Tracking)

Ok, I was not doing that. After resetting the counter, assignment to the `value` variable is not allocating. However, modification of variable `l` still allocates even when using `let` statement

```julia
      928 values = hcubature([0.,0.,0.,0.,0.],[2*pi,pi,rlimit,2*pi,pi],norm=norm,rtol=rtol,atol=atol,maxevals=10000) do point
        - let l = zero(Int32)
        - quadgk(0.,point[3] - singularitybound,rtol=rtolinner,atol=atolinner) do x
  6403224 l+=1
        - value = one(Float64)
        - value = value
        0 helper::Float64 = real(f(a,b,c,d,Z,point[1],point[2],point[3],point[4],point[5],x))
        0 value =value* helper
        0 helper2::Float64 = real(point[3]^2*sin(point[2])*x^2*sin(point[5]))
        0 value =value* helper2
        0 value
        - end
        - end
        - return 0.
        - end

```

Maybe, I don’t understand how Julia works with closures, but I would expect that the line `let l = zero(Int32)` allocates memory on heap and closure references this part of memory and modifies it.

> [@abraemer](#):
>
> Other than that there is the infamous case where [captured variables lead to type instabilities](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured) but this doesn’t seem to be the case here unless you have a variable named `value` outside of the snippet you showed.

There is no other variable `value` nor `l` in my project outside this snippet.

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [August 6, 2024, 9:47am UTC](https://discourse.julialang.org/t/memory-allocation-during-assignment-and-modification-of-float64-variables/117856/4 "2024-08-06T09:47:04Z")

</div>

I do not understand what the `l` is good for. I assume it’s some kind of tally. However, it’s always risky to update captured variables in julia. You should rather use a `Ref`:

```julia
let l = Ref(zero(Int32))
...
    l[] += 1
...
end

```

You will get an allocation with the `Ref`. You can move it outside the call to `hcubature` and just zero it inside with `l[] = 0`.
