# How to track total memory usage of Julia process over time

**URL:** <https://discourse.julialang.org/t/how-to-track-total-memory-usage-of-julia-process-over-time/91167>\
**Category:** Profiling\
**Tags:** memory-allocation\
**Created:** [December 3, 2022, 6:06am UTC](https://discourse.julialang.org/t/how-to-track-total-memory-usage-of-julia-process-over-time/91167 "2022-12-03T06:06:13Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [December 5, 2022, 11:39am UTC](https://discourse.julialang.org/t/how-to-track-total-memory-usage-of-julia-process-over-time/91167/5 "2022-12-05T11:39:46Z")

</div>

FYI, I had some very insightful discussions with @vchuravy on Slack, some of which I would like to share here (and to record it for future reference):

1. He suggested a neat trick using a self-resurrecting `finalizer` to automatically print the maximum RSS (resident set size as reported by Julia’s [`Sys.maxrss()`](https://github.com/JuliaLang/julia/blob/ec8d014a6181a5266ee656e893d5769a7c203b5a/base/sysinfo.jl#L313-L321), which internally uses the C function [`getrusage`](https://www.gnu.org/software/libc/manual/html_node/Resource-Usage.html)):

```julia
julia> function setup_memuse_tracker()
          tracker = Ref(0)
          function mem_use(tracker)
            finalizer(mem_use, tracker)
            @info "Current memory used" Sys.maxrss()
            nothing
          end
          
          finalizer(mem_use, tracker)
          nothing
       end
setup_memuse_tracker (generic function with 1 method)

julia> setup_memuse_tracker()

julia> GC.gc()
┌ Info: Current memory used
└ Sys.maxrss() = 0x000000001280b000

```

1. He suggested looking at the `procfs` file system, e.g., `/proc/<pid>/statm`. This is similar to the suggestion of using `ps` by @PetrKryslUCSD, but could forgo the need for an external program.
2. He further suggested to look at the output of [`Base.gc_num()`](https://github.com/JuliaLang/julia/blob/ec8d014a6181a5266ee656e893d5769a7c203b5a/base/timing.jl#L29). Alternatively, at the values of [`Base.gc_live_bytes()`](https://github.com/JuliaLang/julia/blob/ec8d014a6181a5266ee656e893d5769a7c203b5a/base/timing.jl#L86-L97) and/or [`Base.jit_total_bytes()`](https://github.com/JuliaLang/julia/blob/ec8d014a6181a5266ee656e893d5769a7c203b5a/base/timing.jl#L99-L107).
3. Finally he suggested to take a look at the tool Arm Forge, which combines the Arm DDT/Arm MAP profiling tools. However, one has to pay for those.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-track-total-memory-usage-of-julia-process-over-time/91167)._
