Resetting memory and garbage collection?

Hi

Does anyone know how to reset the memory during a large number of loops? I've read jet's post on this subject and someone mentioned garbage collection but I'm not clear how to go about doing this.

Basically what I want to do is after every 10 loops or so export the answers, keep the few variables I need and then reset the memory (equivalent to closing down maple & reopening).

Is it possible to code this so it does it at run time? At the moment my memory usage just keeps on increasing eventually resulting in an error message about kernel loss and the program terminating. I don't need to be/ shouldnt be using all that memory as the results from 1 loop have no bearing on the results in the 2nd.

 

thanks

 

clarification

sorry I've just realised that my question wasnt very specific...

I would like to "reset" the memory. One potential way forward seems to be invoking garbage collection every few loops. According to the help pages the garbage collection deletes "data to which no reference is made". What exactly does "no reference" mean? There wont be a variable that isnt mentioned (referenced?) in the code will there? Or does "referenced" mean something different?

Is garbage collection the solution to my problem and if so exactly how does it work?

THANKS!

 

roman_pearce's picture

garbage collection trick

Garbage collection in Maple is automatic. Your program runs, intermediate objects are created, and then when objects are no longer referenced their memory is collected to be reused by the system. For example, when you do the following:
f := x+1;
f := f+1;

Maple creates the object x+1 in memory, then it creates a new object (x+1)+1 = x+2 in memory, and since the original object is no longer referenced x+1 may be garbage collected. However, there is a catch. Garbage collection in Maple is "generational", which means that memory may not be recovered inside of a large complex routine until the routine terminates. One solution is to make a subroutine to perform a memory intensive part of the computation. For example, make a new procedure which executes 10 iterations of your loop. To avoid passing too many arguments, you can use lexical scoping. For example:

foo := proc(x)
local f, c, bar;
  f := x;
  c := 1;

  bar := proc(f1)
  local f;
    f := f1;
    to 10 do
      f := f+c;
    end do;
    return f;
  end proc;

   to 10 do
      f := bar(f);
   end do;
end proc;

This is a slightly long example so bear with me. The subroutine bar executes 10 iterations of f := f+c; The variable c is from foo, it is lexically scoped into bar. This way you can avoid passing a lot of variables around. We execute f := f+c; 100 times and create 100 intermediate objects, but 90% of them are created in bar and these can be garbage collected after each call to bar terminates. 10% of them are in foo and can not be garbage collected until after foo terminates.

This may or may not solve your memory problem. It may be that the objects themselves are too large. But if it helps, there it is.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}