# Hey, does anybody know why a generator does allocate memory while an iterator does

**URL:** <https://discourse.julialang.org/t/hey-does-anybody-know-why-a-generator-does-allocate-memory-while-an-iterator-does/57504>\
**Category:** General Usage\
**Created:** [March 18, 2021, 11:32pm UTC](https://discourse.julialang.org/t/hey-does-anybody-know-why-a-generator-does-allocate-memory-while-an-iterator-does/57504 "2021-03-18T23:32:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [March 18, 2021, 11:32pm UTC](https://discourse.julialang.org/t/hey-does-anybody-know-why-a-generator-does-allocate-memory-while-an-iterator-does/57504/1 "2021-03-18T23:32:42Z")

</div>

Hey, does anybody know why a generator does allocate memory while an iterator doesn’t when assigned to an existing location?

```julia
julia&gt; x = randn((1000));

julia&gt; y = 1:1000
1:1000

julia&gt; g = (i for i=1:1000)
Base.Generator{UnitRange{Int64}, typeof(identity)}(identity, 1:1000)

julia&gt; @time x .= g;
  0.000020 seconds (3 allocations: 7.969 KiB)

julia&gt; @time x .= g;
  0.000030 seconds (3 allocations: 7.969 KiB)

julia&gt; @time x .= y;
  0.000048 seconds (3 allocations: 96 bytes)

julia&gt; @time x .= y;
  0.000027 seconds (3 allocations: 96 bytes)

```

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1616110321129100?thread_ts=1616110321.129100&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [March 19, 2021, 9:00am UTC](https://discourse.julialang.org/t/hey-does-anybody-know-why-a-generator-does-allocate-memory-while-an-iterator-does/57504/2 "2021-03-19T09:00:49Z")

</div>

Citing @mcabbott:

“Generators get `collect`ed when used with broadcasting”.

This [issue](https://github.com/JuliaLang/julia/issues/19198) goes into a similar direction.
