# Track memory usage

**URL:** https://discourse.julialang.org/t/track-memory-usage/52158
**Category:** General Usage
**Tags:** question
**Created:** [December 21, 2020, 8:34am UTC](https://discourse.julialang.org/t/track-memory-usage/52158 "2020-12-21T08:34:05Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [December 21, 2020, 8:34am UTC](https://discourse.julialang.org/t/track-memory-usage/52158/1 "2020-12-21T08:34:05Z")

</div>

Is there a package/function that I can use to see, track, log, and/or diagnose the memory usage of variables?

I’m serving a website with `JSServe` and `WGLMakie`, and after a few hours of usage I get `OutOfMemory` errors. My initial tests (like printing `summarysize`) have been helpful, but I’m wondering if there’s something out there that is better-suited for tracking the usage over long periods of time. It would be extra helpful if trends would be picked up as well: like if a variable increases in size by a constant factor as a function of time or a function of “events”.

To be clear, `BenchmarkTools` is great (use it all the time, thank you @jrevels), but it’s mostly for testing a single function, not the usage of a long process (or perhaps I’m wrong?).

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 21, 2020, 10:11am UTC](https://discourse.julialang.org/t/track-memory-usage/52158/2 "2020-12-21T10:11:48Z")

</div>

You can track allocations using this: [Profiling · The Julia Language](https://docs.julialang.org/en/v1/manual/profile/#Memory-allocation-analysis)

Maybe that can help, if you leave the process running for a while (or different spans of time), and check where allocations are occurring.

Here I give an example (because the manual is not as direct): [Disabling allocations - #10 by lmiq](https://discourse.julialang.org/t/disabling-allocations/51028/10)

Depending on your problem, you could check the memory usage and call the GC (I have used this before solving some problems in a parallel code, afterwards it was not useful anymore): [https://discourse.julialang.org/t/garbage-collector-behaviour-when-memory-is-almost-full](https://discourse.julialang.org/t/garbage-collector-behaviour-when-memory-is-almost-full)

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [December 21, 2020, 10:30am UTC](https://discourse.julialang.org/t/track-memory-usage/52158/3 "2020-12-21T10:30:13Z")

</div>

That’s great. It would have been extra useful if that `.mem` file would have been generated every `n` minutes, and/or when triggered from inside the code being profiled. This way, I’d be able to diagnose which LOC results in the most obvious increase, not just which LOC needs the most memory.

I guess the easiest way around that (and what I’ll probably be doing next) is to have it running for long enough until it crashes due to `OutOfMemory` errors, and then the worst LOC should pop up.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 21, 2020, 11:06am UTC](https://discourse.julialang.org/t/track-memory-usage/52158/4 "2020-12-21T11:06:55Z")

</div>

> [@yakir12](#):
>
> That’s great. It would have been extra useful if that `.mem` file would have been generated every `n` minutes, and/or when triggered from inside the code being profiled. This way, I’d be able to diagnose which LOC results in the most obvious increase, not just which LOC needs the most memory.

Yes. Yet, to be true, after some profiling with that (or not), I generally fall back to the manual memory tracking, with `@allocated` . I put bunches of code inside something like:

```julia
a = @allocated begin
    ....
end; a > 0 && println(a)

```

You could, when more directed to a point, use that, and print the allocations of some parts of your code to a file whenever that part is executed, and see what is going wrong.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [December 21, 2020, 11:08am UTC](https://discourse.julialang.org/t/track-memory-usage/52158/5 "2020-12-21T11:08:58Z")

</div>

Awesome, thanks a lot for the great pointers.

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [December 21, 2020, 11:16am UTC](https://discourse.julialang.org/t/track-memory-usage/52158/6 "2020-12-21T11:16:28Z")

</div>

You can also try to use [GitHub - KristofferC/TimerOutputs.jl: Formatted output of timed sections in Julia](https://github.com/KristofferC/TimerOutputs.jl)

It provides more convinient way to trace memory usage and number of calls, you can sprinkle `@timeit` here and there and print output from time to time.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [December 21, 2020, 11:46am UTC](https://discourse.julialang.org/t/track-memory-usage/52158/7 "2020-12-21T11:46:41Z")

</div>

Oh, I think this might be perfect…

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [December 21, 2020, 2:16pm UTC](https://discourse.julialang.org/t/track-memory-usage/52158/8 "2020-12-21T14:16:02Z")

</div>

So with the help of Simon Danisch I’m slowly realizing that it’s not _allocated_ memory I need to track, but actually how much of that memory get retained and never released. So I’m back to `summarysize`…

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [December 21, 2020, 3:04pm UTC](https://discourse.julialang.org/t/track-memory-usage/52158/9 "2020-12-21T15:04:33Z")

</div>

I’m guessing Simon has already told you this, but if not, make sure to update to AbstractPlotting 0.14.2 (which has [https://github.com/JuliaPlots/AbstractPlotting.jl/pull/574](https://github.com/JuliaPlots/AbstractPlotting.jl/pull/574)). That fixed some severe memory issues I was having.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [December 21, 2020, 3:13pm UTC](https://discourse.julialang.org/t/track-memory-usage/52158/10 "2020-12-21T15:13:25Z")

</div>

Thanks, I am on 0.14.2 🙂

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [December 22, 2020, 10:12am UTC](https://discourse.julialang.org/t/track-memory-usage/52158/11 "2020-12-22T10:12:09Z")

</div>

I ended up using all of the solutions mentioned here (so no real one singular way). I’ll argue (for whoever feels up for it) that there is place for a diagnostic tool for detecting memory leaks. Something that could track the change in memory per variable as a function of time/events… But these kind of situations might be too rare to warrant a dedicated tool.

Thanks for all the helpful tips!

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [December 23, 2020, 8:37am UTC](https://discourse.julialang.org/t/track-memory-usage/52158/12 "2020-12-23T08:37:49Z")

</div>

So, what was the leak? Something easy to guess with 20/20 hindsight or something unexpected?

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [December 23, 2020, 9:24am UTC](https://discourse.julialang.org/t/track-memory-usage/52158/13 "2020-12-23T09:24:38Z")

</div>

I still don’t know. It’s a real mystery…

---

<div class="post-metadata">

### Author: ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)
#### Post date: [December 23, 2020, 1:10pm UTC](https://discourse.julialang.org/t/track-memory-usage/52158/14 "2020-12-23T13:10:57Z")

</div>

@JeffreySarnoff recently posted about GFlops.jl ([https://github.com/triscale-innov/GFlops.jl](https://github.com/triscale-innov/GFlops.jl)) might not be exactly what you want but I hadn’t seen this package before and it looks pretty solid and useful for diagnosing issues.

---

<div class="post-metadata">

### Author: ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)
#### Post date: [December 23, 2020, 2:29pm UTC](https://discourse.julialang.org/t/track-memory-usage/52158/15 "2020-12-23T14:29:28Z")

</div>

Hi, I avoid `OutOfMemory` errors with the function described [here](https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575/4). This works for me on a Linux machine. I frequently call it (in my case in JuMP callbacks), and abort my program if the memory consumption reaches my predefined limit. You can, however, clearly write the return values to a \*.log file.  
Note that the function returns the current memory usage of your program, not of specific variables. Maybe this helps anyway.
