# Memory Leak while running Animate/Manipulate/Export

While looking into building a simulation for an ant colony, I started encountering some memory leakage issues.

 ymax = 1000; xmax = 1000;
nest = {500, 500};
maxAnts = 30;
steps = 200;
maxSpeed = 20;
maxAngle = π/6;

orientation =
RandomVariate[UniformDistribution[{0, 2 π}], maxAnts];
ant = Table[
agent[∞, nest, orientation[[i]], #] & /@ Range[i], {i,
maxAnts}];

update[agent[symbol_, location_, direction_, t_]] :=
Module[{dir, speed, loc},
dir = First@
RandomVariate[
UniformDistribution[{direction - maxAngle,
direction + maxAngle}], 1];
speed = RandomVariate[UniformDistribution[{1, maxSpeed}], 1];
loc = Flatten[location + Round[{speed*Cos[dir], speed*Sin[dir]}]];
loc = Min[#] & /@ Transpose[{loc, {xmax, ymax}}];
loc = Max[#] & /@ Transpose[{loc, {0, 0}}];
agent[symbol, loc, dir, t + 1]]

display[agent[symbol_, location_, direction_, t_]] :=
Text[Rotate[symbol, direction], location]
display[list_] := Module[{lst = display[#] & /@ list}, Graphics[lst]]

history =
Join[ant[[#]],
NestList[update, Last@ant[[#]], steps - Length[ant[[#]]]]] & /@
Range[maxAnts];

Animate[Show[Graphics[display[history[[All, i]]]],
Graphics[{Red, PointSize[Large], Point[nest]}],
PlotRange -> {{0, xmax}, {0, ymax}}], {i, 1, steps, 1}]


The issue I have is that Mathematica seems to have a memory leak, as the Animate, or manipulate, or exporting to gif runs, the amount of memory allocated to Mathematica keeps growing until it just stops working. Below is a gif in which I turn the animation on and off to show that the memory keeps growing as Animate (or Manipulate) runs. A bummer because this limits the number of ants and steps to use.

-
Thanks @MichaelE2. Placed a support ticket with Wolfram Research. –  PatoCriollo Oct 11 at 1:34