# Stopping code if a given RAM limit is reached

**URL:** https://discourse.julialang.org/t/stopping-code-if-a-given-ram-limit-is-reached/18593
**Category:** General Usage
**Tags:** memory
**Created:** [December 12, 2018, 1:28pm UTC](https://discourse.julialang.org/t/stopping-code-if-a-given-ram-limit-is-reached/18593 "2018-12-12T13:28:04Z")
**Posts on this page:** 1
**Showing post:** 9

<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 19, 2018, 5:24pm UTC](https://discourse.julialang.org/t/stopping-code-if-a-given-ram-limit-is-reached/18593/9 "2018-12-19T17:24:34Z")

</div>

Thank you for your comments. I found a solution which works fine for me on Linux (Ubuntu 18.04), however, it could be possibly improved. The main idea is to access the file /proc/self/stat , and read the actual memory consumption from that file. E.g.,

```julia

function get_mem_use()
    f = open( "/proc/self/stat" )
    s = read( f, String )
    vsize = parse( Int64, split( s )[23] )
    mb = Int( ceil( vsize / ( 1024 * 1024 ) ) )
    return mb
end

```

returns the virtual memory size in MB (see also: [here](http://man7.org/linux/man-pages/man5/proc.5.html)). It would be nice to call that function on a regular basis in background which I did not achieve yet. However, calling it at critical points in the code is a nice work-around.

---

_[View the full topic](https://discourse.julialang.org/t/stopping-code-if-a-given-ram-limit-is-reached/18593)._
