silx icon indicating copy to clipboard operation
silx copied to clipboard

GPU Memory Leak

Open weichengkai opened this issue 4 years ago • 5 comments

Hi, After running the following code,GPU memory leak. silx version=0.13.2

import numpy
import silx.opencl.sift as sift

def siftTest(image):
    gpuSiftPlan = sift.SiftPlan(template=image, devicetype="GPU")
    gpuSiftPlan.keypoints(image)

image = numpy.zeros((100, 100, 3), dtype=numpy.uint8)
for i in range(500000):
    siftTest(image)
    print(i)

weichengkai avatar Nov 10 '20 07:11 weichengkai

Hi @weichengkai

The purpose of a SiftPlan is to be created once for a given image shape, and then to be used multiple times over different images. It is similar to the "FFTW plan" concept. Of course creating it each time will consume a lot of memory.

So instead, I would rather use:

import numpy
import silx.opencl.sift as sift

image = numpy.zeros((100, 100, 3), dtype=numpy.uint8)
gpuSiftPlan = sift.SiftPlan(template=image, devicetype="GPU")
for i in range(500000):
    gpuSiftPlan.keypoints(image)
    print(i)

pierrepaleo avatar Nov 10 '20 08:11 pierrepaleo

image = numpy.zeros((100, 100, 3), dtype=numpy.uint8) just for testing Actually, Images come from local files and come in different image shape. Function siftTest get a image and return image's keypoint.Like following code

def siftTest(image):
    gpuSiftPlan = sift.SiftPlan(template=image, devicetype="GPU")
    kps = gpuSiftPlan.keypoints(image)
    return kps

When I called the function siftTest many times, I found that GPU memory could not be released. What can I do to free memory before I return kps?

weichengkai avatar Nov 10 '20 08:11 weichengkai

If your image size differs, you will have to destroy the plan each time ...

kif avatar Nov 10 '20 10:11 kif

The plan instantiated within function siftTest,after the function runs, the plan is automatically destroyed.
The problem now is that GPU memory is not released after the plan is destroyed.

weichengkai avatar Nov 10 '20 10:11 weichengkai

pyopencl keeps the context in caches but it should recycle them. We could destroy the CommandQueue but it is not enough.

kif avatar Nov 24 '20 09:11 kif