# OutOfMemory(): How to inspect environment memory?

**URL:** https://discourse.julialang.org/t/outofmemory-how-to-inspect-environment-memory/40738
**Category:** General Usage
**Created:** [June 4, 2020, 2:42pm UTC](https://discourse.julialang.org/t/outofmemory-how-to-inspect-environment-memory/40738 "2020-06-04T14:42:10Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [June 4, 2020, 2:42pm UTC](https://discourse.julialang.org/t/outofmemory-how-to-inspect-environment-memory/40738/1 "2020-06-04T14:42:10Z")

</div>

I run into an `OutOfMemory` error when running an algorithm several times. Everything happens inside functions so I don’t understand why and I would like to inspect what has accumulated in the memory. The error is triggered on a `push!(::Vector, x)`. I see in my task manager that the julia process suddenly takes 6Gb of memory when I spam the algorithm.

Unfortunately, the environment pane in Juno does not show that anything is taking more space.

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [June 4, 2020, 2:49pm UTC](https://discourse.julialang.org/t/outofmemory-how-to-inspect-environment-memory/40738/2 "2020-06-04T14:49:24Z")

</div>

I would help if you post some code. This error can happen for many reasons.

---

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [June 4, 2020, 2:53pm UTC](https://discourse.julialang.org/t/outofmemory-how-to-inspect-environment-memory/40738/3 "2020-06-04T14:53:27Z")

</div>

Yes I know, but it’s 500 lines of code I can’t really show a MWE unfortunately. I can paste the stacktrace though:

```julia
ERROR: LoadError: OutOfMemoryError()
Stacktrace:
 [1] push! at ./array.jl:827 [inlined]
 [2] backward_SDP(::InventoryModels.Scarf.Instance{Float64}, ::Float64) at /home/dehaybe/OneDrive/Doctorat/DRL_SIP/src/InventoryModels/src/Scarf.jl:135
 [3] backward_SDP(::InventoryModels.Scarf.Instance{Float64}) at /home/dehaybe/OneDrive/Doctorat/DRL_SIP/src/InventoryModels/src/Scarf.jl:116
 [4] macro expansion at /home/dehaybe/OneDrive/Doctorat/DRL_SIP/scripts/hyperparameter_tuning.jl:59 [inlined]
 [5] (::var"#15#18"{Hyperoptimizer})(::Symbol) at /home/dehaybe/.julia/packages/Hyperopt/YyteE/src/Hyperopt.jl:82
 [6] #progress#2(::String, ::typeof(Juno.progress), ::var"#15#18"{Hyperoptimizer}) at /home/dehaybe/.julia/packages/Juno/f8hj2/src/progress.jl:35
 [7] progress at /home/dehaybe/.julia/packages/Juno/f8hj2/src/progress.jl:32 [inlined]
 [8] macro expansion at /home/dehaybe/.julia/packages/Hyperopt/YyteE/src/Hyperopt.jl:80 [inlined]
 [9] tune() at /home/dehaybe/OneDrive/Doctorat/DRL_SIP/scripts/hyperparameter_tuning.jl:25
 [10] top-level scope at /home/dehaybe/OneDrive/Doctorat/DRL_SIP/scripts/hyperparameter_tuning.jl:105
 [11] include at ./boot.jl:328 [inlined]
 [12] include_relative(::Module, ::String) at ./loading.jl:1105
 [13] include(::Module, ::String) at ./Base.jl:31
 [14] include(::String) at ./client.jl:424
 [15] top-level scope at REPL[1]:1

```

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [June 4, 2020, 2:59pm UTC](https://discourse.julialang.org/t/outofmemory-how-to-inspect-environment-memory/40738/4 "2020-06-04T14:59:05Z")

</div>

Unfortunately, from that message, the only thing we can infer, I think, is that you are trying to allocate in the `push!` an array that does not fit in the available memory.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [June 4, 2020, 8:12pm UTC](https://discourse.julialang.org/t/outofmemory-how-to-inspect-environment-memory/40738/5 "2020-06-04T20:12:06Z")

</div>

Can you run it with less input? Or maybe stop execution before it’s done? If you could start julia with “–track-allocation=user” to see where most of the memory is being allocated. That might work even with the crash, you just need to terminate julia (Crtl D) to have it write out the allocations.

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [June 4, 2020, 8:36pm UTC](https://discourse.julialang.org/t/outofmemory-how-to-inspect-environment-memory/40738/6 "2020-06-04T20:36:11Z")

</div>

You can also use the functions `sizeof` and `Base.summarysize` to print out the size of the objects in your function. Start with a small input so it dosn’t crash. Increase your input by a magnitude and see the growth in memory size.

I recently had out of memory errors, particularly with a matrix 100,000 x 100,000. The element type of this array was `Int64`. I was only story the numbers 1 to 8 for each element, and substantially reduced my memory consumption by changing the type of each element to `Int16`.

I actually don’t know whether that affects performance or not (something to do with 64 bit machines and optimal CPU utilization), but it worked for my case.

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [June 5, 2020, 7:18am UTC](https://discourse.julialang.org/t/outofmemory-how-to-inspect-environment-memory/40738/7 "2020-06-05T07:18:10Z")

</div>

> [@affans](#):
>
> I was only story the numbers 1 to 8 for each element, and substantially reduced my memory consumption by changing the type of each element to `Int16` .

In this case `[U]Int8` would be also enough, reducing the size by a factor of two.

@HenriDeh I recommend reading the section about profiling: [Profiling · The Julia Language](https://docs.julialang.org/en/v1/manual/profile/) this will help you find the problematic lines and functions.

---

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [June 5, 2020, 8:12am UTC](https://discourse.julialang.org/t/outofmemory-how-to-inspect-environment-memory/40738/8 "2020-06-05T08:12:43Z")

</div>

Hello all,

Thank you for your answers. It turned out that some arguments would make an infinite loop. This was more a mathematical issue in the end.
