限制 Mathematica 内存使用的正确方法? [英] Correct way to cap Mathematica memory use?
问题描述
在 32 位操作系统下,分配给任何程序的最大内存是有限的,Mathematica 会优雅地终止内核并返回最大内存分配错误.
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 allocation error.
然而,在 64 位操作系统上,Mathematica 将自由使用所有可用内存并使系统停止.因此,限制内存使用量的正确方法是什么?可以将 MemoryConstrained
与 $Pre
或 CellEvaluationFunction
结合使用,但我宁愿不为此目的绑定其中任何一个,或者必须修改现有的用于合并此功能.
On a 64-bit OS however, Mathematica will freely use all the memory available and grind the system to a halt. Therefore, what is the correct way to cap memory usage? One could use MemoryConstrained
combined with $Pre
or CellEvaluationFunction
but I would rather not tie up either of those for this purpose, or have to modify existing uses to incorporate this function.
是否有其他方法可以全局限制内存使用,例如内核标志或系统 $Option?
Is there another way to globally restrict memory usage, such as a kernel flag, or system $Option?
推荐答案
在 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}];
这篇关于限制 Mathematica 内存使用的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!