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.
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.
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