限制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屋!