且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

限制Mathematica内存使用的正确方法?

更新时间:2023-10-05 15:54:40

Mathematica 8中,您可以启动内存看门狗,类似于以下内容:

In Mathematica 8 you could start a memory watchdog, something along the lines of:

maxMemAllowed        = 15449604;
intervalBetweenTests = 1; (*seconds*)
iAmAliveSignal       = 0;
Dynamic[iAmAliveSignal]
RunScheduledTask[
       If[MemoryInUse[] > maxMemAllowed , Quit[], iAmAliveSignal++],      
       intervalBetweenTests];

记住要运行

RemoveScheduledTask[ScheduledTasks[]];

将其禁用.

修改

您可以在退出前发出警报或交互式决定要做什么. 根据要求,这是分配了1.3GB的试用版.除了这台机器,我走得更远.

You may alert or interactively decide what to do before quitting. As requested, here is a trial with 1.3GB allocated. I can't go much further than that in this machine.

maxMemAllowed = 1.3 1024^3; (*1.3 GB*)
intervalBetweenTests = 1; (*Seconds*)
iAmAliveSignal = 0;
leyendToPrint = "";
Dynamic[leyendToPrint]
RunScheduledTask[
  If[MemoryInUse[] > maxMemAllowed, 
   CreateDialog[CancelButton["Max Mem Reached", DialogReturn[]]]; 
   Quit[],
   Print["Memory in use: ", MemoryInUse[]]; 
   leyendToPrint = 
    "Seconds elapsed = " <> ToString[iAmAliveSignal++]], 
  intervalBetweenTests];
IntegerPartitions[320, {15}];