# Why accessing a Float property allocates memory?

**URL:** https://discourse.julialang.org/t/why-accessing-a-float-property-allocates-memory/39235
**Category:** Performance
**Tags:** question
**Created:** [May 10, 2020, 5:59pm UTC](https://discourse.julialang.org/t/why-accessing-a-float-property-allocates-memory/39235 "2020-05-10T17:59:09Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![emancu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emancu/32/14530_2.png) [@emancu](https://discourse.julialang.org/u/emancu)
#### Post date: [May 10, 2020, 5:59pm UTC](https://discourse.julialang.org/t/why-accessing-a-float-property-allocates-memory/39235/1 "2020-05-10T17:59:09Z")

</div>

Hello community!

I’m running a simulation which requires millions of iterations, and the memory consumption (even temporary) is huge and I would like to reduce it. Pursuing this, I found something that I wasn’t expecting.

I have my own struct defined, with some basic attributes and I noticed using `@time` that every time that I want to access a Float64 (or any Float) property it allocates `16bytes` but if the property is an Int, it doesn’t.

Here is my type definition

```julia
abstract type AbstractVoltmeter end

struct EChTVoltmeter <: AbstractVoltmeter
  EChTVoltmeter() = new()
end

mutable struct ECTVoltmeter <: AbstractVoltmeter
  pulse_acum::Float64
  phase::UInt8
  I::Float64 # Current

  ECTVoltmeter() = new(0.0, 1, 1.0)
end

```

And here is the output

```julia
julia> z = ECTVoltmeter()
ECTVoltmeter(0.0, 0x01, 1.0)

julia> @time z.phase
  0.000004 seconds
0x01

julia> @time z.I
  0.000005 seconds (1 allocation: 16 bytes)
1.0

```

I know this won’t change much on my memory-leak problem, but I want to understand what is happening.  
I notice something similar using Tuples, which _always_ allocates memory when you access one element (not expected by me at all)

Thank you!

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 10, 2020, 6:24pm UTC](https://discourse.julialang.org/t/why-accessing-a-float-property-allocates-memory/39235/2 "2020-05-10T18:24:27Z")

</div>

Use BenchmarkTools.jl and you should find that there is no allocation.

---

<div class="post-metadata">

### Author: ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)
#### Post date: [May 10, 2020, 7:01pm UTC](https://discourse.julialang.org/t/why-accessing-a-float-property-allocates-memory/39235/3 "2020-05-10T19:01:14Z")

</div>

To expand slightly on @dpsanders correct answer, in your example you’re doing what’s called “benchmarking in global scope”. In other words, the allocation you’re seeing is due to the fact that you’re running `@time z.I` in global scope and not because running `z.I` (which is equivalent to `getproperty(z,:I)` allocates. See the [section of the performance tips manual section on global variables](https://docs.julialang.org/en/v1/manual/performance-tips/index.html#Avoid-global-variables-1).

As @dpsanders mentioned, you can use the [BenchmarkTools](https://github.com/JuliaCI/BenchmarkTools.jl) package to do careful benchmarking:

```julia
julia> using BenchMarkTools
julia> @benchmark getproperty($z,:I)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 0.015 ns (0.00% GC)
  median time: 0.017 ns (0.00% GC)
  mean time: 0.018 ns (0.00% GC)
  maximum time: 0.031 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

```
