Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Module, DynamicModule & MemoryInUse

45 views
Skip to first unread message

Ariel Sepulveda

unread,
Feb 22, 2010, 7:05:23 PM2/22/10
to
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.s...@prontoanalytics.com
http://www.prontoanalytics.com


Leonid Shifrin

unread,
Feb 23, 2010, 8:03:13 AM2/23/10
to
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

0 new messages