5
$\begingroup$

I have a Do loop, which saves data directly to a .txt file. However when I leave it running for long periods of time the RAM starts to run out till finally my laptop crashes. I've read similar posts on here but nothing seems to have worked.

After each run of the Do loop, I no longer need the data because it has been saved in a .txt file. This means I don't require Mathematica to use up my RAM remembering it... but I can't get it to stop!

I tried putting ClearSystemCache[] inside the Do loop at the end but this didnt work. I read somewhere that this could be because I need to clear the memory of the individual subkernels.

What is meant by subkernel? I run ParallelEvaluate[] and it launches four kernels, one for each CPU. Are these 4 kernels called subkernels? If so what is 'sub' about them? But the main question is how do i go about clearing the memory of the subkernals?

My set up is

ParallelEvaluate[
        Do[ 
           (bulk of formulae)
           If[ (condition), PutAppend[ ]     ]
          ]
                ]

I have tried putting

ParallelEvaluate[
        Do[ 
          (bulk of formulae)
          If[ (condition), PutAppend[ ]     ]
          ClearSystemCache[]
          ]
                ]

However this doesnt seem to work.

I've also tried:

  • putting $HistoryLength = 0; at the start of the Do loop (doesn't change anything)

  • putting Clear["Global`*"]

  • putting Remove["Global`*"] inside end of Do loop (gives an error)

If someone could explain how to clear memory of subkernels and/or give any other suggestions that would be great. Thanks

$\endgroup$
2
  • $\begingroup$ Try Unprotect[Out]; Clear[Out];. $\endgroup$
    – chyanog
    Sep 10, 2013 at 9:06
  • $\begingroup$ @kingmonkeywoman Are you using Module for defining your functions? In afirmative case, this might be the issue. @chyaong in such case, your suggestion won't work, because of the bug described in stackoverflow.com/questions/6867892/… $\endgroup$
    – fcpenha
    Apr 9, 2014 at 12:21

1 Answer 1

0
$\begingroup$

The easiest way to clear the memory of the subkernels is like this, for a variable

pc =.; delete it in the master kernel

DistributeDefinitions[pc]

distribute the definition

Touch the two commands I hope it helps you.

$\endgroup$
1
  • $\begingroup$ The easiest way is run CloseKernels[] after each one-two loops with following LaunchKernels[]. Any attempts of calling of ClearAll[] or something else for local clearing of variables finally lead to growing amount of garbage in memory. But starting from the MMA13, the memory management became much better than before.. Previously it was shit. $\endgroup$
    – Rom38
    Nov 18, 2022 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.