# Are there any tools in julia that can test the peak memory consumption of the functions I wrote?

**URL:** <https://discourse.julialang.org/t/are-there-any-tools-in-julia-that-can-test-the-peak-memory-consumption-of-the-functions-i-wrote/113011>\
**Category:** Performance\
**Tags:** high-performance, memory-analysis\
**Created:** [April 16, 2024, 12:56pm UTC](https://discourse.julialang.org/t/are-there-any-tools-in-julia-that-can-test-the-peak-memory-consumption-of-the-functions-i-wrote/113011 "2024-04-16T12:56:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [April 16, 2024, 12:56pm UTC](https://discourse.julialang.org/t/are-there-any-tools-in-julia-that-can-test-the-peak-memory-consumption-of-the-functions-i-wrote/113011/1 "2024-04-16T12:56:13Z")

</div>

Let’s look at this example first:

```julia
julia> using BenchmarkTools

julia> 

julia> function f(x::Matrix{Float64})
           y = x * x
           return nothing
       end
f (generic function with 1 method)

julia> 

julia> a = rand(4,4)
4×4 Matrix{Float64}:
 0.267034 0.604533 0.810068 0.0177578
 0.498969 0.358759 0.515774 0.246705
 0.791061 0.493207 0.843597 0.0925471
 0.706005 0.702466 0.414509 0.150272

julia> 

julia> @btime begin
           f(a)
       end
  156.121 ns (1 allocation: 192 bytes)

julia> 

julia> @btime begin
           f(a)
           f(a)
       end
  310.246 ns (2 allocations: 384 bytes)

julia> 

julia> @btime begin
           f(a)
           f(a)
           f(a)
       end
  465.306 ns (3 allocations: 576 bytes)

```

For the last one, its peak memory consumption should equal 192 bytes, not triple. I don’t know if my example is appropriate, but I think you get the idea, I want to know if there’s a function that can gives the peak memory consumption.

```julia
julia> @time begin
           f(a)
           f(a)
           f(a)
       end
  0.000008 seconds (3 allocations: 576 bytes)

julia> @allocated begin
           f(a)
           f(a)
           f(a)
       end
576

```

Neither can these two.

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [April 16, 2024, 1:00pm UTC](https://discourse.julialang.org/t/are-there-any-tools-in-julia-that-can-test-the-peak-memory-consumption-of-the-functions-i-wrote/113011/2 "2024-04-16T13:00:03Z")

</div>

What is the use of memory estimation given by @time, @btime, @allocated?  
Shouldn’t we be concerned about peak memory consumption? Because it determines whether we can run on a machine with a maximum amount of memory.
