1
$\begingroup$

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

{MemoryAvailable->2.81581GiB,PhysicalUsed->13.3471GiB,PhysicalFree->89.7188MiB,PhysicalTotal->16.GiB,VirtualUsed->15.9964GiB,VirtualFree->19.GiB,VirtualTotal->19.GiB,PageSize->16.KiB,PageUsed->2.64923GiB,PageFree->359.188MiB,PageTotal->3.GiB,AppMemory->3.4355GiB,Wired->1.57626GiB,Active->2.76234GiB,Inactive->2.7282GiB,Compressed->8.33537GiB,PageIns->4468192,PageOuts->49208} 

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

$\endgroup$
3
  • 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$ Commented Jan 9, 2024 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$ Commented Jan 9, 2024 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$ Commented Jan 9, 2024 at 11:53

1 Answer 1

0
$\begingroup$

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[];]; ClearSystemCache[]; Pause[5]; 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

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.