# Load, and Unload SWIPL Code from a DataBase like MySQL to clean/renew or hold the requierements of the Memory so small as possible, on a Deskop-Computer-System

**URL:** <https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223>\
**Category:** Request For Comments\
**Created:** [February 13, 2023, 10:16pm UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223 "2023-02-13T22:16:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![paule32](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/paule32/32/5112_2.png) [@paule32](https://swi-prolog.discourse.group/u/paule32)\
**Post date:** [February 13, 2023, 10:16pm UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223/1 "2023-02-13T22:16:37Z")

</div>

Hello,  
On bigger Projects, the resulting output executable can be very big for small Solutions.  
As this reason, I have look to an alternative, and found one of them:

- that I can load external Files, and execute them with Prolog Interpreter or the virtual zip maschine.
- and I could get the Code from a Data Base System like MySQL

But the question will be: Can I load such kind of Code into Memory, to unload it at later Time ?  
e.g. when the Code is never be relevant or is not need anymore ?

For some of the Programmers is this fact know a GC - gabarge collector.

Jens

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [February 13, 2023, 10:42pm UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223/2 "2023-02-13T22:42:28Z")

</div>

> On bigger Projects, the resulting output executable can be very big for small Solutions.

There should a topic or post on this site that talks about using tell/1 and told/0. The example would look something like

```prolog
?- tell('fiilename.pl'),goal(1,2,3),told.

```

such a file can then be loaded with IIRC include/1.

While that is very fast and effective, it is a manual process each time.

* * *

For something more persistent consider [library(persistency)](https://www.swi-prolog.org/pldoc/man?section=persistency). This requires a bit of learning up front and creating predicates for each clause you want to persist. The first time using it can be hard to grasp but I do use enough that I might be able to help.

* * *

For the real bleeding edge there is the [SWI-Prolog interface for RocksDB](https://github.com/JanWielemaker/rocksdb). I know of only 3 of us here that have used it and it is not even alpha quality, only works on Linux, not Windows unless you want to use WSL and is missing so many features beyond the basics that anything you do would be of interest to others here. I think it is great and hope it eventually makes it into the standard build or is at least a build option.

* * *

As for using an SQL database for persistence, I am not the one to ask. I do not like that way of solving the impedance mismatch problem. More commonly seen as [Object–relational impedance mismatch](https://en.wikipedia.org/wiki/Object%E2%80%93relational_impedance_mismatch)

HTH

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [February 13, 2023, 10:55pm UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223/3 "2023-02-13T22:55:07Z")

</div>

> [@EricGT](#):
>
> For the real bleeding edge there is the [SWI-Prolog interface for RocksDB](https://github.com/JanWielemaker/rocksdb).

If you do any work on this, please coordinate with me. I started extending it but got sidetracked into improving the C++ interface (SWI-cpp.h).  
I’m also wondering whether RocksDB or redis, or something else fits better with the use cases I’m envisaging - it might make sense to have an over-all interface that can work with a variety of database-ish “engines”.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [February 13, 2023, 11:23pm UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223/4 "2023-02-13T23:23:14Z")

</div>

> [@peter.ludemann](#):
>
> it might make sense to have an over-all interface that can work with a variety of database-ish “engines”.

Please don’t. I would hate to lose efficiency for the sake of a unified back end. SQL, Redis, RocksDB, RDF and network databases are each so different that it could wind up being the worst of all choices.

---

<div class="post-metadata">

**Author:** ![paule32](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/paule32/32/5112_2.png) [@paule32](https://swi-prolog.discourse.group/u/paule32)\
**Post date:** [February 14, 2023, 7:07am UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223/5 "2023-02-14T07:07:59Z")

</div>

Thank you for your feedback.

Please sorry, the Subject has a little Typo: “…big Projects…small Solutions”.  
This is Paradox/Type, so the Admins can replace “small Solutions” with empty gap.

I am not on the state, to foreign function calls (FFC).  
I have thinking about the PCE/XPCE to have a own Qt 5 or Delphi VCL like interface implementation.

Because the PCE is very limited (for me).  
But like i said: I have to learn more Basic’s about SWIPL.

Each cooperative work are welcome.

Thanks for reading.  
Jens

---

<div class="post-metadata">

**Author:** ![mgondan1](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/mgondan1/32/803_2.png) [@mgondan1](https://swi-prolog.discourse.group/u/mgondan1)\
**Post date:** [February 14, 2023, 7:59am UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223/6 "2023-02-14T07:59:19Z")

</div>

I can see rocksdb on msys2, what is the issue with Windows?

---

<div class="post-metadata">

**Author:** ![paule32](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/paule32/32/5112_2.png) [@paule32](https://swi-prolog.discourse.group/u/paule32)\
**Post date:** [February 14, 2023, 8:14am UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223/7 "2023-02-14T08:14:59Z")

</div>

For me, msys2 is not new to me.  
I use the the mingw32, and mingw64 very often with fpc.

But the data base RockDB is new to me.  
And I don’t know, if it has SQL facilities, so I don’t need learn C/C++ Library calls (okay, some background in it are useful. But it is not the actual problem in using SQL. Because nearly all SQL-Data base’s has SQL industrial standard).

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 14, 2023, 8:22am UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223/8 "2023-02-14T08:22:48Z")

</div>

Not sure what you are after. You can load code from anything that can be turned into a stream, which is basically any source. You can also unload code. For temporary reasoning there is `in_temporary_module/3`.

As is, there are a few objects that are not subject to GC. These are

- functors (name/arity pairs)
- predicates, unless in a temporary module. Note that the clauses are subject to GC, but the (small) structure that represents the predicate is not.
- modules, again unless it is a temporary one. They may be emptied, but the module structure with the (undefined) predicates it once held remain.

Possibly that will be improved in the future. Notable functors and atoms are united in some systems. We can probably GC modules and predicates if we wanted to.

If there is a lot of data, you can of course consider to use an external DB. Which one depends a lot on the data and what you want to do with it. Typically it is more hassle and slower than using predicates. Still, sometimes needed for scale, communication, persistency, features, etc.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [February 14, 2023, 8:46am UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223/9 "2023-02-14T08:46:45Z")

</div>

A post was split to a new topic: [Current status of SWI-Prolog installed using MSYS2](https://swi-prolog.discourse.group/t/current-status-of-swi-prolog-installed-using-msys2/6229)

---

<div class="post-metadata">

**Author:** ![paule32](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/paule32/32/5112_2.png) [@paule32](https://swi-prolog.discourse.group/u/paule32)\
**Post date:** [February 14, 2023, 8:44am UTC](https://swi-prolog.discourse.group/t/load-and-unload-swipl-code-from-a-database-like-mysql-to-clean-renew-or-hold-the-requierements-of-the-memory-so-small-as-possible-on-a-deskop-computer-system/6223/10 "2023-02-14T08:44:43Z")

</div>

I work under Windows, and can esily create .dll there like under Linux the .so files  
by simple command line:

```prolog
# gcc/g++ -fPIC -O2 -o libname[.dll | .so] objects.o -L/lib/path -lprolog -lc [-l std++]
# strip libname[.dll | .so]

```

The text in brackets are optional.  
Important gnu c/c++ option for DLL/SO files can be -fPIC for Program Independent Code.

amd: yes, you can move.
