Memory usage

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?

1 Like

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 :

 --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().

1 Like

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.

3 Likes

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).