Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Gpu memory leak when resizing asynchronously

I'm facing some problem with gpu resize using opencv. Here is my code:

#define MX 500
#define ASYNC 0

class job {
public:
    cv::cuda::GpuMat gpuImage;
    cv::cuda::Stream stream;
    cv::Mat cpuImage;

    ~job() {
        printf("job deleted\n");
    }
};

void onComplete(int status, void* uData) {
    job* _job = (job*) uData;
    delete _job;
}

void resize(job* _job, vector<uchar> buffer) {
    _job->cpuImage = cv::imdecode(buffer, cv::IMREAD_COLOR);
    if (ASYNC) {
        _job->gpuImage.upload(_job->cpuImage, _job->stream);
        cv::cuda::resize(_job->gpuImage, _job->gpuImage, cv::Size(100, 100), 0, 0, cv::INTER_NEAREST, _job->stream);
        _job->gpuImage.download(_job->cpuImage, _job->stream);
        _job->stream.enqueueHostCallback(onComplete, _job);
        // _job->stream.waitForCompletion();
    } else {
        _job->gpuImage.upload(_job->cpuImage);
        cv::cuda::resize(_job->gpuImage, _job->gpuImage, cv::Size(100, 100), 0, 0, cv::INTER_NEAREST);
        _job->gpuImage.download(_job->cpuImage);
        delete _job;
    }

}

vector<uchar> readFile(string filename) {
    std::ifstream input(filename, std::ios::binary);
    std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input),{});
    return buffer;
}

int main() {
    for (int i = 0; i < MX; i++) {
        vector<uchar> buf = readFile("input.jpg");
        job* _job = new job();
        resize(_job, buf);
        printFreeGPUMemory();
    }
    while (true) {
        // wait
    }
    return 0;
}

When I run resize synchronously (ASYNC = 0), the code works perfectly fine. But when I run it asynchronously (ASYNC = 1), it seems that some gpu memory is lost somewhere despite the fact that I have deleted all created GpuMats and Streams. The more loop I run, the less free memory I have. is there a bug or part of my code is wrong?