[GAP Forum] Memory management with GAP

Stephen Linton sal at mcs.st-andrews.ac.uk
Wed Mar 23 16:38:53 GMT 2011


Dear Mathieu,

In your example, a is not freed. It could be in principle, but the current code doesn't do that. 
In fact, as you may know, the small integer a uses effectively no memory, but if the value of a were some large
object, then it would be sensible to Unbind it before returning the record of functions.

There is a function MemoryUsage() which returns an approximation to the memory used by an object and all its subobjects, but you should be aware that these subobjects are often shared, so that (a) the total memory usage of a lot of objects may be less than the totals for the individual objects and (b) when an object is freed some of its subobjects may remain in use by other objects, so not all the memory reported by MemoryUsage will be recovered.

The "conservative" nature of the garbage collector refers to the fact that it may sometimes fail to free unreachable objects,  because other data is "mistaken" for pointers to it. Generally, such objects get freed a few garbage collections late.

A simple way to make a circular object in GAP is 

gap> a := [];
[ ]
gap> a[1] := a;
[ ~ ]

Here ~ indicates that the object at this position in the list is the topmost object being printed.

A more elaborate version might be:
gap> a := rec();;
gap> b := rec(link := a);;
gap> a.link := b;;

which creates exactly the circular object you describe.

However while these objects give problems for some GAP functions, they do not create problems for the garbage collector, which will correctly discard them if they are unreachable.

	Steve Linton

On 23 Mar 2011, at 13:43, Mathieu Dutour wrote:

> Dear all,
> 
> I run into memory problem with GAP and I want to reduce memory
> expenses. And well I want to understand better how it works.
> 
	[ ...]

> Now suppose I wrote
> MyFunctions:=function(n)
>  local eList, i, MyFactorial, MySum, a;
>  for i in [1..n]
>  do
>    eList[i]:=i;
>  od;
>  a:=2;
>  MyFactorial:=function()
>    return Product(eList);
>  end;
>  MySum:=function()
>    return Sum(eList);
>  end;
>  return rec(MyFactorial:=MyFactorial,
>             MySum:=MySum);
> end;
> Then it is clear that when calling "MyFunctions", "eList" should not
> be freed. But is "a" freed?
> 
> Now some general questions:
> ---Is there a way to know how much memory a variable occupies?
>   The commands GasmanStatistics() is helpful but gives you only
>   global information when what you want to know is more specific.
>   Would it be possible to know how much memory a given variable
>   uses? And the list of variables.
> ---It is said that the garbage collection of GAP is "very conservative"
>   What does that really mean?
> ---In garbage collection, the problem is with cyclical links, 
>   a->b and b->a. Well I do not even know how to create such links,
>   let alone cyclical in GAP.
>   What command could lead to such cyclical links?
>   Is there a way to know if such cyclical links are indeed present
>   in the variables of the user?
> 
> Best,
> 
>  Mathieu
> 
> _______________________________________________
> Forum mailing list
> Forum at mail.gap-system.org
> http://mail.gap-system.org/mailman/listinfo/forum




More information about the Forum mailing list