# Memory accumulation on backtracking

**URL:** <https://swi-prolog.discourse.group/t/memory-accumulation-on-backtracking/7102>\
**Category:** General\
**Created:** [December 23, 2023, 9:00am UTC](https://swi-prolog.discourse.group/t/memory-accumulation-on-backtracking/7102 "2023-12-23T09:00:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![emiruz](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emiruz/32/4495_2.png) [@emiruz](https://swi-prolog.discourse.group/u/emiruz)\
**Post date:** [December 23, 2023, 9:00am UTC](https://swi-prolog.discourse.group/t/memory-accumulation-on-backtracking/7102/1 "2023-12-23T09:00:05Z")

</div>

I’m searching a large graph in a depth first fashion, and the memory usage accumulates until I’m out of memory. I don’t really understand why. Here is some simple code for a depth first search to a finite depth:

```prolog
walk(0, [Node|_], Node) :- !.
walk(N0, [Node|T], Final) :-
    N is N0 - 1,
    connect(Node, Next),
    \+ memberchk(Next, T),
    walk(N, [Next,Node|T], Final).

```

which I invoke like this, with the aim of counting all the final nodes:

```prolog
aggregate_all(count, Final, walk(64, [Start], Final), Count).

```

Why does it run out of memory? How can I write it so that it does not?

---

<div class="post-metadata">

**Author:** ![emiruz](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emiruz/32/4495_2.png) [@emiruz](https://swi-prolog.discourse.group/u/emiruz)\
**Post date:** [December 23, 2023, 9:22am UTC](https://swi-prolog.discourse.group/t/memory-accumulation-on-backtracking/7102/3 "2023-12-23T09:22:10Z")

</div>

Would you mind providing a short example how I could achieve a count of the unique final nodes using mode directed tabling?

An alternative is to just not use backtracking by having cuts all over the place and manage the stack myself, but that seems to defy the point of using Prolog…

---

<div class="post-metadata">

**Author:** ![emiruz](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emiruz/32/4495_2.png) [@emiruz](https://swi-prolog.discourse.group/u/emiruz)\
**Post date:** [December 23, 2023, 9:30am UTC](https://swi-prolog.discourse.group/t/memory-accumulation-on-backtracking/7102/5 "2023-12-23T09:30:00Z")

</div>

Ok, just managing the stack myself looks neater to be honest, so I’ll just do that. I also strongly suspect that tabling will explode the memory too.

---

<div class="post-metadata">

**Author:** ![emiruz](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emiruz/32/4495_2.png) [@emiruz](https://swi-prolog.discourse.group/u/emiruz)\
**Post date:** [December 23, 2023, 9:33am UTC](https://swi-prolog.discourse.group/t/memory-accumulation-on-backtracking/7102/7 "2023-12-23T09:33:30Z")

</div>

How would I do that with the walk predicate above to count unique final states? A final state is a function of the path that brought it to the final state. How would I avoid tabling paths in order not to explode the memory?

---

<div class="post-metadata">

**Author:** ![emiruz](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emiruz/32/4495_2.png) [@emiruz](https://swi-prolog.discourse.group/u/emiruz)\
**Post date:** [December 23, 2023, 9:41am UTC](https://swi-prolog.discourse.group/t/memory-accumulation-on-backtracking/7102/9 "2023-12-23T09:41:24Z")

</div>

Marvelous, thank you. I’ll try it.
