# Memory usage

**URL:** https://discourse.julialang.org/t/memory-usage/109873
**Category:** New to Julia
**Tags:** memory-allocation
**Created:** [February 7, 2024, 1:50pm UTC](https://discourse.julialang.org/t/memory-usage/109873 "2024-02-07T13:50:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![emily](https://avatars.discourse-cdn.com/v4/letter/e/8c91f0/32.png) [@emily](https://discourse.julialang.org/u/emily)
#### Post date: [February 7, 2024, 1:50pm UTC](https://discourse.julialang.org/t/memory-usage/109873/1 "2024-02-07T13:50:01Z")

</div>

Hi!

I am trying to understand when memory allocations are cleared. When I run a problem requiring large memory use, I have discovered that it is not freed until I use the garbage collector (GC.gc()), or shut down the REPL. Take the following example:

function func(n)  
A = rand(n,n)  
nothing  
end  
func(20000)

I would assume that when execution is done, memory is freed. However, this is not the case. If I afterward change the input to something requiring much smaller allocations, e.g., func(200), high memory usage is still reported (in activity monitor and by htop (Mac user)). Is the only solution to run gc manually?

---

<div class="post-metadata">

### Author: ![AMJ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amj/32/214096_2.png) [@AMJ](https://discourse.julialang.org/u/AMJ)
#### Post date: [February 7, 2024, 1:58pm UTC](https://discourse.julialang.org/t/memory-usage/109873/2 "2024-02-07T13:58:57Z")

</div>

Hey, welcome to the community.  
Please consider putting your code between two ````` to make it more readable.

And as for you question, you can run Julia with `--heap-size-hint=<size> ` to make Julia’s gc more eager to act. More information can be seen with ` --help` :

```julia
 --heap-size-hint=<size> Forces garbage collection if memory usage is higher than that value.
                            The memory hint might be specified in megabytes(500M) or gigabytes(1G)

```

Also yes, the only solution to run gc manually is `GC.gc()`.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [February 7, 2024, 4:53pm UTC](https://discourse.julialang.org/t/memory-usage/109873/3 "2024-02-07T16:53:02Z")

</div>

Basically, the GC does not start to clear memory until it starts running out of memory. The option mentioned above describes the limit where this starts to occur.

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [February 7, 2024, 5:14pm UTC](https://discourse.julialang.org/t/memory-usage/109873/4 "2024-02-07T17:14:05Z")

</div>

I believe it’s very unusual to run GC manually, not something I would recommend worrying about. Rather avoid allocating if you can and need to do that. And conrol when allocated.

Often it’s no problem if Julia does use much memory. Note, if GC runs and you see lower number it’s likely NOT returned to the OS (I think maybe never, still not sure, will just be less memory to scan, still part of Julia’s virtual memory?), for other processes.

What triggers GC is only Julia “running out” or actually if it feels using too much, and this is implementation defined. In the future, this feeling could be more aggressive. In Java there are many different GC implementations to choose from. Julia could adopt a new one.

One new already added to Julia (but then it was dropped before 1.10 was released) is very intriguing, based on MemBalancer, which is very intriguing, because then if a different process, Julia or not (e.g. a web browser), can influence Julia to do GC.

Memory is a global property of the system, why this is a good and surprising development, that it can actually work (already implemented in GC by web browsers, and soon, hopefully, in Julia after bug fixed).
