# Why am I getting OutOfMemoryError() and how do I avoid it?

**URL:** <https://discourse.julialang.org/t/why-am-i-getting-outofmemoryerror-and-how-do-i-avoid-it/74639>\
**Category:** General Usage\
**Tags:** question, memory\
**Created:** [January 14, 2022, 7:00pm UTC](https://discourse.julialang.org/t/why-am-i-getting-outofmemoryerror-and-how-do-i-avoid-it/74639 "2022-01-14T19:00:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [January 14, 2022, 7:00pm UTC](https://discourse.julialang.org/t/why-am-i-getting-outofmemoryerror-and-how-do-i-avoid-it/74639/1 "2022-01-14T19:00:55Z")

</div>

I am trying to generate combinations of integers in the range from 1 to 12. Using 8 as an argument for the function below I get an OutOfMemory() error. The function works as expected for N \< 8. My PC has 32GB of memory of which only 31% is being used. Since `binomial(12, 8) == 495` the number of items is small and so is the space occupied by each one. What is happening?

```julia
function all_combs(n)
    m12 = 1:12 |> collect
    Base.product(values(repeat([m12], n))...) |> collect |> vec
end

julia> all_combs(8)
ERROR: OutOfMemoryError()

```

Is there an alternative way to create this array?

---

<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:** [January 14, 2022, 7:14pm UTC](https://discourse.julialang.org/t/why-am-i-getting-outofmemoryerror-and-how-do-i-avoid-it/74639/2 "2022-01-14T19:14:28Z")

</div>

No issues on my side. Try this  
for `n = 12`

```julia
julia> vec(collect(Base.product(values(repeat(collect(1:12), 12))...)))
1-element Vector{NTuple{144, Int64}}:
 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

```

and for `n = 8`

```julia
julia> vec(collect(Base.product(values(repeat(collect(1:12), 8))...)))
1-element Vector{NTuple{96, Int64}}:
 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 14, 2022, 7:17pm UTC](https://discourse.julialang.org/t/why-am-i-getting-outofmemoryerror-and-how-do-i-avoid-it/74639/3 "2022-01-14T19:17:01Z")

</div>

You are creating all Combinations with repetition (and order matters):

```julia
julia> all_combs(2)
144-element Vector{Tuple{Int64, Int64}}:
(1, 1)
...
(12, 1)
...
(1, 12)
...
(12, 12)

```

but

```julia
binomial(12, 8) == 495

```

is all combinations without repetition and this is how you can do it:

```julia
julia> using Combinatorics

julia> collect(combinations(1:12,8))
495-element Vector{Vector{Int64}}:
...

```

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [January 14, 2022, 7:19pm UTC](https://discourse.julialang.org/t/why-am-i-getting-outofmemoryerror-and-how-do-i-avoid-it/74639/4 "2022-01-14T19:19:57Z")

</div>

`all_combs(n)` creates a `Vector` of length `12^n` where each element consists of `n` `Int`s (each requiring 8 bytes of memory, assuming a 64-bit system). So for `n == 8`, you need `12^8 * 8 * 8` bytes, or about 27.5 GB, which is \> 69% of your total RAM.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 14, 2022, 7:26pm UTC](https://discourse.julialang.org/t/why-am-i-getting-outofmemoryerror-and-how-do-i-avoid-it/74639/5 "2022-01-14T19:26:14Z")

</div>

The easy answer here is to just not collect and use the iterator directly.
