# Stack size limit with findall

**URL:** <https://swi-prolog.discourse.group/t/stack-size-limit-with-findall/2835>\
**Category:** Help!\
**Created:** [August 27, 2020, 9:02pm UTC](https://swi-prolog.discourse.group/t/stack-size-limit-with-findall/2835 "2020-08-27T21:02:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kassepro](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kassepro](https://swi-prolog.discourse.group/u/kassepro)\
**Post date:** [August 27, 2020, 9:02pm UTC](https://swi-prolog.discourse.group/t/stack-size-limit-with-findall/2835/1 "2020-08-27T21:02:32Z")

</div>

I’m using: SWI-Prolog version 8.2.1  
I have 16GB RAM ubuntu 20  
I want the code to:  
When i call in the first i don’t hav any error i make a query with findall at secondi have this error :  
But what I’m getting is:  
[Thread httpd@8082\_1] POST /prove: [503] Stack limit (1.0Gb) exceeded  
Stack sizes: local: 50Kb, global: 38.3Mb, trail: 1Kb  
Stack depth: 27, last-call: 33%, Choice points: 141  
In:  
[27] system:’$add\_findall\_bag’([length:2])  
[26] ‘$bags’:findall\_loop([length:2], ‘\<garbage\_collected\>’, \_572, [])  
[25] system:setup\_call\_catcher\_cleanup(\<compound (:)/2\>, \<compound (:)/2\>, \_602, \<compound (:)/2\>)  
[21] ‘$bags’:bagof(’\<garbage\_collected\>’, ‘\<garbage\_collected\>’, \_644)  
[20] user:my\_findall(’\<garbage\_collected\>’, ‘\<garbage\_collected\>’, \_666)

```
    Use the --stack_limit=size[KMG] command line option or
    ?- set_prolog_flag(stack_limit, 2_147_483_648). to double the limit.

```

set\_prolog\_flag(stack\_limit, 2\_147\_483\_648) don’t resole the probleme  
My code looks like this:

```prolog
   findall(Names,call(Term),ProveResultList),

```

---

<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:** [August 27, 2020, 9:16pm UTC](https://swi-prolog.discourse.group/t/stack-size-limit-with-findall/2835/2 "2020-08-27T21:16:17Z")

</div>

Can you provide a [Minimal and reproducible working example](https://swi-prolog.discourse.group/t/minimal-and-reproducible-working-examples/2447)

* * *

Also

```prolog
 findall(Names,call(Term),ProveResultList),

```

looks wrong. I would expect the `Template` to have a variable from the `Goal`. See findall/3.

---

<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:** [August 28, 2020, 6:44am UTC](https://swi-prolog.discourse.group/t/stack-size-limit-with-findall/2835/4 "2020-08-28T06:44:58Z")

</div>

findall/3 execution doesn’t use much stack as it is backtracking, but at the end it needs to put all solutions in a list. While collecting the answers it computes how much stack it will need in the end and if that exceeds the limit it will abort with a stack overflow while the stack itself still seems small (as the first post indicates).

What to do depends on what is called. Possibly just enlarging the stacks enough fixes the problem. Possibly there is a mistake in your program and the number of solutions is infinite or simply far too large. Possibly the result set is just very big and you really want the result with limited resources. In that case you need some streaming way to process the answers. What and how depends on the overall scenario. If the findall/3 call collects answers to send back as JSON this is unlikely to help as the browser will also try to read the entire JSON term and thus also run out of memory.

---

<div class="post-metadata">

**Author:** ![kassepro](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kassepro](https://swi-prolog.discourse.group/u/kassepro)\
**Post date:** [August 28, 2020, 7:29am UTC](https://swi-prolog.discourse.group/t/stack-size-limit-with-findall/2835/5 "2020-08-28T07:29:42Z")

</div>

But during my first call I receive the data certainly there is a lot anyway but during the second call that’s when the memory problem arises.  
` findall(Names,limit(4,Term),ProveResultList),`

With the first two calls I get an answer

---

<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:** [August 28, 2020, 8:03am UTC](https://swi-prolog.discourse.group/t/stack-size-limit-with-findall/2835/6 "2020-08-28T08:03:19Z")

</div>

Who knows … If these are exactly the same calls I see two likely options: your call has side effects (assert, etc) that make the next behave differently or you are close to the limit. As you get close to the stack limit the system may sometimes signal an overflow and sometimes not, depending on the history.

Otherwise we have very little to go on. It runs out of stack in findall/3. We know it calls _Term_, but have no clue what that is, nor what the program called by Term does. For debugging you can also call your mystery Term on the interactive session and see how many solutions it has and how big it is (see length/2 and term\_size/2).

Note that if you use the Prolog flag `stack_limit` you must set it **before** starting the HTTP server or e.g., using `swipl --stack-limit=4g`. Setting it later doesn’t have any effect as the stack limit is set for the calling thread and threads created from it and the HTTP threads have already been created.

---

<div class="post-metadata">

**Author:** ![kassepro](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kassepro](https://swi-prolog.discourse.group/u/kassepro)\
**Post date:** [August 31, 2020, 7:13am UTC](https://swi-prolog.discourse.group/t/stack-size-limit-with-findall/2835/7 "2020-08-31T07:13:38Z")

</div>

The probleme is before calling `findall` i doing some `assert` that way we have the error now after the assert i `retract` all `assert` .

THanks .
