clj-python / clj-python/libpython-clj

Consider releasing returned values from instance fns

Open
#242 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Clojure
Stars
1.2k
Forks
74
PR merge metrics
No merged PRs in 30d

Description

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 :-)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing make-instance-fn and how it is invoked from py/run-simple-string, then reproduce the dataset examples to observe retained memory. Determine how returned values are held in the normal JVM context and verify that Python reference handling can release them without manual System/gc calls. Done means repeated dataset access no longer accumulates those JVM references.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure, python
Domain
backend, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.