yuce / pyswip

PySwip is a Python - SWI-Prolog bridge enabling to query SWI-Prolog in your Python programs. It features an (incomplete) SWI-Prolog foreign language interface, a utility class that makes it easy querying with Prolog and also a Pythonic interface.
https://pyswip.org
MIT License
472 stars 97 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.