Why does the Frontend slow down in long sessions?

I'm running Mathematica 12.1.1 on my Windows 10 laptop (Dell Latitude E5550 with Intel iCore 5, 8 GB RAM). My MMA sessions tend to be very long, sometimes extending on over days and weeks, interrupted by hibernation and wakeup. I develop software that uses a lot of graphical output such as ArrayPlot[] of matrices size 300x300 or so.

The stability is good, but the MMA Frontend always slows down over time. In heavy use, the slowdown becomes already noticeable within a single working day, i.e., before I hibernate the laptop at the end of the working day. The slowdown is most obvious when editing or typing commands and I sometimes reach the point that it takes the Frontend a second or so to display character after character when quickly typing a new command or line in a Module[]. Quitting and restarting MMA is then the only option (ClearAll["*"] or even Quit[] and kernel restart doesn't help), but it takes me about 10 minutes to reload all notebooks and definitions and resume work, hence I'm hesitant to do so too often.

I usually have about 10 to 15 notebooks open, but normally I either save and close notebooks with lots of figure output, or else overwrite them on each rerun of the code I'm testing. I monitor the Frontend and Kernel memory which both keep climbing steadily, but not to the point that they consume more than 1 GB or so, hence just a small fraction of my total RAM. I already set $HistoryLength=150 to prevent the command history from accumulating. Can anybody tell me what's the reason of the slowdown (memory leaks, excessive number of variables to look up in the automatic variable suggestion/completion function, stale file handles, memory page faults, or a bug in the Frontend....)? Any suggestions how to better monitor my session or find/remove the root cause of the slowdown? Thanks, Ron • Maybe the answer is this ;) – Henrik Schumacher Dec 21 '20 at 14:47 • How about setting$HistoryLength=0 for starters. – Dominic Dec 29 '20 at 14:29
• Related/duplicate: mathematica.stackexchange.com/questions/216746/… – Michael E2 Dec 29 '20 at 17:49
• Hi Michel, I don't think this post is very much related, or even a duplicate. The post you linked above reports an issue when using datasets with MMA version 12.1.0. I'm using 12.1.1 and no datasets. – RonH Jan 2 at 16:37

By default Mathematica uses $HistoryLength = Infinity, so it stores every output expression since you started Mathematica in the data associated with the System symbol Out. If a lot of your output takes a lot of memory, you can easily slow down your computer. Instead, you could use this $HistoryLength = 2;

Then Mathematica will only remember the two most recent output expressions (but that would not apply to anything you did before $HistoryLength was changed). Better yet, run the following once, and Mathematica will do that every time it starts! InitializationValue[$$Initialization] = Hold[$$HistoryLength = 2]  • ... or you could just chuck $HistoryLength = 2 into init.m`... – J. M.'s ennui Dec 29 '20 at 13:12