mrkn / pycall.rb

Calling Python functions from the Ruby language
MIT License
1.05k stars 72 forks source link

Memory leak with numpy #167

Closed mknkmyzk closed 1 year ago

mknkmyzk commented 1 year ago

Thank you for merging my memory leak patch. However, there remains one more memory leak with numpy. e.g.

require 'pycall/import'
include PyCall::Import

pyimport :gc
# require 'numpy'
# np = Numpy
pyimport :numpy, as: :np

zzz = np.zeros(1000000)
10000.times{
  zzz[zzz == 255] = 0   # mem leak !!
  zzz > 100             # mem leak !!
  gc.collect()
  GC.start
}

The following is my tentative patch:

--- pycall.c.ORG    2023-08-25 12:33:56.702026311 +0900
+++ pycall.c.NEW    2023-08-25 12:57:10.719879875 +0900
@@ -793,7 +793,10 @@ pycall_libpython_helpers_m_compare(VALUE
     pycall_pyerror_fetch_and_raise("PyObject_RichCompare in pycall_libpython_helpers_m_compare");
   }

-  return pycall_pyobject_to_ruby(res);
+  // return pycall_pyobject_to_ruby(res);
+  VALUE obj = pycall_pyobject_to_ruby(res);
+  pycall_Py_DecRef(res); // ADDED *** for numpy (e.g. np.array([1,2,3]) < 1)
+  return obj;
 }

 static int is_pyobject_wrapper(VALUE obj);
mrkn commented 1 year ago

@mknkmyzk Thank you for your report. Could you make this patch a pull-request?