MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: What Happens to Garbage in Mathematica?

  • To: mathgroup at
  • Subject: [mg43952] Re: What Happens to Garbage in Mathematica?
  • From: "Steven T. Hatton" <hattons at>
  • Date: Wed, 15 Oct 2003 04:59:28 -0400 (EDT)
  • References: <bm84s2$fug$> <bmg0nf$e9t$>
  • Sender: owner-wri-mathgroup at

Olaf Rogalsky wrote:

> "Steven T. Hatton" wrote:
>> What happens to the alocated memory of objects such as graphics
>> primatives
>> when their variable are assigned new values?  For example, suppose I have
>> a thousand pairs of triple which I use to create a thousand Line objects.
>> If I then change the values of all the tripples, and create new Lines
>> assigning them to the same variables I used to store the original Lines,
>> what happens to the memory that held the original lines?
> Mathematica "reference counts" every data object. The reference count of
> an object A is the number of other objects, which reference A.
> In the example below, the created table is referenced 4 times: One
> reference from the symbols a and b, and one reference from Out[2] and
> Out[4]. If the reference count of an object drops to 0, the memory may be
> reclaimed by Mathematica. But note, the freed memory is not given back to
> the operating system, but only marked for reuse by Mathematica.
> Olaf

I really don't care too much if the OS gets the memory back. What matters
more to me is how much allocation work has to be done.  When working with
very large graphics data sets, and changing values with each iteration of
the scene clock, there are often significant performance enhancements to be
gained by minimizing the number of new memory allocations, assignments,
etc. that need to be made.  I'm trying to get an idea of what happens when
I recreate graphics primitives. Currently, the only way I know to change
the location of a primitive is to create a new one, and assign it to the
original variable.

I'm still learning the syntax of Mathematica, so the following snippet is a
bit hard to understnad.  It's from the following file:


RotateShape[ shape_, phi_, theta_, psi_ ] :=
    Block[{rotmat = RotationMatrix3D[N[phi], N[theta], N[psi]]},
        shape /. { poly:Polygon[_] :> Map[(rotmat . #)&, poly, {2}],
                   line:Line[_]    :> Map[(rotmat . #)&, line, {2}],
                   point:Point[_]  :> Map[(rotmat . #)&, point,{1}] }

Obviously it is something of a "switch" which branches to the appropriate
function (transformation rule?).  It pulls the points out of the shape and
transforms them using the rotation matrix. It would seem it places the
results in a list with a head of the same type as the shape.  What it
clearly doesn't do is modify the 'members' or elements of the original
list.  For instance this code creates an image with two line segments:

lna = Line[{{1, 2, 3}, {4, 5, 6}}]
lnb = RotateShape[lna, 2, 2, 2]
Show[Graphics3D[{lna, lnb}]]

It would seem that if I were to assign the return value of RotateShape[] to
lna like this: lna = RotateShape[ln, 2, 2, 2], I would create a dirty chunk
of memory which will require housekeeping before it can be reused.

I've learned that Java3D tends to avoid allocating new memory in ways
similar to this.  I really can't see a way around the garbage generation
demonstrated above.  I don't know how big of an issue it might turn out to
be.  I can try to avoid unnecessary regeneration of primitives, which I
already do by only destroying the graphics in a branch that has been

This also plays a role in deciding whether to avoid recreating the points
composing an object's representation using the mapping that created it, as
opposed to simply transforming its points.  If I can perform the mapping
based on the new location and orientation of its 'inertial frame', it may
be cheaper than applying the matrix multiplication on each point.

I guess the essence of my original question is: how expensive is this
garbage production and recycling?



  • Prev by Date: Re: Rows & Columns; What do *You* Call a Vector
  • Next by Date: Re: Integration
  • Previous by thread: Re: What Happens to Garbage in Mathematica?
  • Next by thread: Re: What Happens to Garbage in Mathematica?