# Module and memory use

### Introduction

My question is related to this one: https://stackoverflow.com/questions/6867892/reducing-memory-usage-in-an-extended-mathematica-session

I have a lot of Mathematica code in which I define functions and do many calculations. Overall, in a single session I could get to hundreds of thousands of iterations.

In order to avoid running out of memory my functions have the form:

f =
Function[{arguments},
Module[{local vars},
do all calculations;
result= result of calc;
ClearAll[ all local vars except result];
result]]


I also use \$HistoryLength = 10

This works rather well, except for a two flaws.

### First flaw

Some of my results are actually intermediate steps. This is a result of my doing some calculation, then returning a result to another function, and using this returned value to preform another calculation. Eventually, I wish to clear the first result, but how can I reference it outside the scope of the original module?

### Second Flaw

Eventually the returned values of all calculations are plots. Sometimes I wish be able to use the get coordinates of curves to obtain specific values. For this I use CoordinatesToolOptions to get the desired data. My problem is that the function called using CoordinatesToolOptions needs to take as an argument several intermediate results (each matrix can take about ~200MB).

As I need them for the get coordinates, I cannot clear them. Is there a way for me to clear these matrices is still use get coordinates?

• You might consider using HoldXXX attributes for some functions and passing in the variables as symbols. This emulates call-by-reference. When done with them they can of course be cleared. – Daniel Lichtblau Jul 31 '16 at 15:17
• Thanks for the comment. I never used Hold attribute. Could you please elaborate? In what way do you suggest to alter the order of evaluation? – Yair M Jul 31 '16 at 20:09