# Memory usage data for getproperty function

**URL:** https://discourse.julialang.org/t/memory-usage-data-for-getproperty-function/64139
**Category:** Performance
**Created:** [July 6, 2021, 9:41am UTC](https://discourse.julialang.org/t/memory-usage-data-for-getproperty-function/64139 "2021-07-06T09:41:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jsjie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsjie/32/22477_2.png) [@jsjie](https://discourse.julialang.org/u/jsjie)
#### Post date: [July 6, 2021, 9:41am UTC](https://discourse.julialang.org/t/memory-usage-data-for-getproperty-function/64139/1 "2021-07-06T09:41:54Z")

</div>

I’m analysing memory usage [as described in julia manual](https://docs.julialang.org/en/v1/manual/profile/#Memory-allocation-analysis) , and I found that getproperty functions seem to be related to large numbers.  
The following lines is taken from the result .mem file:

```julia
        - function Base.getproperty(system::System, v::Symbol)
1573581184 if v in [
        - :lattice, :k_lattice, :Ω
        - ]
        0 return getproperty(system.cell, v)
1797157632 elseif v in [
        - :elements, :n_atoms, :n_species, :positions, :a2e_inds, :e2a_inds
        - ]
        0 return getproperty(system.ions, v)

```

I think getproperty function should not need to allocate memories, so are these numbers only times the  
corresponding lines are executed?

A little problem: the answer of @Gnimuc explains the reason and gives a solution, but I finally choose to adopt the tuple way as proposed by @Salmon . Whose answer should I mark as “Solution”?

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [July 6, 2021, 10:31am UTC](https://discourse.julialang.org/t/memory-usage-data-for-getproperty-function/64139/2 "2021-07-06T10:31:41Z")

</div>

Do not implement `Base.getproperty` in this way, use simple `if-else` instead:

```julia
if v === :lattice
  return getfield(system.cell, v)
elseif v === :k_lattice
   return getfield(system.cell, v)
...

```

> I think getproperty function should not need to allocate memories, so are these numbers only times the  
> corresponding lines are executed?

Several arrays are created in your code, so the function allocates.

---

<div class="post-metadata">

### Author: ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)
#### Post date: [July 6, 2021, 10:46am UTC](https://discourse.julialang.org/t/memory-usage-data-for-getproperty-function/64139/3 "2021-07-06T10:46:44Z")

</div>

I agree that this should get rid of the allocations. Do you know if it has any disadvantages to just use tuples instead of arrays? I.e

```julia
if v in (:lattice, :k_lattice, :Ω)
  return getproperty(system.cell, v)

```

I used similar style before without thinking much about it, so I’d be interested if this is something to stop in general

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [July 6, 2021, 10:56am UTC](https://discourse.julialang.org/t/memory-usage-data-for-getproperty-function/64139/4 "2021-07-06T10:56:09Z")

</div>

`Tuple` is generally OK, Julia compiler should be good at optimizing it.
