Message Boards Message Boards

0
|
6560 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

How do I free memory after ListConvolve operation

Hello Community,

I am trying to generate random spots for a simulation and I encountered an issue with ListConcolve using up a lot of memory that I cannot reuse. Here is the code. I think it is quite easy to understand, but here a short explanation: x1 is a large 2d list containing 0s, which is being filled with 1s at 100 random positions. Then I convolve this list with a Gaussian matrix k to make a Gaussian looking spot from every 1 in x1. I repeat this 10 times in this example to demonstrate how much memory this code "eats". Each time I evaluate the code, more memory is in use.

k = GaussianMatrix[{50, 5}];
a = Table[
   x1 = Array[0 &, {1000, 1000}];
   (x1[[Sequence @@ #]] = 1) & /@ (RandomInteger[999, {100, 2}] + 1);
   x2 = ListConvolve[k, x1, {51, 51}];
   x3 = Image[x2];
   , {10}];
MemoryInUse[]

I found that the memory-eater is "ListConvolve", since the effect is gone when an image based convolution is in use instead:

k = Image[GaussianMatrix[{50, 5}]];
a = Table[
   x1 = Array[0 &, {1000, 1000}];
   (x1[[Sequence @@ #]] = 1) & /@ (RandomInteger[999, {100, 2}] + 1);
   x2 = Image[x1];
   x3 = ImageConvolve[x2, k];
   , {10}];
MemoryInUse[]

When I evaluate this code several times, the memory in use stays the same after the first run.

Well I solved the problem, but I still wonder how I could free the memory used in the first example, and what is going on. Also, I guess with ImageConvolve there is no way to connect left and right by cyclic repetition of the list elements. The time the ListConvolve and ImageConvolve operations need seem to be the same though.

Thanks for your ideas!

Max

3 Replies

Hello David and Alexay,

thanks for your suggestions! However, none of them changed the outcome.

Then I updated from 10.0.2 to 10.2.0 and now the issue is gone! I am also working with Win7 Pro x64.

Thanks again for your replies!

Max

Hello, Maximilian! Maybe, using "$HistoryLength=1" - can solve your problem? Inform me, please...

Posted 9 years ago

Hi Max,

On my Win7 Pro x64, Mathematica 10.2.0, your ListConvolve does not appear to leak memory. MemoryInUse is pretty much the same after each of the 10 iterations, and in the kernel it uses less memory that the ImageConvolve. However, consolidating memory is done by the function Share[].

Best, David

In[1]:= k = GaussianMatrix[{50, 5}];
a = Table[x1 = Array[0 &, {1000, 1000}];
   (x1[[Sequence @@ #]] = 1) & /@ (RandomInteger[999, {100, 2}] + 1);
   x2 = ListConvolve[k, x1, {51, 51}];
   x3 = Image[x2];
   Print[MemoryInUse[]], {10}];

During evaluation of In[1]:= 43397800

During evaluation of In[1]:= 43398104

During evaluation of In[1]:= 43398248

During evaluation of In[1]:= 43398392

During evaluation of In[1]:= 45485456

During evaluation of In[1]:= 45485456

During evaluation of In[1]:= 45485456

During evaluation of In[1]:= 45485456

During evaluation of In[1]:= 45485456

During evaluation of In[1]:= 45485456

In[3]:= (* kernel *) MemoryInUse[]

Out[3]= 45350272

In[4]:= MemoryInUse[$FrontEnd]

Out[4]= 140931072

In[5]:= k = Image[GaussianMatrix[{50, 5}]];
a = Table[x1 = Array[0 &, {1000, 1000}];
   (x1[[Sequence @@ #]] = 1) & /@ (RandomInteger[999, {100, 2}] + 1);
   x2 = Image[x1];
   x3 = ImageConvolve[x2, k];, {10}];
MemoryInUse[]

Out[7]= 55257384
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract