# Why does rand() report no allocations, but zeros() does when measured with --track-allocation=user?

**URL:** https://discourse.julialang.org/t/why-does-rand-report-no-allocations-but-zeros-does-when-measured-with-track-allocation-user/65974
**Category:** New to Julia
**Tags:** memory-allocation, profiling
**Created:** [August 7, 2021, 1:46am UTC](https://discourse.julialang.org/t/why-does-rand-report-no-allocations-but-zeros-does-when-measured-with-track-allocation-user/65974 "2021-08-07T01:46:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mdtisdall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdtisdall/32/27418_2.png) [@mdtisdall](https://discourse.julialang.org/u/mdtisdall)
#### Post date: [August 7, 2021, 1:46am UTC](https://discourse.julialang.org/t/why-does-rand-report-no-allocations-but-zeros-does-when-measured-with-track-allocation-user/65974/1 "2021-08-07T01:46:42Z")

</div>

Very basic question: I’m trying to understand why the instantiation of `A` in the following small example doesn’t appear to produce any allocations in the profiling output:

```julia
        - function testfunc()
        0 A = rand(1001,2,3);
        -
    48144 B = zeros(1001,2,3);
        - end

```

However, if I change the instantiation of `A` to use `zeros` instead of `rand` I get:

```julia
        - function testfunc()
    48144 A = zeros(1001,2,3);
        -
    48144 B = zeros(1001,2,3);
        - end

```

which makes more sense to me.

What is the origin of the difference in these two outputs?

To be clear, I got these results with `julia --track-allocation=user` and ran

```julia
julia> testfunc();

julia> Profile.clear_malloc_data()

julia> testfunc();

```

to generate these results.

_ **Update** _ Just to add, although the runtimes unsurprisingly differ, both examples report the (identical) expected size of allocation when measured with `@btime`:

```julia
julia> @btime testfunc();
  6.637 μs (4 allocations: 94.03 KiB)

```

So I’m curious how I can get the output of `@btime` to be assigned to the “responsible” lines in my code.

---

<div class="post-metadata">

### Author: ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)
#### Post date: [August 7, 2021, 4:50am UTC](https://discourse.julialang.org/t/why-does-rand-report-no-allocations-but-zeros-does-when-measured-with-track-allocation-user/65974/2 "2021-08-07T04:50:30Z")

</div>

Just a guess,

but I would expect the memory for `rand` to be allocated inside `rand` function and the assignment `A = rand(...)` is just a binding of a name. If you dive to rand, you should see the allocation.

---

<div class="post-metadata">

### Author: ![mdtisdall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdtisdall/32/27418_2.png) [@mdtisdall](https://discourse.julialang.org/u/mdtisdall)
#### Post date: [August 7, 2021, 1:58pm UTC](https://discourse.julialang.org/t/why-does-rand-report-no-allocations-but-zeros-does-when-measured-with-track-allocation-user/65974/3 "2021-08-07T13:58:16Z")

</div>

Interesting; so is there a way to record cumulative allocations? I’d like both lines to show that, as they are run, they both allocate the same amount of memory, to help me understand where allocations are being generated within my own code.

---

<div class="post-metadata">

### Author: ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)
#### Post date: [August 7, 2021, 2:43pm UTC](https://discourse.julialang.org/t/why-does-rand-report-no-allocations-but-zeros-does-when-measured-with-track-allocation-user/65974/4 "2021-08-07T14:43:01Z")

</div>

I have no idea, but for measure time and allocations in your code I recommend to use [TimerOutputs.jl](https://github.com/KristofferC/TimerOutputs.jl) it is very simple and useful.
