Thanks for offering to help with the memory issues in my code. It does perform a numeric calculation. Basically, it starts by reading a file with heights and soil depths on a grid, profiles it to rank the heights of four nearest neighbours to each point and work out the probability that the soil will become saturated, based on soil depth. Then it just goes through and iteratively selects points as saturated (turns 0s to 1s) based on the PDF, and if already saturated it routes through to the lowest unsaturated point along the perimeter of locally connected 1s. It then needs to start over a given number of times and calculate an average pattern of saturation. When that number of Monte Carlo runs is done, I want to calculate a few things, save them, and then change the values of two parameters and start again. As you can see, it's all done in serial, so if it can be optimised to work that way, that would be fantastic.
It's already come a really long way through your suggestions, as I've added datatype=float to a few Arrays and changed evalf to evalhf in a few instances where I was able to do so without causing errors; but the allocated memory is still noticably increasing with each run (albeit by about 5M rather than 100M), and the whole thing could probably go a lot more quickly. One more thing: those few calculations that are performed at the end of a set of Monte Carlo runs with a given set of parameters are sucking up huge amounts of memory, and I can't see why. This wasn't my primary concern, as I need to first be able to run through some number of Monte Carlo runs over the grid of parameters without having to be here to execute that every 20 mins; but it is another looming issue, so if you do see a quick way of making the very last for loop not suck up memory, that would be fantastic.
I've attached my .mw file, along with the .xlsx file that it reads from. I've moved everything into one execution block, but there are really about four parts: the initial physical profiling that ends with a garbage collection that works great; a procedure that gets called in order to determine the extent of the locally connected patch; the Monte Carlo for loop, with a given set of parameters; and then the calcullations that are based on the results with those parameters. Ultimately, I'd write two for loops around these last two steps in order to vary the parameters and get my results in serial.
I hope the code doesn't look too much like I went off and invented a square-shaped wheel. As I said before, I haven't got a computing background.
Thanks again for responding (over here)!