# OutOfMemoryError() instead of allocating too much resources in the job scheduler

**URL:** https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575
**Category:** General Usage
**Created:** [August 26, 2020, 1:15pm UTC](https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575 "2020-08-26T13:15:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)
#### Post date: [August 26, 2020, 1:15pm UTC](https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575/1 "2020-08-26T13:15:23Z")

</div>

Hi everyone,  
I’ve encountered a few times that my code running interactively on an LSF cluster reaches the maximum of the (by me) allocated memory. This memory is not the memory limit of the node and instead of Julia throwing an `OutOfMemoryError()`, as it would if the whole node is allocated or it was running on a desktop machine, the job scheduler terminates the job due to excessive memory allocation.

Is there a way to feed a maximum memory into Julia, e.g. an environment variable `JULIA_MAX_MEMORY` in analogy to `JULIA_NUM_THREADS`, to avoid a termination of the job?

Thanks for any hints.

---

<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: [August 26, 2020, 1:35pm UTC](https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575/2 "2020-08-26T13:35:22Z")

</div>

I do this as I described in an old post [here](https://discourse.julialang.org/t/stopping-code-if-a-given-ram-limit-is-reached/18593/9). In addition just add something like

```julia
# MEMORY CONSUMPTION TEST
function memOK( mb::Int, memlimit::Int )
    if( mb > memlimit )
        println("### Memory-usage too high: $mb (MB)")
        return false
    else
        return true
    end
end

```

and terminate your program if the latter function returns `false`.

---

<div class="post-metadata">

### Author: ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)
#### Post date: [August 26, 2020, 1:44pm UTC](https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575/3 "2020-08-26T13:44:37Z")

</div>

Thanks for the reference, I didn’t find that. This is already interesting and I will check if it makes sense to use it in my case 🙂

---

<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: [August 26, 2020, 2:06pm UTC](https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575/4 "2020-08-26T14:06:16Z")

</div>

Wait, I found a mistake in the reference I’ve linked. You need to close the file in the referenced function. Otherwise you may get an error message like “too many files open” if you call that function very often. For convenience, here is the repaired version:

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

```

I use this mainly in callbacks in JuMP.jl. Clearly, you cannot avoid all `OutOfMemoryError()` with this method if you put your “julia memlimit” too close to you physical limit. However, for me it works fine and I really have this errors very rarely now.

---

<div class="post-metadata">

### Author: ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)
#### Post date: [June 8, 2022, 8:18pm UTC](https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575/5 "2022-06-08T20:18:12Z")

</div>

There is no such directory of file.

So, how can that ever work?

---

<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: [June 9, 2022, 7:27am UTC](https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575/6 "2022-06-09T07:27:27Z")

</div>

This works on Linux, see [here](https://man7.org/linux/man-pages/man5/proc.5.html).
