From jan@swi.psy.uva.nl  Fri Jun  9 13:46:12 2000
Received: from gollem.swi.psy.uva.nl (root@gollem [145.18.152.30])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id NAA25865;
	Fri, 9 Jun 2000 13:46:11 +0200 (MET DST)
Received: from localhost (localhost [[UNIX: localhost]])
	by gollem.swi.psy.uva.nl (8.9.3/8.9.3/SuSE Linux 8.9.3-0.1) id NAA10079;
	Fri, 9 Jun 2000 13:46:49 +0200
From: Jan Wielemaker <jan@swi.psy.uva.nl>
Organization: SWI, University of Amsterdam
To: Paul Sephton <paul@inet.co.za>, prolog@swi.psy.uva.nl
Subject: Re: Heap heaping up?
Date: Fri, 9 Jun 2000 13:37:10 +0200
X-Mailer: KMail [version 1.0.28]
Content-Type: text/plain
References: <Pine.LNX.3.91.1000609123534.11510A-100000@pdev.inet.co.za>
In-Reply-To: <Pine.LNX.3.91.1000609123534.11510A-100000@pdev.inet.co.za>
MIME-Version: 1.0
Message-Id: <00060913464900.09949@gollem>
Content-Transfer-Encoding: 8bit

On Fri, 09 Jun 2000, Paul Sephton wrote:
>When I run the following program, the heap just keeps getting bigger.  
>Does the garbage collector have a memory leak?

The garbage collector doesn't do anything to the heap, it only
affects the global- and trail-stacks.  Besides, the control-structure
is failure driven, so all data created on the stacks in each iteration
is destroyed anyhow on backtracking.

It seems there is a memory-leak in consult.  This doesn't really
surprise me.  Consult is for loading sources and it does a lot of
bookkeeping to deal with editing, re-consult, etc.  A small amount
of leakage isn't too bad as source-files are loaded only once, or
at most a few times during debugging.

In general, don't use consult/1 and friends for external storage
of terms.  Simply use read and write for this.  Reading a file is
easy:

read_file(File) :-
	open(File, read, Fd),
	read(Fd, Term),
	read_file(Term, Fd),
	close(Fd).

read_file(end_of_file, _) :- !.
read_file(Term, Fd) :-
	assert(Term),
	read(Fd, Term2),
	read_file(Term2, Fd).

This way you avoid loosing dynamic declarations, the overhead
of source-administration, macro-expansion, style-checking, etc.

	Regards --- Jan

>:- dynamic
>  myfact/2.
>
>factfile('myfacts.pl').
>
>change(_, _) :-
>  factfile(FName), not(exists_file(FName)),
>  tell(FName), nl, told, fail.
>change(_, _) :-
>  factfile(FName), consult(FName), fail.
>change(X, Y) :-
>  factfile(FName), retractall(myfact(X, _)), assert(myfact(X, Y)),
>  tell(FName), listing(myfact), told.
>
>go :-
>  between(1,300,X), between(1,300,Y),
>  change(Y, X),
>  garbage_collect,
>  statistics(heapused, HU),
>  writef("Heap Used: %w(%w)\n", [HU]), fail.
>go.

