GPU memory not freed when using OpenCV DNN
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).
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.
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.
It's now possible to call DnnModel.close(). It will also be possible to call StarDist2D.close() in the next release of the extension.