MathGroup Archive 2010

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

Search the Archive

Re: Module, DynamicModule & MemoryInUse

  • To: mathgroup at smc.vnet.net
  • Subject: [mg107693] Re: [mg107686] Module, DynamicModule & MemoryInUse
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Tue, 23 Feb 2010 08:03:17 -0500 (EST)
  • References: <201002230005.TAA26554@smc.vnet.net>

Hi Ariel,

I've encountered this problem before. The problem is that the variables
created by Module are not de-allocated as long as there is a reference to
them from any global symbol or construct outside the original Module's
scope. In your case, the panels wrapped in DynamicModule are such
constructs.

One possible solution is to create explicit deallocating functions, for
example as follows:

Clear[memModule, getHeap, clearHeap];
Module[{heap = {}},
  getHeap[] := heap;
  clearHeap[] :=
   While[heap =!= {},
    Remove @@ Last[heap];
    heap = Most@heap];

  memModule[] :=
   Module[{data, memBefore, mu},
    mu := Grid[{{"Memory in use: ", MemoryInUse[]/(2^30.), "GB"}}];
    memBefore = mu;
    data = RandomReal[1, {300000, 20}];
    heap = Append[heap, Hold[data, memBefore, mu]];
    DynamicModule[{d1}, d1 := data[[1]];
     Panel[Grid[{{memBefore}, {mu}}]], UnsavedVariables -> {d1}]]
  ];

I introduced getHeap for illustrational purposes - the real work is done by
clearHeap. Now observe:

In[3]:=
memModule[]

Out[3]= (*Skipped *)

In[4]:= getHeap[]

Out[4]= {Hold[data$510, memBefore$510, mu$510]}

In[5]:= MemoryInUse[]

Out[5]= 58864208

In[6]:= clearHeap[]

In[7]:= MemoryInUse[]

Out[7]= 10864784

In[8]:= memModule[]
memModule[]

Out[8]=  Skipped

Out[9]= Skipped

In[10]:= getHeap[]

Out[10]= {Hold[data$526, memBefore$526, mu$526],
 Hold[data$531, memBefore$531, mu$531]}

In[11]:= MemoryInUse[]

Out[11]= 106863840

In[12]:= clearHeap[]

In[13]:= MemoryInUse[]

Out[13]= 10871688

This is one of the instances of the more general garbage collection issues
associated with Module-generated variables used in "non-standard" ways.
Please see my third post in this thread:

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/5eff6213a0ab5f51/29f3c90fe11b4926

and there also references to a couple of more threads with a discussion of
this topic.

Hope this helps.

Regards,
Leonid




On Mon, Feb 22, 2010 at 4:05 PM, Ariel Sepulveda <sepulveda.cuevas at gmail.com
> wrote:

> The following example has been created for showing a memory issue related
> to
> Module and DynamicModule.  Everytime memModule[] is executed the memory in
> use increments.  My question is, is there a way to avoid this unwanted
> increment in MemoryInUse?  I need data to be a local variable in the
> external module and d1 to be a local variable in the DynamicModule.  Note
> that pressing Ctrl+Shift+E shows that data is not part of the saved
> expression in the DynamicModule output, thus I don't understand why the
> memory keeps increasing.
>
> memModule[] :=
>  Module[{data, memBefore, mu},
>  mu := Grid[{{"Memory in use: ", MemoryInUse[]/(2^30.), "GB"}}];
>  memBefore = mu;
>  data = RandomReal[1, {300000, 20}];
>  DynamicModule[{d1},
>   d1 := data[[1]];
>   Panel[Grid[{{memBefore}, {mu}}]]
>   , UnsavedVariables -> {dl}
>   ]
>  ];
> memModule[]
> memModule[]
> memModule[]
>
> Your support will be appreciated,
>
> --
> Ariel Sepulveda, Ph.D.
> __________________________
> President, Pronto Analytics Inc.
> Tel. 787.354.6947
> ariel.sepulveda at prontoanalytics.com
> http://www.prontoanalytics.com
>
>
>



  • Prev by Date: Re: Re: Handheld mathematica
  • Next by Date: Re: DiscreteUniformDistribution PDF isn't piecewise?
  • Previous by thread: Module, DynamicModule & MemoryInUse
  • Next by thread: Re: Re: problem with double integral - Very useful for