# A clever way to find the available memory the host currently has free/available?

**URL:** <https://discourse.julialang.org/t/a-clever-way-to-find-the-available-memory-the-host-currently-has-free-available/36611>\
**Category:** General Usage\
**Tags:** shell, memory, dispatch\
**Created:** [March 27, 2020, 6:42pm UTC](https://discourse.julialang.org/t/a-clever-way-to-find-the-available-memory-the-host-currently-has-free-available/36611 "2020-03-27T18:42:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [March 27, 2020, 6:42pm UTC](https://discourse.julialang.org/t/a-clever-way-to-find-the-available-memory-the-host-currently-has-free-available/36611/1 "2020-03-27T18:42:08Z")

</div>

Writing a program which uses single files to large to fit in the memory of _normal_ desktops or laptops, I thought it would be nice to dispatch on the available memory relative to some _expected_ required memory. In case the available memory is not enough to fit the data and overhead, the function should dispatch and use load only the first couple of entries. This preview should be enough to write a analysis routine on a laptop. Once everything is figured out, the routine could be submitted to a HPC cluster.

I have [read the docs](https://docs.julialang.org/en/v1/manual/running-external-programs/) but need some clarification.  
Some thing similar to `vmstat -s | grep "free memory" | awk '{print $1 $2}'`  
I came up this

```julia
function getFreeMemory()
    run(pipeline(`vmstat -s`, `grep "free memory"`, `awk '{print $1}'`))
end

```

However, running: `freeMem = getFreeMemory()` prints the number and returns the ProcessChain:

> 8064912  
> Base.ProcessChain(Base.Process[Process(`vmstat -s`, ProcessExited(0)), Process(`grep 'free memory'`, ProcessExited(0)), Process(`awk '{print $1}'`, ProcessExited(0))], Base.DevNull(), Base.DevNull(), Base.DevNull())

How could I _catch_ the number (and discard the ProcessChain)?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 27, 2020, 7:31pm UTC](https://discourse.julialang.org/t/a-clever-way-to-find-the-available-memory-the-host-currently-has-free-available/36611/2 "2020-03-27T19:31:24Z")

</div>

[https://docs.julialang.org/en/v1/manual/running-external-programs/#Running-External-Programs-1](https://docs.julialang.org/en/v1/manual/running-external-programs/#Running-External-Programs-1)  
therefore you may use

```julia
function getFreeMemory()
    read(pipeline(`vmstat -s`, `grep "free memory"`, `awk '{print $1}'`),String)
end

```

Didn’t checked it, because I don’t have a linux shell currently. But I checked with a Cygwin bash this and it worked:

```julia
julia> function getFreeMemory()
           read(pipeline(`ls`, `grep "Dok"`),String)
       end
getFreeMemory (generic function with 1 method)

julia> getFreeMemory()
"Dokumente\n"

```

---

<div class="post-metadata">

**Author:** ![bcmichael](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcmichael/32/10003_2.png) [@bcmichael](https://discourse.julialang.org/u/bcmichael)\
**Post date:** [March 27, 2020, 8:17pm UTC](https://discourse.julialang.org/t/a-clever-way-to-find-the-available-memory-the-host-currently-has-free-available/36611/3 "2020-03-27T20:17:39Z")

</div>

Are you aware of the Base.Sys.free\_memory function? If I’m understanding what you’re trying to do that might be able to give you the information you need without needing to run a pipeline of external programs.

---

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [March 27, 2020, 8:55pm UTC](https://discourse.julialang.org/t/a-clever-way-to-find-the-available-memory-the-host-currently-has-free-available/36611/4 "2020-03-27T20:55:27Z")

</div>

Thanks. I did not know about it.  
[Here a previous discussion](https://discourse.julialang.org/t/how-to-check-how-free-ram-is-available/15288/2), [and the docs](https://docs.julialang.org/en/v1/base/base/#Base.Sys.free_memory).

---

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [March 27, 2020, 9:07pm UTC](https://discourse.julialang.org/t/a-clever-way-to-find-the-available-memory-the-host-currently-has-free-available/36611/5 "2020-03-27T21:07:11Z")

</div>

This works as well.  
For completeness:

```julia
parse(Int, read(pipeline(`vmstat -s`, `grep "free memory"`, `awk '{print $1}'`),String)[1:end-1])

```

I’ll stay with the `Base.Sys.free_memory` solution.
