Skip to content

GC stuck after running multithread code with GC.enable(false) #60448

@songjhaha

Description

@songjhaha

versioninfo:

julia> versioninfo()
Julia Version 1.10.10
Commit 95f30e51f4 (2025-06-27 09:51 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 12 × 11th Gen Intel(R) Core(TM) i5-11500H @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, tigerlake)
Threads: 6 default, 0 interactive, 3 GC (on 12 virtual cores)

RAM: 32GB

MWE:

running this code with julia -t 6

using Base.Threads
using LinearAlgebra

function mcross!(a::AbstractArray{TA,N}, b::AbstractArray{TA,N}, dim) where {TA,N}
    MCROSS_FUNC = (a, b, c, d) -> a * b - c * d
    c = zeros(size(a))

    c1 = selectdim(c, dim, 1)
    c2 = selectdim(c, dim, 2)
    c3 = selectdim(c, dim, 3)

    a1 = selectdim(a, dim, 1)
    a2 = selectdim(a, dim, 2)
    a3 = selectdim(a, dim, 3)

    b1 = selectdim(b, dim, 1)
    b2 = selectdim(b, dim, 2)
    b3 = selectdim(b, dim, 3)

    broadcast!(MCROSS_FUNC, c3, a1, b2, a2, b1)
    broadcast!(MCROSS_FUNC, c1, a2, b3, a3, b2)
    broadcast!(MCROSS_FUNC, c2, a3, b1, a1, b3)

    return c
end

function test_threadeds(
    p0, p1, p2,
    n_idc::Int, n_node::Int, n_m::Int,
    data,
    node_BRF, R_BRF,
    idcs::Vector{Int},
)

    ret1 = zeros(n_idc, n_node, 3)
    ret2 = zeros(n_idc, n_node, 3)
    GC.enable(false)
    @threads for i in 1:n_idc
        j = idcs[i]
        mm = rand(3,3)

        nt = repeat(p1[j, 1:3]', 1, 1)
        nn = repeat(p2[j, 1:3]', n_node, 1)

        pp = node_BRF * mm'

        nrr = mcross!(nn, pp, 2)

        nr = nt .+ nrr

        nf = zeros(n_node, 3)

        for k in 1:n_m
            tt = @views R_BRF[:, :, k]
            # nf += data[j, k] * (tt * mm') 
            rand(3000)
        end

        ret1[i, :, :] = nr
        ret2[i, :, :] = (nf + nr) * mm'
    end
    GC.enable(true)
    return ret1, ret2
end

N = 210000

p0 = rand(N, 3)
p1 = rand(N, 3)
p2 = rand(N, 3)

data = rand(N, 2545)

node_BRF = rand(26355, 3)
R_BRF = rand(26355, 3, 2545)

idcs = collect(Int, 1:2048)

n_idc = 120
n_node = 26355
n_m = 2545

GC.enable_logging(true)

@threads for i = 1:Threads.nthreads()
    GC.gc(true)
end
@timev @eval test_threadeds(
    p0, p1, p2,
    n_idc, n_node, n_m,
    data,
    node_BRF, R_BRF,
    idcs
);
@info "finish computation. start running gc"
    GC.gc(true)
@info "finish gc"
nothing

the gc is disable during computation just to simulation some cases.

My REPL would stuck for a long time after finish computation and try to run GC.

Metadata

Metadata

Assignees

No one assigned

    Labels

    GCGarbage collectormultithreadingBase.Threads and related functionalityneeds more infoClarification or a reproducible example is required

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions