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