I've already checked this post How to clear up RAM without exiting kernel?

But the longer my code works, it builds up RAM and cannot clear it. I've stopped the evaluation and done the following.

  • Clear all my variables
  • Unprotect Out and clear it
  • Clear cache
  • set $HistoryLength=0

I did run the SystemInformation["Machine"] check and obtained


Seems like Mathematica maintains a lot of RAM used as Compressed and I cannot find a way to clear it from code. Obviously, if I free the kernel the memory goes back down, but I wish to have my code running for days without reaching that much RAM usage

I am using Version 13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)

 {" Machine Type", "ARM"},
 {" Operating System", "MacOSX"},
 {" Processor Type", "ARM64"}

Thank you in advance

  • 3
    $\begingroup$ It will be very difficult to give any concrete and useful suggestions without your actual code that you are using. It might be a memory leak in some function. $\endgroup$
    – Domen
    Commented Jan 9 at 10:56
  • $\begingroup$ @Domen thanks for replying. Code is really long and will not be understood. I use like 10 modules and I read that Modules can cause memory leaks. However my code ran in loop to iterate with a pause. Every iteration the RAM builds up, but I do not understand why clearing up all my variables and functions still keeps compressed RAM. Any general tip? $\endgroup$
    – Jose
    Commented Jan 9 at 11:07
  • 5
    $\begingroup$ In your case, I would try to determine first which block of the code is the problematic one. Try to divide your code into separate blocks, test each one, use MemoryInUse[] to locate the problem ... $\endgroup$
    – Domen
    Commented Jan 9 at 11:53

1 Answer 1


Thanks to @Domen I managed to get an answer.

Appart from the mentioned above I've substituted all my "Module" blocks with the "Block" function. However, that didn't solve the RAM issue.

To get rid of the RAM issue I had to add the following code:

  If[(AbsoluteTime[] - TimerAfterStartingCalculation) > 60*60,
  TimerAfterStartingCalculation = AbsoluteTime[];
  If[Length[ParallelEvaluate[$KernelID]] > 0, CloseKernels[];];
  If[Length[ParallelEvaluate[$KernelID]] == 0, LaunchKernels[];]; 

TimerAfterStartingCalculation is an AbsoluteTime[] variable to detect for how long my code has been running, so I execute the code inside every 60*60 seconds (1 hour). The solution turns out to be, certainly, ClearSystemCache[]. The memory is not freed unless you close your kernels first. Closing the kernels is mandatory (to me, at least) to allow Mathematica to clear the SystemCache. I added a pause to ensure the cores have enough pause to relaunch them. Without a Pause the cores weren't closing completely (I think).

I am quite certain my answer can be tuned up a bit more, but that's what I experienced. Since this piece of code was added the RAM pressure on my MacOS never raises too much


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.