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

Program and Kernel are leaking memory #24

Closed
vchuravy opened this issue Mar 17, 2014 · 7 comments
Closed

Program and Kernel are leaking memory #24

vchuravy opened this issue Mar 17, 2014 · 7 comments

Comments

@vchuravy
Copy link
Member

This issue is slightly different from #21 and to minimize the confusion I decided to open up a second issue.

The following code leaks memory:
Note: The Buffer creation is only there to make it abundantly clear when you look at the memory output.

import OpenCL; const cl = OpenCL
size = 1024
function leaky()
          device, ctx, queue = cl.create_compute_context()
          # Allocate buffer
          a = cl.Buffer(Float64, ctx, :rw, size*size)
          b = cl.Buffer(Float64, ctx, :rw, size*size)
          out = cl.Buffer(Float64, ctx, :rw, size*size)
          p = cl.Program(ctx, source=simpleKernel) |> cl.build!
          return nothing
end

function leak()
    for i in 1:1000
       leaky()
    end
end


simpleKernel =  "
        #define number float

        __kernel void add(
                      __global const number *a,
                      __global const number *b,
                      __global number *out) {

        int i = get_global_id(0);

        out[i] = a[i] + b[i];
    }
"

Substituting leaky with one of the following fixes the memory leak

function leaky()
          ....
          p = cl.Program(ctx, source=simpleKernel)
          return nothing
end

or

function leaky()
          ...
          p = cl.Program(ctx, source=simpleKernel) |> cl.build!
          cl.release!(p)
          return nothing
end

Adding a Kernel into the mix creates the leak again:

function leaky()
          ...
          p = cl.Program(ctx, source=simpleKernel) |> cl.build!
          k = cl.Kernel(p, "add")
          cl.release!(p)
          return nothing
end

and manually releasing the Kernel fixes the memory leak

function leaky()
          ...
          p = cl.Program(ctx, source=simpleKernel) |> cl.build!
          k = cl.Kernel(p, "add")
          cl.release!(k)
          cl.release!(p)
          return nothing
end

My intuition is that in this case we don't have the problem of #21 where we retain too often, but something different going on.

Please note that the last example will leak memory without the changes in 721c962 .

Since the memory leak only appears for me when the program is build I thought that maybe clProgramBuild increase the ref count by one, but issuing a second clReleaseProgram when the program is build resulted in a segmentation fault and since the manual release works it might have something to do with the order you release program.

@jakebolewski
Copy link
Member

From that exellent overview I think https://github.com/jakebolewski/OpenCL.jl/blob/master/src/kernel.jl#L44 needs to be set to false. I'm not around to test, does this fix the leak?

@vchuravy
Copy link
Member Author

@jakebolewski That is the change I introduced in 721c962 similar to what I did yesterday for context.

@jakebolewski
Copy link
Member

...which I obviously didn't read carefully enough ;-) I'm almost out of suggestions, I'll have to think on this one a bit. Does forcing a Base.gc() every iteration change the behavior you are seeing?

@vchuravy
Copy link
Member Author

Yes it does. Calling the gc() causes the memory leak to disappear.

function leaky()
         ....
          p = cl.Program(ctx, source=simpleKernel) |> cl.build!
          k = cl.Kernel(p, "add")
          gc()
          return nothing
end

@jakebolewski
Copy link
Member

Ok, what about forcing a gc() every Nth iteration? Are all objects gc'd? What about forcing a gc() just before the end? If the leak goes away this has something to do with the libraries interaction with Julia's garbage collector and I'll have to punt on that.

@vchuravy
Copy link
Member Author

The smaller the step size I call the gc in the less memory leak occurs...

  • Stepsize = 100: No noticeable leak
  • Stepsize = 150: Small but noticeable leak
    Also the example I gave on [WIP] memory leak #22 about array copying leaks in the same way as described here.

So #22 fixed the problems I had in my application where I first encountered the memory leaks. The issue here is not that likely to appear in the wild since you gotta do pretty weird stuff that warrant creating programs and kernels in a tight loop.

@vchuravy
Copy link
Member Author

closed in favor of #46

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

2 participants