Why accessing a Float property allocates memory?

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

abstract type AbstractVoltmeter end

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

mutable struct ECTVoltmeter <: AbstractVoltmeter
  I::Float64 # Current

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

And here is the output

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

julia> @time z.phase
  0.000004 seconds

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

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!

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

1 Like

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.

As @dpsanders mentioned, you can use the BenchmarkTools package to do careful benchmarking:

julia> using BenchMarkTools
julia> @benchmark getproperty($z,:I)
  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
1 Like