Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symbols are stored globally and never freed. #203

Open
rkaminsk opened this issue Mar 20, 2020 · 10 comments
Open

Symbols are stored globally and never freed. #203

rkaminsk opened this issue Mar 20, 2020 · 10 comments
Assignees
Milestone

Comments

@rkaminsk
Copy link
Member

Clingo internalizes symbols in global hash tables to only allocate memory for the same symbol once. Currently, symbols are never removed form this hash table. When an application runs for a long time and generates a lot of different symbols, this can lead to memory problems.

Unused symbols should be freed. This could be done via some form of garbage collection or reference counting.

@rkaminsk rkaminsk added this to the unknown milestone Mar 20, 2020
@rkaminsk rkaminsk self-assigned this Mar 20, 2020
@chenkins
Copy link

Workaround for if you have the problem using multiprocessing.pool.Pool in Python: https://stackoverflow.com/questions/38294608/python-multiprocessing-pool-new-process-for-each-variable. Documentation: https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool

@rkaminsk
Copy link
Member Author

Workaround for if you have the problem using multiprocessing.pool.Pool in Python: https://stackoverflow.com/questions/38294608/python-multiprocessing-pool-new-process-for-each-variable. Documentation: https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool

Just be careful when exchanging data between the workers. The classes in the clingo API do not support pickling (and it would defy the attempt to safe memory). If you restrict yourself to python classes for communication, then these can be passed around easily and they will be freed when no longer referenced.

@0x326
Copy link
Contributor

0x326 commented Aug 6, 2020

@rkaminsk Are symbols freed when a clingo Control object is garbage-collected? E.g.:

import clingo


# Abstraction of long-running business logic
while True:
    clingo_control = clingo.Control()
    clingo_control.load(...)
    for _ in range(3):
        clingo_control.ground((
            ...
        ))
        for model in clingo_control.solve(yield_=True, async_=True):
            for symbol in model.symbols(shown=True):
                ...

@rkaminsk
Copy link
Member Author

rkaminsk commented Aug 6, 2020

@rkaminsk Are symbols freed when a clingo Control object is garbage-collected? E.g.:

No, they are never freed and actually shared between multiple control objects.

@0x326
Copy link
Contributor

0x326 commented Aug 6, 2020

While a garbage-collection/reference-counting approach of individual symbols is probably the ideal solution, is it possible count references to clingo.Control objects in the meantime and clear the hash table when all clingo.Control objects are garbage-collected by Python?

@rkaminsk
Copy link
Member Author

rkaminsk commented Aug 6, 2020

While a garbage-collection/reference-counting approach of individual symbols is probably the ideal solution, is it possible count references to clingo.Control objects in the meantime and clear the hash table when all clingo.Control objects are garbage-collected by Python?

It is not that easy. The python program might store some other symbol objects. I can have a look if I can provide a function to clear all symbols. But it would be dangerous to use...

@0x326
Copy link
Contributor

0x326 commented Aug 6, 2020

Hmm. It's probably better to be memory-safe than to provide a function that could result in a segfault. I think some form of memory management is going to be an essential feature for real-world applications, but I can look into using subprocesses in the meantime.

@kherud
Copy link

kherud commented Apr 12, 2024

Is there a way with the C/C++ API to do that? It would be really helpful to at least have a manual option, even if the Python binding doesn't support it.

@rkaminsk
Copy link
Member Author

The next major clingo release will give more control about storing symbols. For now there is no way to delete them - not even on the C++ level.

@sthiele
Copy link
Member

sthiele commented Apr 12, 2024

@rkaminsk That is really nice to read. You once mentioned that this feature has to be well planned. Can you elaborate on how you plan to do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants