Well, think of it this way
Pretend locations are units.
If you have too many units, it lags, right?
So, also, if you have too many LOCATIONS, it lags, so you gotta destroy them all. ( this also applies to anything but Integers, Reals, Strings, Booleans, and Codes )
To fix this, set every (location in this case) to its own variable, and after you're done using it, add this line
Custom Script: call RemoveLocation( udg_x )
Replacing x with your variable's name.