# Why does using structs increase allocations (and calculation time)

**URL:** https://discourse.julialang.org/t/why-does-using-structs-increase-allocations-and-calculation-time/96843
**Category:** New to Julia
**Created:** [March 30, 2023, 12:37pm UTC](https://discourse.julialang.org/t/why-does-using-structs-increase-allocations-and-calculation-time/96843 "2023-03-30T12:37:17Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [March 30, 2023, 12:52pm UTC](https://discourse.julialang.org/t/why-does-using-structs-increase-allocations-and-calculation-time/96843/2 "2023-03-30T12:52:50Z")

</div>

> [@Thomas\_Van\_Giel](#):
>
> ```julia
> struct single_HOI_ecosystem <: one_HOI_ecosystem
> N::Int
> n::Vector{<:Real}
> m::Matrix{Float64}
> A::Matrix{Float64}
> R::Vector{<:Real}
> d::Vector{<:Real}
> end
> 
> ```

This is creating containers with abstract types unfortunately. Dealing with abstract types require heap allocations (as you don’t know how much memory is need ahead of time). You could do:

```julia
struct single_HOI_ecosystem{T<:Real} <: one_HOI_ecosystem
    N::Int
    n::Vector{T}
    m::Matrix{Float64}
    A::Matrix{Float64}
    R::Vector{T}
    d::Vector{T}
end

```

Modifying if you need some more types. The general rule of thumb is that the types should be either concrete in the `struct` or have a parametric type which should also be concrete. You should check what `T` is when creating the struct to make sure it is concrete.

Also, use `@code_typed` to check for type instabilities , Unions, Anys or abstract types that can’t be inferred.

---

_[View the full topic](https://discourse.julialang.org/t/why-does-using-structs-increase-allocations-and-calculation-time/96843)._
