Skip to main content
Inspiring
December 23, 2023
Answered

[Q] Can I somehow facilitate memory management / garbage collection in tight loops?

  • December 23, 2023
  • 1 reply
  • 385 views

Hi,

 

I'm running into memory problems and am looking for some advice.

 

I run a double tight for-loop in which I draw thousands of shapes. While the for-loops are active I notice that memory is being consumed quickly. When I stop the drawing process and with the resulting Illustrator file still open, I see memory usage fall significantly, back to normal levels. This makes me suspect that it is not the resulting drawing that is consuming memory but the variables I use in the loops. It feels as if garbage collection isn't working properly: as if variables that go out of scopes are not released for cleanup.

 

I there anything I can/should do about this? I already do a 'save as' action at the end of each inner loop. Should I bring all variables outside of the loops? Should I nullify them?

 

Any help greatly appreciated.

 

// Tom

 

for (var i = 0; i < kNumRows; i++) {
for (var j = 0; j < kNumCols; j++) {

// I use a lot of local variables here

}

// saveas action

}

 

 

This topic has been closed for replies.
Correct answer CarlosCanto

awesome, that's what's usually adviced, taking variable declarations outside of loops does help. 

1 reply

CarlosCanto
Community Expert
Community Expert
December 23, 2023

that's a known problem in Illustrator, garbage collection doesn't work. The longer you work with it manually or automatically the slower is gets.

 

you have to restart illustrator to release memory.

 

can you post some of your code inside the loops?

Inspiring
December 23, 2023

Hi Carlos,

 

Many thanks for your reaction. I think I may have solved my problem. Well, not really solved it but it's a work around for a memory leak: I simply declared all variables that are used within the loops outside of the loops. So new values get assigned on every iteration of the loop but the reserved memory locations stay the same (I presume). Not so elegant, counter to good programming practice, I believe, but it works.

 

Oh, and how I wish it was faster...

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
December 23, 2023

awesome, that's what's usually adviced, taking variable declarations outside of loops does help.