Message Boards Message Boards

1
|
7776 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How can I clear the history in a session?

Posted 3 years ago

Hello everyone,

I'm running a lot of loops on big data sets and need to conserve as much memory as possible. Each big loop creates a big file or results. I have set

$HistoryLength = 1;

but am wondering whether there is a way to clear all history of previous computations between runs of a loop without clearing variable definitions. Any tips?

Greg

POSTED BY: Gregory Lypny
5 Replies
Posted 2 years ago

This answer on MSE has a function for identifying which symbols are consuming memory greater than some specified size. I have found it useful for identifying symbols that consume a lot of memory which I can clear when they are no longer needed.

POSTED BY: Rohit Namjoshi

A while back I wrote a Mathematica function for starting completely fresh on a filled and previously saved notebook. A word of caution: it clears notebook history and system cache among several other things. I'll give you all definitions. Most of it is just an experiment in creating a self-correcting function. The only definition you really need is main, defaults, and clearToolbars[]. You may also wish to adjust default values to your preferences so that no arguments need be filled. You can remove patterns from arguments if you wish but make certain to change argument names into pattern names. For example n:zpat becomes n_, and h:fpat becomes h_.

I just noticed that for some reason my alerts arent rendering properly to warn on invalid inputs. Not sure why but everything else works. In short, both arguments must be n >= 0 and h >= 0 or infinity, Please write back if you have any questions or difficulties. I am very happy to help. :-)

(* freshStart 0.1.0 *)

(*** defaults and patterns ***)
freshdef = {0, 16};
{zpat = _Integer?(# >= 0 &), fpat = zpat | Infinity};

(*** main ***)
Options[freshStart] = {save -> True, quit -> False, 
   syscache -> True, toolbars -> False, remove -> {"Global`*"}};
freshStart[n : zpat, h : fpat, OptionsPattern[]] := 
  Block[{nb = EvaluationNotebook},
      If[OptionValue@save, NotebookSave[]];
      Unprotect[In, Out];
      Clear[In, Out];
      Protect[In, Out];
      $Line = n; $HistoryLength = h;
      If[OptionValue@syscache, ClearSystemCache[]];
      If[OptionValue@toolbars, clearToolbars[]];
      If[Length@OptionValue@remove > 0, (Quiet@Remove[#]) & /@ 
     remove]; If[OptionValue@quit, Quit[]];
      ];

(*** direct defaults ***)
freshStart[OptionsPattern[]] := 
  freshStart[freshdef[[1]], freshdef[[2]]];
freshStart[x : zpat, OptionsPattern[]] := freshStart[x, freshdef[[2]]];

(*** self-correcting ***)
freshStart[x_, y_, 
   z__] := (alert[freshStart, "trailing at pos 3", "dropped",
        (Style[#, col[trail], Bold] &) /@ {z}]; freshStart[x, y]);
freshStart[x_ /; x === Infinity] := freshStart[freshdef[[1]], x];
freshStart[x_ /; x === Infinity, y : fpat] := 
  freshStart[freshdef[[1]], x];

(*** alert fail ***)
freshStart[xy__] :=
    alert[freshStart, "inv args", (Style[#, Bold, col[fail]] &) xy, 
   "expecting", 
   "{n \[Element] \[DoubleStruckCapitalZ] \[VerticalSeparator] n \
\[GreaterEqual] 0, h \[Element] {n,+\[Infinity]}}"];

(*** args and alerts generators ***)
Options[alert] = {id -> Null, info -> Null};
alert[fn_, anomaly_ : "trailing dropped at pos 3", init_, final_, 
   OptionsPattern[]] := 
  Echo@Row[{fn, ": ", anomaly, " ", init, 
     Style[" \[Rule] ", Orange, Bold], final}];

(*** additionl utilities ***)
clearToolbars[] := 
  SetOptions[EvaluationNotebook[], DockedCells -> Inherited, 
   TaggingRules -> Inherited, CellContext -> Inherited];

freshStart[]
POSTED BY: Jules Manson
Posted 3 years ago

Oh... That makes sense. And thank you for sharing what you've learned.

POSTED BY: Mike Besso
Posted 3 years ago

Hi Mike,

Thank you for replying to my question.

I make regular use of Block, With, and Module. The slowdowns in processing many big files have to do with Mathematica accumulating a history of past computations. This happens even with Block or other scoping mechanisms if they contain loops.

A Wolfram tech support rep suggested that I throw in ClearSystemCache[ ], and that has resulted in a marked improvement in speed.

Greg

POSTED BY: Gregory Lypny
Posted 3 years ago

Greg:

I'm not yet a Wolfram Language expert, but if the Wolfram Language works like other languages that I've used, then you should might be able to use Block, With, and Module to scope your variables that require lots of memory.

This post might help.

POSTED BY: Mike Besso
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract