# \#preallocation

**URL:** https://discourse.julialang.org/tag/preallocation/1097.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Reducing the number of allocations in a function](https://discourse.julialang.org/t/reducing-the-number-of-allocations-in-a-function/126906)

<div class="topic-metadata">

**Author:** [@andrea\_bertozzi](https://discourse.julialang.org/u/andrea_bertozzi)\
**Replies:** 14\
**Last updated:** [March 14, 2025, 12:30pm UTC](https://discourse.julialang.org/t/reducing-the-number-of-allocations-in-a-function/126906 "2025-03-14T12:30:39Z")

</div>

Hello, I have this function. When running benchmark, I get 519 allocations. Does anyone know how I can reduce those? Thank you! using LinearAlgebra struct Settings rho::Float64 g\_earth::Vector{Float64} cd…

---

## [Solver and AD advice for nonlinear LS problem](https://discourse.julialang.org/t/solver-and-ad-advice-for-nonlinear-ls-problem/125014)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 5\
**Last updated:** [January 21, 2025, 5:26pm UTC](https://discourse.julialang.org/t/solver-and-ad-advice-for-nonlinear-ls-problem/125014 "2025-01-21T17:26:51Z")

</div>

(This is a vague open-ended question for which I cannot provide an MWE.) I have a nonlinear least squares problem \\min\_x \\| f(x) \\|\_2^2 where x \\in \\mathbb{R}^N, N \\approx 20...40, and f(x) has about 100–200 elements…

---

## [Not using PreallocationTools.jl correctly](https://discourse.julialang.org/t/not-using-preallocationtools-jl-correctly/123609)

<div class="topic-metadata">

**Author:** [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)\
**Replies:** 11\
**Last updated:** [December 12, 2024, 2:26pm UTC](https://discourse.julialang.org/t/not-using-preallocationtools-jl-correctly/123609 "2024-12-12T14:26:42Z")

</div>

I have run into the same problem that others have run into before me. Unfortunately, I can’t solve the problem despite the fact that the question has been asked in (slightly) different guises (e.g. link1, link2). The pr…

---

## [Avoiding allocations and run-time dispatch with ForwardDiff](https://discourse.julialang.org/t/avoiding-allocations-and-run-time-dispatch-with-forwarddiff/123699)

<div class="topic-metadata">

**Author:** [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Replies:** 2\
**Last updated:** [December 11, 2024, 1:29pm UTC](https://discourse.julialang.org/t/avoiding-allocations-and-run-time-dispatch-with-forwarddiff/123699 "2024-12-11T13:29:24Z")

</div>

I have an optimization problem which involves solving an ODE and finding the input to the “system” such that an objective is minimized. A MWE of a toy problem is below: import Optimization import ForwardDiff import Opti…

---

## [Tidiest way to reuse variables inside a function to reduce gc time](https://discourse.julialang.org/t/tidiest-way-to-reuse-variables-inside-a-function-to-reduce-gc-time/119509)

<div class="topic-metadata">

**Author:** [@fmor](https://discourse.julialang.org/u/fmor)\
**Replies:** 8\
**Last updated:** [September 18, 2024, 9:09am UTC](https://discourse.julialang.org/t/tidiest-way-to-reuse-variables-inside-a-function-to-reduce-gc-time/119509 "2024-09-18T09:09:22Z")

</div>

I’m writing a function to fit a spatio-temporal model for clustering. At each iteration it has to compute spatial cohesions on subsets of units, and I saw that this takes a lot of the execution time, which is spent in ga…

---

## [Optimising allocations and memory use in ODEProblems](https://discourse.julialang.org/t/optimising-allocations-and-memory-use-in-odeproblems/119513)

<div class="topic-metadata">

**Author:** [@markjholmes](https://discourse.julialang.org/u/markjholmes)\
**Replies:** 2\
**Last updated:** [September 18, 2024, 9:14am UTC](https://discourse.julialang.org/t/optimising-allocations-and-memory-use-in-odeproblems/119513 "2024-09-18T09:14:30Z")

</div>

If I have an ODEProblem with many lines of computation going on at each step, when (if ever) does it become sensible to pre-allocate the space for these computations inside the parameters, p? With a simplified example, …

---

## [Performance of persistent variables](https://discourse.julialang.org/t/performance-of-persistent-variables/112044)

<div class="topic-metadata">

**Author:** [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Replies:** 0\
**Last updated:** [March 24, 2024, 4:33pm UTC](https://discourse.julialang.org/t/performance-of-persistent-variables/112044 "2024-03-24T16:33:09Z")

</div>

Hello, I have a simple toy example in which I compare two methods of creating persistent variables. In my case, both methods exhibit identical performance. Are there any differences to consider regarding safety, compil…

---

## [IterativeSolvers.jl not working as expected with mutating and allocation](https://discourse.julialang.org/t/iterativesolvers-jl-not-working-as-expected-with-mutating-and-allocation/110277)

<div class="topic-metadata">

**Author:** [@erny123](https://discourse.julialang.org/u/erny123)\
**Replies:** 9\
**Last updated:** [March 5, 2024, 9:49pm UTC](https://discourse.julialang.org/t/iterativesolvers-jl-not-working-as-expected-with-mutating-and-allocation/110277 "2024-03-05T21:49:00Z")

</div>

I’m trying to solve a simple linear system. However, it seems like the preallocated versions of any IterativeSolvers.jl functions are either much slower or allocating more memory. Here’s the example: using IterativeSol…

---

## [How to preallocate for a parallel monte carlo simulation?](https://discourse.julialang.org/t/how-to-preallocate-for-a-parallel-monte-carlo-simulation/107104)

<div class="topic-metadata">

**Author:** [@Taneli\_Tolppanen](https://discourse.julialang.org/u/Taneli_Tolppanen)\
**Replies:** 5\
**Last updated:** [December 8, 2023, 8:08am UTC](https://discourse.julialang.org/t/how-to-preallocate-for-a-parallel-monte-carlo-simulation/107104 "2023-12-08T08:08:22Z")

</div>

I am trying to run a series of monte carlo simulations. My simulation gives as an output a matrix. I would like to preallocate this matrix for each of the simulations, and also preallocate some work vectors since I am ru…

---

## [Faster isapprox()](https://discourse.julialang.org/t/faster-isapprox/101202)

<div class="topic-metadata">

**Author:** [@user\_231578](https://discourse.julialang.org/u/user_231578)\
**Replies:** 14\
**Last updated:** [July 5, 2023, 2:51pm UTC](https://discourse.julialang.org/t/faster-isapprox/101202 "2023-07-05T14:51:14Z")

</div>

I have to run (possibly) millions of times the function isapprox(A, A\_, rtol=1e-4), where A and A\_ are two matrices. To improve efficiency, I defined a custom isapprox() function using a more efficient norm: using LoopV…

---

## [Symbolics.jacobian\_sparsity detection errors with PreallocationTools](https://discourse.julialang.org/t/symbolics-jacobian-sparsity-detection-errors-with-preallocationtools/100715)

<div class="topic-metadata">

**Author:** [@sanjeev](https://discourse.julialang.org/u/sanjeev)\
**Replies:** 2\
**Last updated:** [June 22, 2023, 10:02pm UTC](https://discourse.julialang.org/t/symbolics-jacobian-sparsity-detection-errors-with-preallocationtools/100715 "2023-06-22T22:02:52Z")

</div>

When I try to do jacobian sparsity detection, I get error that ERROR: MethodError: no method matching zero(::Type{Any}). The array returned from PreallocationsTools.jl for array of Symbolics.Num datatype, is of type Ve…

---

## [Performance of function with pre-allocated outputs](https://discourse.julialang.org/t/performance-of-function-with-pre-allocated-outputs/98457)

<div class="topic-metadata">

**Author:** [@jekyllstein](https://discourse.julialang.org/u/jekyllstein)\
**Replies:** 3\
**Last updated:** [May 8, 2023, 8:19am UTC](https://discourse.julialang.org/t/performance-of-function-with-pre-allocated-outputs/98457 "2023-05-08T08:19:37Z")

</div>

Consider the following mutating function that mutates a vector input: function f\_test!(xout::Vector{T}, a::T, b::T) where T \<: Real xout .= a .\* b view(xout, 1:max(1, lastindex(xout)-5)) .-= b return xout end I time…

---

## [Disappearing methods from loaded libraries, seemingly iff \`using PreallocationTools\`](https://discourse.julialang.org/t/disappearing-methods-from-loaded-libraries-seemingly-iff-using-preallocationtools/93496)

<div class="topic-metadata">

**Author:** [@joaovgranja](https://discourse.julialang.org/u/joaovgranja)\
**Replies:** 6\
**Last updated:** [January 25, 2023, 4:32pm UTC](https://discourse.julialang.org/t/disappearing-methods-from-loaded-libraries-seemingly-iff-using-preallocationtools/93496 "2023-01-25T16:32:50Z")

</div>

I have a weird issue that I am unable to reproduce in a MWE. It happens in the context of a reasonably large project. Some methods are not available despite their libraries being loaded. For instance: MethodError: no m…

---

## [Pre-allocating a structure to reduce memory?](https://discourse.julialang.org/t/pre-allocating-a-structure-to-reduce-memory/85791)

<div class="topic-metadata">

**Author:** [@jrhalket](https://discourse.julialang.org/u/jrhalket)\
**Replies:** 14\
**Last updated:** [August 16, 2022, 9:56am UTC](https://discourse.julialang.org/t/pre-allocating-a-structure-to-reduce-memory/85791 "2022-08-16T09:56:19Z")

</div>

I have a function SimData that I will call in a loop 1000s of times. I will then “call the loop” a number of times in the course of searching for a maxima. So the SimData function will be call perhaps 100,000s of times. …

---

## [Preallocating Vectors of NamedTuples](https://discourse.julialang.org/t/preallocating-vectors-of-namedtuples/85105)

<div class="topic-metadata">

**Author:** [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Replies:** 3\
**Last updated:** [August 1, 2022, 5:44pm UTC](https://discourse.julialang.org/t/preallocating-vectors-of-namedtuples/85105 "2022-08-01T17:44:19Z")

</div>

Dear all, I hope you are well! I have a question regarding preallocating Vectors of NamedTuples. I have a custom struct that contains a field that is a NamedTuple. mutable struct MyCustomStruct{T\<:NamedTuple} param…
