# Overuse of memory leads to computer freeze

**URL:** https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671
**Category:** Performance
**Tags:** memory
**Created:** [June 10, 2021, 12:37pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671 "2021-06-10T12:37:41Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)
#### Post date: [June 10, 2021, 12:37pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/1 "2021-06-10T12:37:41Z")

</div>

I have a memory issue. On my small laptop (8Gb RAM) when I build overly big matrices I get a nice `OutOfMemory` error, the function is stopped and all is well.  
However on my desktop (~48Gb RAM) overly big matrices will take all the memory and then just freeze my computer. My only solution is then to force-reboot which is a painful process. Anyone has an idea what’s going on and how can I solve it (of course I try to solve the matrix problem on the side but the debugging is quite complicated in this context)

---

<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: [June 10, 2021, 12:50pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/2 "2021-06-10T12:50:43Z")

</div>

I asked a similar question a while ago and there’s a small check you could put into your code, for debugging purposes: [OutOfMemoryError() instead of allocating too much resources in the job scheduler](https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575)

---

<div class="post-metadata">

### Author: ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)
#### Post date: [June 10, 2021, 12:55pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/3 "2021-06-10T12:55:08Z")

</div>

Nice! My only problem is I think one matrix is enough to freeze my computer 😆.  
I would actually be happy with `OutOfMemoryError` instead of my computer freezing 😅

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [June 10, 2021, 1:26pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/4 "2021-06-10T13:26:11Z")

</div>

> [@theogf](#):
>
> My only solution is then to force-reboot which is a painful process.

If you are on Windows here you can use the Task Manager to kill the stalled process.

---

<div class="post-metadata">

### Author: ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)
#### Post date: [June 10, 2021, 1:35pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/5 "2021-06-10T13:35:08Z")

</div>

> [@joa-quim](#):
>
> If you are on Windows here you can use the Task Manager to kill the stalled process.

I am on Linux and everything is so slow that I cannot even access the virtual console (`Ctrl+Alt+F1`)

---

<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: [June 10, 2021, 1:44pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/6 "2021-06-10T13:44:22Z")

</div>

you could try running julia with a memory limit below the physical memory limit maybe? (i didn’t try that yet): [Limit memory usage for a single Linux process - Unix & Linux Stack Exchange](https://unix.stackexchange.com/a/125024)

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 10, 2021, 2:32pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/7 "2021-06-10T14:32:39Z")

</div>

What I do for my scientific experiments (in Linux) is:

```julia
sudo sh -c "echo 2 > /proc/sys/vm/overcommit_memory"
sudo sh -c "echo 95 > /proc/sys/vm/overcommit_ratio"

```

So the SO reserves 95% of the memory for userspace processes, and if a process tries to get these last 5% the allocation fails (instead of the SO letting it happen and, when the memory pages are really requested, calling the OOM reaper to kill the process with more memory allocated). For me, this is what enabled me to actually capture `OutOfMemory` exceptions in Julia and then skip the runs that are unfesible.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 10, 2021, 2:50pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/8 "2021-06-10T14:50:07Z")

</div>

> [@theogf](#):
>
> I am on Linux and everything is so slow that I cannot even access the virtual console ( `Ctrl+Alt+F1` )

How much swap do you have (and how much is used)? This sounds like you’re swapping your PC to death.

---

<div class="post-metadata">

### Author: ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)
#### Post date: [June 10, 2021, 2:58pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/9 "2021-06-10T14:58:03Z")

</div>

> [@Sukera](#):
>
> How much swap do you have (and how much is used)? This sounds like you’re swapping your PC to death.

I have 320GB on a non-SSD hard-disk, so that sounds like the issue 😆  
I did not imagine that using too much SWAP space would slow down my computer like that! But now that you mention it that makes sense

Thanks a lot!

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 10, 2021, 3:19pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/10 "2021-06-10T15:19:43Z")

</div>

Using too much swap can (if the wrong stuff gets swapped) slow down _everything_, since the CPU has to constantly fetch things from disk, stalling processes. 320GB _swap_ is most certainly waaay too much, don’t do that.

---

<div class="post-metadata">

### Author: ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)
#### Post date: [June 10, 2021, 3:26pm UTC](https://discourse.julialang.org/t/overuse-of-memory-leads-to-computer-freeze/62671/11 "2021-06-10T15:26:07Z")

</div>

> [@Sukera](#):
>
> Using too much swap can (if the wrong stuff gets swapped) slow down _everything_ , since the CPU has to constantly fetch things from disk, stalling processes. 320GB _swap_ is most certainly waaay too much, don’t do that.

yes that was definitely the problem, I reduced the swap size to 8GB and now I get a beautiful `OutOfMemory` error 😃  
You saved me hours of pain, because restarting a Julia session is slow but restarting the whole computer is even worse 😆  
Thank you so much!
