Correct way to cap Mathematica memory use?

前端 未结 1 1985
灰色年华
灰色年华 2020-12-08 01:31

Under a 32-bit operating system, where maximum memory allocated to any one program is limited, Mathematica gracefully terminates the kernel and returns a max memory allocati

相关标签:
1条回答
  • 2020-12-08 02:17

    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];
    

    Remember to run

    RemoveScheduledTask[ScheduledTasks[]];
    

    to disable it.

    Edit

    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}];
    

    enter image description here

    0 讨论(0)
提交回复
热议问题