Note that in Groovy variables should not be declared inside the loops.
The following code can result in an error message "Too many new instances created" in the server log:
for (...) { def var = [:] }
So you should always change it to this form:
def var for (...) { var = [:] }
The technical background is that the first script allocates a new variable which can increase the number of FormulaSandbox instances (above the limit).