Skip to content
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

Why the allocated memory keep increase in for loop #49761

Closed
crazyfireji opened this issue May 11, 2023 · 11 comments
Closed

Why the allocated memory keep increase in for loop #49761

crazyfireji opened this issue May 11, 2023 · 11 comments

Comments

@crazyfireji
Copy link

crazyfireji commented May 11, 2023

steps = 799500:100:800000

function main(nx, ny, nz, steps, Axi, Axj, Ayi, Ayj)

    hx = 1.0/(nx - 1); hy = 1/(ny - 1);

    for n in steps
		
       
        w=rand(nx,ny,nz)
        u=rand(nx,ny,nz)
        v=rand(nx,ny,nz)
        
        Tsb = 110.4 / T_inf

        Amu =@. 1.0 / Re * (1.0 + Tsb) * sqrt(T .^ 3)/ (Tsb + Tw)

        ux, uy, uz = get_grad(u, hx, hy, Axi, Axj, Ayi, Ayj, sz)
        vx, vy, vz = get_grad(v, hx, hy, Axi, Axj, Ayi, Ayj, sz)
        wx, wy, wz = get_grad(w, hx, hy, Axi, Axj, Ayi, Ayj, sz)

        Theta = ux + vy + wz

        Sigma11 = @. (ux*2 - 2/3*Theta) * Amu
        Sigma22 = @. (vy*2 - 2/3*Theta) * Amu
        Sigma33 = @. (wz*2 - 2/3*Theta) * Amu
        Sigma12 = @. (uy + vx) * Amu
        Sigma23 = @. (vz + wy) * Amu
        Sigma13 = @. (uz + wx) * Amu

        println("Write OCFD$(string(n,pad=7)).jld2")
				
        jldopen("$(filepath1)test$(string(n,pad=7))_sigma_theta.jld2","w") do f
            f["σ11"] = Sigma11
            f["σ22"] = Sigma22
            f["σ33"] = Sigma33
            f["σ12"] = Sigma12
            f["σ13"] = Sigma13
            f["σ23"] = Sigma23
            f["θ"] = Theta
        end
    end

end


@time main( nx, ny, nz, steps, Axi, Axj, Ayi, Ayj)

The memory allocated by Julia will keep increase in for loop. How to solve it, only by gc() manually? This fig shows the memory allocated when run this Julia code.
536fec9f26a3c3c4

@Seelengrab
Copy link
Contributor

Welcome! This is the issue tracker for the julia language, where bugs are reported. Please ask usage questions on https://discourse.julialang.org/.

@oscardssmith
Copy link
Member

IMO, this appears to be a gc bug

@Seelengrab
Copy link
Contributor

How can you tell? This is not a runnable example - FortranFile and flow3D_Once are not included, for example.

@mreppen
Copy link

mreppen commented May 12, 2023

FWIW, I have a similar issue since moving to 1.9, using only standard libraries. Adding GC.gc() to each loop resolves it.

@Seelengrab
Copy link
Contributor

If you have a reproducible example, please do share it! Without having a particular case to investigate, nothing can be really done.

@denglerchr
Copy link

I have another example where memory just keep stacking up (CNN training using FLux). In Julia 1.8, all is fine. A manual GC.gc() call cleans it up.

using Flux
import IterTools: ncycle

struct CNNModel
    cnnlayer1
    cnnlayer2
    poollayer
    dense
end
getparams(model::CNNModel) = Flux.params(model.cnnlayer1, model.cnnlayer2, model.dense)

struct Data
    input
    label
end

function evalmodel(input_CNN, model::CNNModel)
    temp = model.cnnlayer1(input_CNN)
    temp = model.cnnlayer2(temp)
    temp = model.poollayer(temp)
    temp = dropdims(temp; dims = (2, 4))
    temp = model.dense(permutedims(temp, (2, 1)))
    return vec(temp)
end

loss(data::Data, model) = Flux.logitcrossentropy( evalmodel(data.input, model), data.label )
loss(data::Vector{Data}, model) = Flux.mean(loss(d, model) for d in data)


X = [randn(500, 2, 1, 1) for i = 1:50]
Y = [vec( sum(X[i]; dims = 2)) for i = 1:50]
datavec = [ Data(X[i], Y[i]) for i in eachindex(X) ]

data_loader = Flux.Data.DataLoader(datavec, batchsize=20, shuffle=true);

cnnlayer1 = fmap(f64, Conv((128, 2), 1=>16, tanh; stride = 1, pad = (64, 1)))
cnnlayer2 = fmap(f64, Conv((16, 2), 16=>8, tanh; stride = 1, pad = (7, 0)))
cpooling = MeanPool((1, 2)) 
cdense = fmap(f64, Dense( 8=>1 ; bias = false))
model = CNNModel(cnnlayer1, cnnlayer2, cpooling, cdense)

function callback!(counter, Nbatches)
    counter[1]+=1
    if counter[1] > Nbatches
        counter[2] += 1
        counter[1] = 1
        println("Episode $(counter[2]) done")
    end
end

countervec = [0, 0]
Flux.train!(data->loss(data, model), getparams(model), ncycle(data_loader, 100), ADAM(); cb = ()->callback!(countervec, length(data_loader)))

# to clean up
# Main.GC.gc()

@denglerchr
Copy link

Forgot to mention, reduced example was tested on Windows 11.

@denglerchr
Copy link

This issue remains in 1.9.1 btw, at least for the example I provide (tested under Linux as well)

@KristofferC
Copy link
Member

Maybe similar to #49545.

@AtsushiSakai
Copy link
Contributor

I‘m sorry for not being able to provide the code, but I am having a similar issue with my application. While it works fine with Julia 1.8.
However, with Julia 1.9, the memory is not being released, and as time passes, the memory usage continues to increase.

oscardssmith added a commit that referenced this issue Jul 23, 2023
This PR implements GC heuristics based on the amount of pages allocated
instead of live objects like was done before.
The heuristic for new heap target is based on
https://dl.acm.org/doi/10.1145/3563323 (in summary it argues that the
heap target should have square root behaviour).
From my testing this fixes
#49545 and
#49761
KristofferC pushed a commit that referenced this issue Jul 24, 2023
This PR implements GC heuristics based on the amount of pages allocated
instead of live objects like was done before.
The heuristic for new heap target is based on
https://dl.acm.org/doi/10.1145/3563323 (in summary it argues that the
heap target should have square root behaviour).
From my testing this fixes
#49545 and
#49761

(cherry picked from commit 32aa29f)
@denglerchr
Copy link

Happy to report that at least for the Flux example I provided, the issue seems resolved with Julia v1.10.0-beta1.

d-netto pushed a commit to RelationalAI/julia that referenced this issue Oct 4, 2023
This PR implements GC heuristics based on the amount of pages allocated
instead of live objects like was done before.
The heuristic for new heap target is based on
https://dl.acm.org/doi/10.1145/3563323 (in summary it argues that the
heap target should have square root behaviour).
From my testing this fixes
JuliaLang#49545 and
JuliaLang#49761
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants