MathGroup Archive 2003

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

Search the Archive

Re: Compile + Module => Memory Leak

  • To: mathgroup at
  • Subject: [mg39945] Re: Compile + Module => Memory Leak
  • From: Jens-Peer Kuska <kuska at>
  • Date: Thu, 13 Mar 2003 03:00:17 -0500 (EST)
  • Organization: Universitaet Leipzig
  • References: <b4mocf$inm$>
  • Reply-to: kuska at
  • Sender: owner-wri-mathgroup at

Bill Rowe wrote:
> On 3/11/03 at 2:37 AM, bgress at (Bern) wrote:
> [example snipped]
> >Presumably, upon exit, a Module should release any memory used, no?
> No, I don't believe this is the way Mathematica deals with memory. My understanding is Mathematica releases memory that is no longer needed as determined by whether there are expressions that reference that memory. Once all of the expressions that reference a piece of memory are deleted the referenced memory is released.
> In the code you posted (that I snipped) the Module construct creates symbolic references. The code does not delete the created symbols. As far as I know, neither Module nor Block deletes symbols created unless you write code to specifically do this. Since the symbols are not specifically deleted, the memory these symbols reference is not released.



Temporary is an attribute assigned to symbols which are created as local
variables by Module. 

and the help say

"Symbols with attribute Temporary are automatically removed when they
are no longer needed."

so you have *not* to write code to remove the symbols from Module[].

Block[] only add the new symbols to the global symbol stack, an when 
you leave the Block[] the stack pointer is reseted to it's original
value -- nothing to release.

The problem has nothing to do with Module[] or Block[] because
the memory is allmost constant with the uncompiled


while the compiled w increments the memory by very huge values, and I
the memory for the u array is not released. You can try it with

w = Compile[{{XJ, _Real, 1}, {XX, _Real, 1}, {nj, _Integer}, {ni,
    Block[{p}, u = Table[Random[], {i, ni}, {j, nj}, {p, 0, 1}]; 
                Outer[Times, u[[i, j]], u[[i, j]]*(XX[[i]] -
XJ[[j]])^2], {i, 
                  1, ni}]].Sum[
              Table[u[[i, j]]*XX[[i]]*(XX[[i]] - XJ[[j]])^2], {i, 1, 
                ni}]], {j, nj}]], {{u, _Real, 3}}]

that does not change the memory *and* is significant faster than the
original function with the local u.


  • Prev by Date: Re: This should evaluate to zero
  • Next by Date: Re: What's legit here?
  • Previous by thread: Re: Compile + Module => Memory Leak
  • Next by thread: SetPrecision & Plot bug?