# Iterators.product OutOfMemoryError()

**URL:** <https://discourse.julialang.org/t/iterators-product-outofmemoryerror/83542>\
**Category:** General Usage\
**Created:** [June 30, 2022, 12:55am UTC](https://discourse.julialang.org/t/iterators-product-outofmemoryerror/83542 "2022-06-30T00:55:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuri\_Mizusawa](https://avatars.discourse-cdn.com/v4/letter/y/e480ec/32.png) [@Yuri\_Mizusawa](https://discourse.julialang.org/u/Yuri_Mizusawa)\
**Post date:** [June 30, 2022, 12:55am UTC](https://discourse.julialang.org/t/iterators-product-outofmemoryerror/83542/1 "2022-06-30T00:55:39Z")

</div>

Hi,

I have the following code:

```julia

ary_comb_n=collect(Iterators.product(n_max_O...)) 

```

Where n\_max\_O is an array with vectors like this [[0,1,2,3,4] ,[0,1,2] ].

Iterators.product gives me the combinatory of all the vectors in the variable. Ex.:

5×3 Matrix{Tuple{Int64, Int64}}:

(0, 0) (0, 1) (0, 2)

(1, 0) (1, 1) (1, 2)

(2, 0) (2, 1) (2, 2)

(3, 0) (3, 1) (3, 2)

(4, 0) (4, 1) (4, 2)

I use each combination later to multiplicate a vector. Like (0,0)\*(x,y)=z and I collect these numbers.

My problem is that n\_max\_O is variable and can have up to 60 vectors inside it. When I try to generate that I run out of memory.

Ex. for 20 vectors:

[[0, 1], [0, 1, 2], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1], [0, 1, 2], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1],

[0, 1, 2], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1], [0, 1, 2], [0, 1], [0, 1, 2], [0, 1, 2, 3]]

Is there a better way to do this? Or to generate the combinatory as I use it?

Thanks!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 30, 2022, 1:28am UTC](https://discourse.julialang.org/t/iterators-product-outofmemoryerror/83542/2 "2022-06-30T01:28:57Z")

</div>

> [@Yuri\_Mizusawa](#):
>
> Or to generate the combinatory as I use it?

That’s exactly what `Iterators.product` already does. The problem is that you’re calling `collect()` which tries to collect that sequence into a vector. Just don’t do that:

```julia
ary_comb_n = Iterators.product(...)

```

---

<div class="post-metadata">

**Author:** ![Yuri\_Mizusawa](https://avatars.discourse-cdn.com/v4/letter/y/e480ec/32.png) [@Yuri\_Mizusawa](https://discourse.julialang.org/u/Yuri_Mizusawa)\
**Post date:** [June 30, 2022, 2:29am UTC](https://discourse.julialang.org/t/iterators-product-outofmemoryerror/83542/3 "2022-06-30T02:29:48Z")

</div>

Thank you!!
