qupath icon indicating copy to clipboard operation
qupath copied to clipboard

GPU memory not freed when using OpenCV DNN

Open petebankhead opened this issue 4 years ago • 2 comments

Bug report

Describe the bug GPU memory isn't released when using a DnnModel repeatedly. This can be seen when using StarDist.

To Reproduce See https://github.com/qupath/qupath-extension-stardist/issues/11 Copied here because the fix may need to be implemented within the core, rather than the StarDist extension.

Expected behavior GPU memory released after processing is complete.

Desktop (please complete the following information):

  • OS: Likely any with CUDA (Windows, Linux)
  • QuPath v0.3.0

Additional context I guess a blob may need to be reset... or something. Although that may be tricky to achieve if allowing multithreading and trying not to unduly harm performance (or perhaps it won't be).

petebankhead avatar Nov 09 '21 16:11 petebankhead

Calling Net.close() is reported to fix this... although it's still not entirely obvious when or how this should be done.

One option could be to use a Cleaner.

If this works, it shouldn't require any API change and so could be included in v0.3.1.

petebankhead avatar Nov 10 '21 07:11 petebankhead

Looking into this some more, it is more awkward than I had realised.

I had thought that wrapping everything in a PointerScope would help, by ensuring that the Net was closed properly without further effort - however the use if Net.retainReference() thwarts this.

Calling System.gc() before/after using a DNN can help patch over the problem, however a better solution is likely to require a more thorough revision of OpenCVDnn - possibly by updating DnnModel to implement AutoCloseable.

This makes it more of a v0.4.0 issue than a v0.3.1 issue.

petebankhead avatar Nov 15 '21 19:11 petebankhead

It's now possible to call DnnModel.close(). It will also be possible to call StarDist2D.close() in the next release of the extension.

petebankhead avatar Dec 03 '22 06:12 petebankhead