# How to get current Julia process memory usage?

**URL:** <https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734>\
**Category:** General Usage\
**Created:** [June 19, 2020, 4:45pm UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734 "2020-06-19T16:45:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![yiyuezhuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiyuezhuo/32/27101_2.png) [@yiyuezhuo](https://discourse.julialang.org/u/yiyuezhuo)\
**Post date:** [June 19, 2020, 4:45pm UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/1 "2020-06-19T16:45:00Z")

</div>

I was debugging a `ccall` memory leak problem and found the memory usage value in `top` useful:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/3/939e0cc92dabd4b5ac0ef69fb1004f36ffb2f386.png)

Is there a direct method to access this value from Julia? I used `Sys.free_memory` as a proxy but it suffers from noise introduced by other processes.

---

<div class="post-metadata">

**Author:** ![yiyuezhuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiyuezhuo/32/27101_2.png) [@yiyuezhuo](https://discourse.julialang.org/u/yiyuezhuo)\
**Post date:** [June 19, 2020, 5:11pm UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/2 "2020-06-19T17:11:10Z")

</div>

Made a workaround:

```julia
out = read(`top -bn1 -p $(getpid())`, String)
split(split(out, "\n")[end-1])[6]

```

---

<div class="post-metadata">

**Author:** ![lbilli](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lbilli](https://discourse.julialang.org/u/lbilli)\
**Post date:** [June 19, 2020, 5:16pm UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/3 "2020-06-19T17:16:22Z")

</div>

In the same vein, you could also use `ps`:

```julia
julia> run(`ps -p $(getpid()) -o pid,comm,vsize,rss,size`);

```

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [June 19, 2020, 5:57pm UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/4 "2020-06-19T17:57:07Z")

</div>

Since you are using linux you might want to try reading `"/proc/$(getpid())/mstat"`. You can a full description of the fields from:  
[https://man7.org/linux/man-pages/man5/proc.5.html](https://man7.org/linux/man-pages/man5/proc.5.html)

From the page:

> Provides information about memory usage, measured in pages.  
> The columns are:
> 
> ```
> size (1) total program size
> (same as VmSize in /proc/[pid]/status)
> resident (2) resident set size
> (same as VmRSS in /proc/[pid]/status)
> shared (3) number of resident shared pages (i.e., backed by a file)
> (same as RssFile+RssShmem in /proc/[pid]/status)
> text (4) text (code)
> lib (5) library (unused since Linux 2.6; always 0)
> data (6) data + stack
> dt (7) dirty pages (unused since Linux 2.6; always 0)
> 
> ```

---

<div class="post-metadata">

**Author:** ![yiyuezhuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiyuezhuo/32/27101_2.png) [@yiyuezhuo](https://discourse.julialang.org/u/yiyuezhuo)\
**Post date:** [June 20, 2020, 12:45am UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/5 "2020-06-20T00:45:25Z")

</div>

pixel27 gives a solution without command:

```julia
open("/proc/$(getpid())/statm") do io
    split(read(io, String))[1]
end

```

But I still wonder if there is a cross platform solution so that memory leak checks can be written in the unit test / CI?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [June 20, 2020, 1:07am UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/6 "2020-06-20T01:07:08Z")

</div>

I believe it’s hard to determine programatically if there is a memory leak unless you are counting allocation and frees. Many of the allocation libraries allocate chunks of RAM from the OS then feed that out to the program in smaller chunks to improve allocation speed. So while the program is only actively using X amount of RAM Y amount (which is greater than X) will be allocated and I don’t think we as the end developer have much control over releasing that memory back to the system.

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [February 5, 2022, 11:53am UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/7 "2022-02-05T11:53:54Z")

</div>

Here’s at least a windows solution:

```julia
function memory()
    s = String(read(`tasklist /FI "PID eq $(getpid())" /NH /FO csv`))
    s = replace(s, "\"" => "")
    @info s
    m = strip(split(s, ",")[end])
    m = replace(m, "K" => "1000", "M" => "1000000", "G" => "1000000000")
    parse.(Float64, split(m)) |> prod |> Int
end

```

(I’m not sure, but it seems to me that Windows counts K as 1000 and not as 1024, etc.)

or this shorter one:

```julia
function memory()
    s = String(read(`tasklist /FI "PID eq $(getpid())" /NH /FO csv`))
    m = replace(match(r"""([^"]+)"[^"]+$""", s).captures[1],
        "K" => "1000", "M" => "1000000", "G" => "1000000000")
    parse.(Float64, split(m)) |> prod |> Int
end

```

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [December 14, 2022, 1:36pm UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/8 "2022-12-14T13:36:45Z")

</div>

> [@hhaensel](#):
>
> ```julia
> function memory()
> s = String(read(`tasklist /FI "PID eq $(getpid())" /NH /FO csv`))
> m = replace(match(r"""([^"]+)"[^"]+$""", s).captures[1],
> "K" => "1000", "M" => "1000000", "G" => "1000000000")
> parse.(Float64, split(m)) |> prod |> Int
> end
> 
> ```

Just was in need of tracking a memory leak and found out that ‘,’ are not decimal separators and should be discarded. Furthermore, it seems that only “K” appears as dimension. So my new version looks like this:

```julia
function memory()
    s = String(read(`tasklist /FI "PID eq $(getpid())" /NH /FO csv`))
    m = replace(match(r"""([^"]+)"[^"]+$""", s).captures[1],
        "K" => "1024", "M" => "1024 1024", "G" => "1024 1024 1024", r"\.|," => "")
    parse.(Int, split(m)) |> prod
end

```

EDIT: replace both, ‘,’ and ‘.’  
EDIT 2: replaced string by regex string

---

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [December 28, 2022, 7:24pm UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/9 "2022-12-28T19:24:31Z")

</div>

> [@hhaensel](#):
>
> ```julia
> function memory()
> s = String(read(`tasklist /FI "PID eq $(getpid())" /NH /FO csv`))
> m = replace(match(r"""([^"]+)"[^"]+$""", s).captures[1],
> "K" => "1000", "M" => "1000000", "G" => "1000000000")
> parse.(Float64, split(m)) |> prod |> Int
> end
> 
> ```

It does not work when I cut and past it on my REPL, Julia v1.8.4.

```julia
julia> function memory()
           s = String(read(`tasklist /FI "PID eq $(getpid())" /NH /FO csv`))
           m = replace(match(r"""([^"]+)"[^"]+$""", s).captures[1],
               "K" => "1024", "M" => "1024 1024", "G" => "1024 1024 1024", "\.|," => "")
ERROR: syntax: invalid escape sequence
Stacktrace:
 [1] top-level scope
   @ none:1

julia> parse.(Int, split(m)) |> prod
ERROR: UndefVarError: m not defined
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> end

```

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [December 29, 2022, 11:24am UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/10 "2022-12-29T11:24:54Z")

</div>

I forgot to add a regex string, sorry. I edited the post above which does work now.

Meanwhile I found that the tasklist command seems to always return answers in true KB.  
In that case, I’d propose to have memory always return the result in KB.

```julia
function memory()
    s = String(read(`tasklist /FI "PID eq $(getpid())" /NH /FO csv`))
    parse(Int, replace(match(r"""([^"]+)"[^"]+$""", s).captures[1], r"[.,K]" => ""))
end

```

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [October 22, 2024, 4:25pm UTC](https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/11 "2024-10-22T16:25:08Z")

</div>

Just needed this on a Docker where no ps is installed, so that information came in handy.  
I build a small function which also captures the pagesize and returns the memory size in Bytes or one of “KB”, “MB”, “GB”.

```julia
function memory_usage(unit::String = "Bytes")::Union{Float64, Int}
    exponents = Dict("Bytes" => 0, "Byte" => 0, "B" => 0, "KB" => 1, "MB" => 2, "GB" => 3)

    exponent = exponents[unit]
    pagesize = parse(Int, read(`getconf PAGESIZE`, String))
    meminfo = parse(Int, split(read(`cat /proc/$(getpid())/statm`, String))[1])
    memsize = exponent == 0 ? meminfo * pagesize : meminfo * pagesize / 1024^exponent

    return memsize
end

# example usage

memory_usage()
memory_usage("GB")

```
