# Unexpected memory cost with Julia

**URL:** <https://discourse.julialang.org/t/unexpected-memory-cost-with-julia/84295>\
**Category:** General Usage\
**Tags:** question, memory\
**Created:** [July 16, 2022, 2:29am UTC](https://discourse.julialang.org/t/unexpected-memory-cost-with-julia/84295 "2022-07-16T02:29:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [July 16, 2022, 2:29am UTC](https://discourse.julialang.org/t/unexpected-memory-cost-with-julia/84295/1 "2022-07-16T02:29:25Z")

</div>

Recently I found my code had an unexpected memory cost while working with Julia.

See the following example:

```julia
using OMEinsum

    len = 20
    tensor1 = rand(len,len,len,len)
    tensor2 = rand(len,len,len)
    tensor3 = rand(len,len)

    @time @ein tensorA[a,l,b,k,j] := tensor1[i, j, k, l] * tensor2[a,b,i]
    @time @ein tensorB[b, d, k] := tensor3[a, b] * tensor2[a, d, k]

    @time @ein tensorA[a,l,b,k,j] := tensor1[i, j, k, l] * tensor2[a,b,i]
    @time @ein tensorB[b, d, k] := tensor3[a, b] * tensor2[a, d, k]

```

output:

```julia
  2.807763 seconds (9.26 M allocations: 526.600 MiB, 9.24% gc time, 98.71% compilation time)
  0.377838 seconds (1.04 M allocations: 52.680 MiB, 2.59% gc time, 99.94% compilation time)
  0.034800 seconds (125 allocations: 50.117 MiB, 39.26% gc time)
  0.000097 seconds (78 allocations: 70.391 KiB)

```

The actual cost of the operation in my example would be:

```julia
 using OMEinsum
using BenchmarkTools

    len = 20
    tensor1 = rand(len,len,len,len)
    tensor2 = rand(len,len,len)
    tensor3 = rand(len,len)

    @btime @ein tensorA[a,l,b,k,j] := tensor1[i, j, k, l] * tensor2[a,b,i]
    @btime @ein tensorB[b, d, k] := tensor3[a, b] * tensor2[a, d, k]

```

output:

```julia
  15.992 ms (126 allocations: 50.12 MiB)
  16.452 μs (78 allocations: 70.39 KiB)

```

I knew Julia would actually be a little slower when called the function in the package the first time, but I didn’t think it would cost 10 times more memory than expected, sometimes causing outofMemory errors.

I guess it has something to do with Julia precompilation and not the OMEinsum package. But I don’t quite understand, can someone give me some guidance on how to reduce memory overhead?

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [July 16, 2022, 3:46am UTC](https://discourse.julialang.org/t/unexpected-memory-cost-with-julia/84295/2 "2022-07-16T03:46:34Z")

</div>

I find an even more outrageous example:

```julia
using Integrals
using SciMLSensitivity
using SphericalHarmonics
using FastTransforms
using BenchmarkTools
    y=0.5
    @time solve(IntegralProblem((x,y)->SphericalHarmonics.sphericalharmonic(x, 0, 0,0)*exp(y*cos(x)),0, pi,y),QuadGKJL(),abstol=1e-16)[1]

```

This output:

```julia
14.107230 seconds (55.32 M allocations: 4.008 GiB, 6.54% gc time, 99.97% compilation time)

```

And then run:

```julia
using Integrals
using SciMLSensitivity
using SphericalHarmonics
using FastTransforms
using BenchmarkTools
    y=0.5

    @btime solve(IntegralProblem((x,y)->SphericalHarmonics.sphericalharmonic(x, 0, 0,0)*exp(y*cos(x)),0, pi,y),QuadGKJL(),abstol=1e-16)[1]

```

The output is:

```julia
9.404 μs (113 allocations: 4.19 KiB)

```

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [July 16, 2022, 4:53am UTC](https://discourse.julialang.org/t/unexpected-memory-cost-with-julia/84295/3 "2022-07-16T04:53:40Z")

</div>

Are you actually running out of memory during compilation? What’s the peak memory usage of the Julia process?

What you posted doesn’t seem totally out of line for compiling what may be some complex code. It might take a similar amount of time for an ahead-of-time compiler. Keep in mind that the allocated memory amount reported is the sum of all allocations. In general, memory will be freed many times by the garbage collector throughout that period, so it should not be construed with memory usage at a single point in time or peak usage.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 16, 2022, 6:21am UTC](https://discourse.julialang.org/t/unexpected-memory-cost-with-julia/84295/4 "2022-07-16T06:21:54Z")

</div>

Julia has to compile your code to native code at some point. Julia is not an interpreted language.

If you would like to compile to native code ahead of time, I suggest looking into building a new system image:

> **[GitHub - JuliaLang/PackageCompiler.jl: Compile your Julia Package](https://github.com/JuliaLang/PackageCompiler.jl)**
>
> Compile your Julia Package. Contribute to JuliaLang/PackageCompiler.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [July 16, 2022, 7:05am UTC](https://discourse.julialang.org/t/unexpected-memory-cost-with-julia/84295/5 "2022-07-16T07:05:01Z")

</div>

> [@robsmith11](#):
>
> Are you actually running out of memory during compilation? What’s the peak memory usage of the Julia process?

I took a closer look at my code and found that I had made a mistake. I’m terribly sorry. Although the first compile operation consumes a lot of memory, it is not the cause of running out of memory.

I think the memory ran out because there was no GC in the nested loop

> [@robsmith11](#):
>
> In general, memory will be freed many times by the garbage collector throughout that period, so it should not be construed with memory usage at a single point in time or peak usage.

I think the memory ran out in my code because there was no GC in the nested loop. I found a [similar problem](https://stackoverflow.com/questions/46330121/julia-nested-loops-are-consuming-a-lot-of-memory).

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [July 16, 2022, 7:07am UTC](https://discourse.julialang.org/t/unexpected-memory-cost-with-julia/84295/6 "2022-07-16T07:07:20Z")

</div>

> [@mkitti](#):
>
> Julia has to compile your code to native code at some point. Julia is not an interpreted language.
> 
> If you would like to compile to native code ahead of time, I suggest looking into building a new system image:

Thank you very much, but I don’t think my code problem is due to precompilation
