-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Large memory leak when using threads #31923
Comments
Did you check if this is actually a leak by calling GC? A memory leak would mean that it doesn't get freed, not that the behavior of the GC might want to keep it around for a bit longer for performance reasons. Here, since the GC is not multithreaded, I would assume that the GC tries to not run for as long as possible when threaded, but that doesn't mean that your memory will fill up and Julia will crash. Instead it'll wait quite a bit until it either has to GC or it can GC after the multithreading, which is the behavior you would want for performance. |
I tried: julia> versioninfo()
Julia Version 1.3.0-DEV.163
Commit c40d9b099c (2019-05-04 03:30 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: AMD Ryzen Threadripper 1950X 16-Core Processor
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-8.0.0 (ORCJIT, znver1)
Environment:
JULIA_NUM_THREADS = 16
julia> function foo(N)
Threads.@threads for i in 1:N
sum(collect(1:10^6))
end
end
foo (generic function with 1 method)
julia> @time foo(10^5)
46.325609 seconds (337.58 k allocations: 736.214 GiB, 15.02% gc time)
julia> @time foo(10^5)
42.238979 seconds (199.09 k allocations: 738.308 GiB, 9.36% gc time)
julia> GC.gc()
julia> @time foo(10^5)
42.090349 seconds (199.11 k allocations: 738.472 GiB, 8.85% gc time) So there seems to be a lot of GC activity. After the first run it used about 25G of RAM. After the last run, it was using over 32G. |
The issue is similar to the one in this SO question asked a while back. You do not need function f(n)
Threads.@threads for i=1:n
zeros(n)
end
end Try with large values like |
For me, adding a call to |
Line 44 in 9b5fea5
This is normal behavior (and also occurs on master). |
The segfault is normal. You should ignore it. |
I'm going to take a wild (uneducated) guess and say that somehow, the arrays getting allocated sometimes don't get rooted in the GC, and so effectively become lost (which is why an explicit Assuming the above theory is plausible, is there any documentation on how I could go about recording GC statistics over time, e.g. to confirm that every allocation is later deallocated? |
No. Rooting does exactly the opposite. |
Regarding whether the memory actually leaks: You need to test whether julia's memory use grows without bounds or reaches a steady state. With the default config, julia / linux / glibc is very bad at returning memory to the OS, probably due to heap fragmentation. However, the memory is still there and not leaked (glibc knows that the memory is free and will hand it out on Has anyone tried reproducing on windows or osx? |
This is incorrect; the GC does not wait for the end of |
I tried running #31923 (comment) on the 1.1 release binary and master and with 4 threads I see memory use holding steady at no more than about 3GB. On the second and third |
Is there any progress on this subject? I'm running into similar issues with a simulation of mine. @code_warntype doesn't show any type instabilities but memory consumption fluctuates a lot after a few iterations even on current 1. 1 release. |
I didn't manage to reduce the code, but I think it is related to this issue. When I run the benchmark for YaoArrayRegister defined here (with https://github.com/QuantumBFS/YaoArrayRegister.jl/blob/master/benchmark/benchmarks.jl with the following configuration BenchmarkConfig(
id="origin/multithreading",
env = Dict("JULIA_NUM_THREADS"=>4),
juliacmd=`julia -O3`
) I hit memory leak (but was fine on Mac OS), but if I run this with single thread ( julia> versioninfo()
Julia Version 1.1.1
Commit 55e36cc308 (2019-05-16 04:10 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.1 (ORCJIT, skylake) I tried this on both julia 1.1.1, and current master ( You should be able to reproduce this by running the following: ] dev YaoBase YaoArrayRegister BitBasis
] add StaticArrays LuxurySparse PkgBenchmark BenchmarkTools and using PkgBenchmark
benchmarkpkg("YaoArrayRegister", BenchmarkConfig(
id="origin/multithreading",
env = Dict("JULIA_NUM_THREADS"=>4),
juliacmd=`julia -O3`
)
"""
I'll try if I could find a reduced version, and post here later. |
I tried #31923 (comment) as well, got memory keep increasing on this machine as well. |
For those on linux experiencing this, try ccalling |
Doesn't seem to have an effect for me. Julia was using 26GB of RAM after running:
|
How much RAM does your system have? |
My system has 64GB of RAM. Every time I run the loop (without restarting Julia), it will end up with a different (seemingly random) amount of RAM used, so it does appear to be freeing memory sometimes. With this example at least, Julia's resident memory never exceeds ~32GB, so my system only starts swapping if other processes are using at least ~32GB. |
For me this seems to keep the memory leak in check (with some random RAM fluctuation still present). I notice however, that robsmith11's example seems to be running really slow
yields
whereas
yields
The test were run for |
Good news! Please try #32217. |
I've just tried your branch, but unfortunately I don't see a big difference. After a few seconds of running my example, I see some spikes up to 46GB of memory used.. EDIT: Sorry, I just realized I cloned the wrong branch.. let me retest EDIT2: Awesome! Now that I'm actually using your branch, memory stays under 512MB. Looks like that fixed it. :) |
Also works for me, both the above example as well as in my actual code. Many thanks! |
Great, that confirms this is a duplicate of #27173. |
@JeffBezanson, did you mean to link to #27173? That issue is still open and is about a data race not a memory leak. |
It's not actually a memory leak; it's growing memory use due to incorrect updating of the GC counters. |
Perhaps I'm a bit slow on the uptake today, but how is this fixed but #27173 isn't? |
It's not fixed yet; I closed this as a duplicate of it. |
With the following minimal example, I am seeing memory usage reach 10GB+ within seconds if I start Julia with
JULIA_NUM_THREADS=4
(no other options).If I only use single thread, memory usage remains around 200MB.
I've tested on both the 1.1 release and yesterday's nightly on a Linux Skylake Xeon.
The text was updated successfully, but these errors were encountered: