bcdev / jpy

A bi-directional Python-Java bridge used to embed Java in CPython or the other way round.
Apache License 2.0
187 stars 37 forks source link

Memory Leak - ExecuteCode Expression #149

Open seblb opened 6 years ago

seblb commented 6 years ago

Hello, I am currently hitting a memory leak with jpy 0.8 (I haven't tried w/ 0.9 yet) on MacOS 64b. Following code creates dict object and attempts to release/collect them. Java side manages objects fine (based on jvisualvm), but the dict seem to stick around forever on the Python side

        Properties properties = new Properties();
        properties.load(new FileInputStream(jpyconfig));
        properties.forEach((k, v) -> System.setProperty((String) k, (String) v));

        if (!PyLib.isPythonRunning()) {
            PyLib.startPython();
        }

        PyLib.Diag.setFlags(PyLib.Diag.F_ALL);

        int count = 0;

        while (true) {
            PyObject pyo = PyObject.executeCode("{}", PyInputMode.EXPRESSION);
            pyo = null;

            count++;
            if (count % 100000 == 0) {
                System.gc();
            }
        }

The memory logs show:

Java_org_jpy_PyLib_incRef: pyObject=0x1277475c8, refCount=1, type='dict'
Java_org_jpy_PyLib_incRef: pyObject=0x1277476e0, refCount=1, type='dict'
Java_org_jpy_PyLib_incRef: pyObject=0x1277477f8, refCount=1, type='dict'
Java_org_jpy_PyLib_decRef: pyObject=0x1277447f8, refCount=2, type='dict'
Java_org_jpy_PyLib_decRef: pyObject=0x1277477f8, refCount=2, type='dict'
Java_org_jpy_PyLib_decRef: pyObject=0x1277476e0, refCount=2, type='dict'

any thought if this is a bug / any work around? Thank you! Sebastien