clj-python / libpython-clj

Python bindings for Clojure
Eclipse Public License 2.0
1.05k stars 68 forks source link

Consider releasing returned values from instance fns #242

Open cnuernber opened 1 year ago

cnuernber commented 1 year ago

This will collect memory automatically:

(py/run-simple-string "
import torch.utils.data as tud
import torch

class DebugDataset (tud.Dataset):
    def __init__(self):
        pass
    def __len__(self):
        return 1000
    def __getitem__ (self, i):
        return torch.rand(1000, 1000, 1000)

d = DebugDataset()
print(d[0])
print(d[0])")

If the user defines a dataset using clojure and this will cause issues unless the user spams (System/gc):

(let [globals! (py/module-dict (py/add-module "__main__"))]
  (.put globals! "DebugDatasetClj" DebugDataset))

(py/run-simple-string "
d = DebugDatasetClj()
print(d[0])
print(d[0])")

The issue is make-instance-fn runs in the normal jvm context so it is keeping jvm references to the produced items. It does not need to do this; the normal python reference keeping system will work just fine. Fixing this in a robust way will require some thought :-)