Explicitly invokes the garbage collector.


This predicate invokes the garbage collector to reclaim data structures in the heap that are no longer accessible to the computation.

No expansion of the heap is done, even if gc_margin kilobytes cannot be reclaimed (see ref-lps-flg-cha). This means that calls to this predicate are effective only when the heap contains a significant amount of garbage.

The cut may be used in conjunction with garbage_collect/0 to allow code that works in cycles and builds up large data structures to run for more cycles without running out of memory. The cut removes any alternatives that may be pending, thus potentially freeing up garbage that could not otherwise be collected.


In the code fragment:

     cycle(X) :- big_goal(X, X1), cycle(X1).

if cycle/1 is to run for a long time, and if big_goal/2 generates a lot of garbage, then rewrite the code like this:

     cycle(X) :- big_goal(X, X1), !, garbage_collect, cycle(X1).


Use of the !, garbage_collect idiom is only desirable when you notice that your code does frequent garbage collections. It will allow the garbage collector to collect garbage more effectively, and the cycle will run without demanding increasing amounts of memory.

See Also

gc/0, prolog_flag(gc_margin,_,_), nogc/0, statistics/2