yuce / pyswip

PySwip is a Python-Prolog interface that enables querying SWI-Prolog in your Python programs.
https://pyswip.org
MIT License
481 stars 98 forks source link

Memory Leak in pythonic interface #119

Open zwilling opened 3 years ago

zwilling commented 3 years ago

Dear maintainers,

there seems to be a memory leak when using the pythonic interface.

Running the following script similar to the readme example

from pyswip import Functor, Variable, Query, call
import time

assertz = Functor("assertz", 1)
father = Functor("father", 2)
call(assertz(father("michael","john")))
call(assertz(father("michael","gina")))

start = time.time()
while time.time() < start + 20:
    X = Variable()
    q = Query(father("michael",X))
    while q.nextSolution():
        print("Hello,", X.value, time.time())
    q.closeQuery()

leads to this memory usage (measured with mprof run pyswip-memleak.py): pythonic-memleak

I am using pyswip==0.2.10 with SWI-Prolog version 7.7.10 and Python 3.5 on Ubuntu 18.04.

It would be great if you can fix this because the pythonic interface allows simple processing of the solution compared to string queries which have to be parsed first. Lio would be thankful for your help: Lio

zwilling commented 3 years ago

From looking at the code, I am wondering if the Atoms allocated by PL_new_atom = _lib.PL_new_atom need to be freed again.