# Memory allocation: funny counting

**URL:** https://discourse.julialang.org/t/memory-allocation-funny-counting/32027
**Category:** General Usage
**Created:** [December 8, 2019, 7:31pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027 "2019-12-08T19:31:26Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [December 8, 2019, 7:31pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027/1 "2019-12-08T19:31:26Z")

</div>

With Julia 1.3, julia --track-allocation=user.

```julia
        0 lambda = E * nu / (1 + nu) / (1 - 2*(nu));
        0 mu = E / 2. / (1+nu);
   860578 mI = diagm(0=>[1, 1, 1, 0.5, 0.5, 0.5]);
      768 m1m1 = _m1*_m1';
  3499241 I3 = [1.0 0 0; 0 1.0 0; 0 0 1.0]
     2016 b = [0.0 0 0; 0 0.0 0; 0 0 0.0]
     2016 sigma = [0.0 0 0; 0 0.0 0; 0 0 0.0]
     6560 return MatDeforNeohookean(mr, mass_density, E, nu, lambda, mu,
        - tangentmoduli3d!, update3d!, 
        - deepcopy(mI), deepcopy(m1m1), deepcopy(I3), deepcopy(b), deepcopy(sigma))

```

How does one interpret the above? The function with this code is called once.  
Where does all the allocation come from?

Edit: I should have mentioned that the measurement is collected during the second run, after clearing the malloc data after the first run.

---

<div class="post-metadata">

### Author: ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)
#### Post date: [July 9, 2020, 6:45pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027/2 "2020-07-09T18:45:00Z")

</div>

What is the dimension and size of \_m1? Be aware with `--track-allocation` sometimes the allocations are not attributed properly so you may get some strange-looking results.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [July 9, 2020, 6:54pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027/3 "2020-07-09T18:54:23Z")

</div>

`_m1` is a 6-element vector. `mI` is a 6 x 6 matrix, and so is `m1m1`.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [July 9, 2020, 7:16pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027/4 "2020-07-09T19:16:20Z")

</div>

> [@PetrKryslUCSD](#):
>
> `I3 = [1.0 0 0; 0 1.0 0; 0 0 1.0]`

What is strange to me is why the count in this line is so high. I mean, it is the same size of `b` below and allocates so much more.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [July 9, 2020, 7:18pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027/5 "2020-07-09T19:18:17Z")

</div>

Notice that the function is called _once_. Not many times.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [July 9, 2020, 7:22pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027/6 "2020-07-09T19:22:41Z")

</div>

Even if it was called multiple times, makes no sense for it to have a value different of `2016`, both `b` and `sigma` have the same size as `I3` and they have the same value that is different from `I3`. If the function is called one, or a thousand times, the three should have the same value (as they would be created the same number of times, there is no `if`).

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [July 9, 2020, 7:47pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027/7 "2020-07-09T19:47:49Z")

</div>

There is no `I3` global variable in the code right? No chance that this can be doing something not obvious?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [July 9, 2020, 7:58pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027/8 "2020-07-09T19:58:16Z")

</div>

Indeed, no globals. Here is the code: [https://github.com/PetrKryslUCSD/FinEtoolsDeforNonlinear.jl/blob/7ec2765d6bf07e2934bef930e069f7e8dbc0225d/src/MatDeforNeohookeanModule.jl#L101](https://github.com/PetrKryslUCSD/FinEtoolsDeforNonlinear.jl/blob/7ec2765d6bf07e2934bef930e069f7e8dbc0225d/src/MatDeforNeohookeanModule.jl#L101)

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [July 9, 2020, 9:33pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027/9 "2020-07-09T21:33:26Z")

</div>

There is some reason for you to create `I3`, `b`, and `sigma`, and then, soon after, pass a `deepcopy` of them to the function instead of the originals? I mean, the originals are not being used for anything, why do you need to pass copies of them instead?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [July 9, 2020, 9:44pm UTC](https://discourse.julialang.org/t/memory-allocation-funny-counting/32027/10 "2020-07-09T21:44:42Z")

</div>

No particular reason other than laziness. The code is borrowed from an earlier project.  
I just didn’t bother removing the deep copies, since this function is called only once per simulation.
