# Avoid leak of memory while using FindMinimum?

**URL:** <https://community.wolfram.com/t/avoid-leak-of-memory-while-using-findminimum/10051>\
**Category:** Questions\
**Tags:** physics, mathematica, high-performance-computing, optimization\
**Created:** [February 21, 2017, 8:09pm UTC](https://community.wolfram.com/t/avoid-leak-of-memory-while-using-findminimum/10051 "2017-02-21T20:09:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luisatesta](https://avatars.discourse-cdn.com/v4/letter/l/a5b964/32.png) [@luisatesta](https://community.wolfram.com/u/luisatesta)\
**Post date:** [February 21, 2017, 8:09pm UTC](https://community.wolfram.com/t/avoid-leak-of-memory-while-using-findminimum/10051/1 "2017-02-21T20:09:30Z")

</div>

Hi everybody,  
my problem is the following, I need to minimize a certain function “errorfunc” with FindMinimum

```
(*
errorfunc minimize the L2 HIlbert norm of the difference between TLE \
and TLN*) 
errorfunc[sig_, m_] := Module[{TLN, diff},
   TLN = trT[sig, m] trH[sig, m];
   diff = Total[(TLN - TLE)^2]^(1/2);
   Return[diff];
   Clear[TLN, diff]]; 

```

FindMinimum needs to evaluate the errofunc thousand times on each couple (sig, m).  
The calculation of each function value is lengthy, and requires a reasonable amount of memory.  
But, for some reason, the memory usage in Mathematica grows steadily until it consumes 83% of my cluster memory (32GB) then the computation continues extremely slowly; why is this?   
Ive read something:

[https://groups.google.com/forum/#!topic/comp.soft-sys.math.mathematica/UYzbXE615lk](https://groups.google.com/forum/#!topic/comp.soft-sys.math.mathematica/UYzbXE615lk)

[http://stackoverflow.com/questions/6867892/reducing-memory-usage-in-an-extended-mathematica-session](http://stackoverflow.com/questions/6867892/reducing-memory-usage-in-an-extended-mathematica-session)

and I thought that the problem was due to the fact that I frequently use Module to define functions.  
I ve tried to solve the problem setting $HistoryLength=0 and cleaning such variables before exiting Module, but nothing seems to change, the leakage still remains.  
Has anybody an idea how to avoid this leakage?  
Attached you can find the entire code. (I’m usign Mathematica v10.0)

Thanks in advance for any suggestions,  
Luisa

---

<div class="post-metadata">

**Author:** ![lehinp](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/lehinp/32/8056_2.png) [@lehinp](https://community.wolfram.com/u/lehinp)\
**Post date:** [February 28, 2017, 1:06pm UTC](https://community.wolfram.com/t/avoid-leak-of-memory-while-using-findminimum/10051/2 "2017-02-28T13:06:34Z")

</div>

`Module` does cause memory leaks in a number of specific cases, but I don’t think the memory leak you describe is due to `Module`. I recommend you reading [this](https://wolfram.discourse.group/t/10048/1) (specifically the penultimate paragraph from the second post by Leonid Shifrin and posts linked therein) for additional information about situations when `Module` can cause memory leaks. Also you can find useful the following threads:

- [Profiling memory usage in Mathematica](http://stackoverflow.com/q/3418892/590388)

- [How to un-eat memory?](http://mathematica.stackexchange.com/a/38861/280)

---

<div class="post-metadata">

**Author:** ![lehinp](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/lehinp/32/8056_2.png) [@lehinp](https://community.wolfram.com/u/lehinp)\
**Post date:** [February 28, 2017, 1:33pm UTC](https://community.wolfram.com/t/avoid-leak-of-memory-while-using-findminimum/10051/3 "2017-02-28T13:33:30Z")

</div>

> I ve tried to solve the problem setting $HistoryLength=0

As I understand, you tried to set

```
$HistoryLength = 0

```

in the master kernel only. But the slave kernels still have infinity history length what you can check by evaluating

```
 ParallelEvaluate[$HistoryLength]

```

I recommend you setting zero history length in the slave kernels too:

```
ParallelEvaluate[$HistoryLength = 0]

```
