# Memory allocation in generator with zip

**URL:** <https://discourse.julialang.org/t/memory-allocation-in-generator-with-zip/1903>\
**Category:** General Usage\
**Created:** [February 4, 2017, 2:56pm UTC](https://discourse.julialang.org/t/memory-allocation-in-generator-with-zip/1903 "2017-02-04T14:56:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [February 4, 2017, 2:56pm UTC](https://discourse.julialang.org/t/memory-allocation-in-generator-with-zip/1903/1 "2017-02-04T14:56:15Z")

</div>

Hi,

I am experiencing some suspect memory allocation using a generator containing a `zip` iterator. The minimal example is this:

```julia
as = Vector{Float64}[randn(5) for i = 1:200000]
bs = Vector{Float64}[randn(5) for i = 1:200000]

gen1 = (dot(a, b) for (a, b) in zip(as, bs))
gen2 = (dot(a, a) for a in as)

println(@benchmark mean(gen1))
println(@benchmark mean(gen2))

```

Using `BenchmarkTools` I get

```julia
BenchmarkTools.Trial: 
  memory estimate: 6.10 mb
  allocs estimate: 200001
  --------------
  minimum time: 5.282 ms (0.00% GC)
  median time: 5.801 ms (0.00% GC)
  mean time: 5.914 ms (2.72% GC)
  maximum time: 9.829 ms (0.00% GC)
  --------------
  samples: 846
  evals/sample: 1
  time tolerance: 5.00%
  memory tolerance: 1.00%

```

for the generator with the zip and and

```julia
BenchmarkTools.Trial: 
  memory estimate: 16.00 bytes
  allocs estimate: 1
  --------------
  minimum time: 3.055 ms (0.00% GC)
  median time: 3.233 ms (0.00% GC)
  mean time: 3.294 ms (0.00% GC)
  maximum time: 5.912 ms (0.00% GC)
  --------------
  samples: 1518
  evals/sample: 1
  time tolerance: 5.00%
  memory tolerance: 1.00%

```

for the other one. This might be known, but I would like to know why this happens and if there is a way round.

Thanks!

Davide

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [February 5, 2020, 9:49am UTC](https://discourse.julialang.org/t/memory-allocation-in-generator-with-zip/1903/2 "2020-02-05T09:49:12Z")

</div>

I know I’m late to the party 🙂  
I think the problem here is not actually the zip. You’re computing two different things as you only use `a` in one of them and `a` and `b` in the other. If you write:

```julia
gen1 = (dot(a, b) for (a, b) in zip(as, as))

```

You get the same performance and memory as for gen2

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [February 5, 2020, 9:56am UTC](https://discourse.julialang.org/t/memory-allocation-in-generator-with-zip/1903/3 "2020-02-05T09:56:17Z")

</div>

No, in fact this is fixed by now:

```julia

julia> @btime mean(gen1)
  4.321 ms (1 allocation: 16 bytes)
-0.0011262565703488662

julia> @btime mean(gen2)
  3.628 ms (1 allocation: 16 bytes)
5.004320788116237

```
