# Large memory allocation in ForwardDiff

**URL:** <https://discourse.julialang.org/t/large-memory-allocation-in-forwarddiff/42581>\
**Category:** Optimization (Mathematical)\
**Created:** [July 5, 2020, 11:49pm UTC](https://discourse.julialang.org/t/large-memory-allocation-in-forwarddiff/42581 "2020-07-05T23:49:24Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![s-broda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s-broda/32/3946_2.png) [@s-broda](https://discourse.julialang.org/u/s-broda)\
**Post date:** [July 7, 2020, 8:54am UTC](https://discourse.julialang.org/t/large-memory-allocation-in-forwarddiff/42581/7 "2020-07-07T08:54:43Z")

</div>

Note also that ForwardDiff seems to do some recompilation when you call a function with the same signature with an array of different length:

```julia
julia> using ForwardDiff

julia> @time ForwardDiff.gradient(sum, [1., 2., 3., 4.]);
  0.106838 seconds (301.52 k allocations: 15.820 MiB)

julia> @time ForwardDiff.gradient(sum, [1., 2., 3., 4.]);
  0.000014 seconds (4 allocations: 608 bytes)

julia> @time ForwardDiff.gradient(sum, [1., 2., 3., 4., 5.]);
  0.119015 seconds (308.93 k allocations: 16.164 MiB)

julia> @time ForwardDiff.gradient(sum, [1., 2., 3., 4., 5.]);
  0.000020 seconds (4 allocations: 816 bytes)

```

Related: [Another post on package compilation time - #15 by s-broda](https://discourse.julialang.org/t/another-post-on-package-compilation-time/42015/15)

---

_[View the full topic](https://discourse.julialang.org/t/large-memory-allocation-in-forwarddiff/42581)._
