本文介绍了打开太多数字时如何避免MATLAB崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有时候我会启动一个MATLAB脚本并且意识到太晚了它会输出太多数字。最终我得到了一个Sometimes I start a MATLAB script and realize too late that it is going to output way too many figures. Eventually I get an可以在我的机器上轻松复制which can easily be reproduced on my machine usingfor i=1:inf figure;end我在标准设置崩溃之前达到约90个数字(首选项/ Java堆内存)堆,虽然将堆加倍到256 MB,给了我大约200个数字。I get to around ~90 figures before it crashes with the standard setting (Preferences / Java Heap Memory) of 128 MB Java heap, while doubling the Heap to 256 MB gives me around 200 figures. 您是否看到避免 Java错误消息的方法?如果没有足够的内存供另一个人使用,我希望我的脚本告诉而不是崩溃。Do you see any way to avoid the Java error message? If there is not enough memory for another figure, I'd like my script to be told rather than crash.也许我可以有数字的包装器(以某种方式?)检查有多少Java堆可用,哪些?如果没有足够的空间,拒绝打开新的数字?Maybe I could have a wrapper for figure which (somehow?) checks how much Java heap is available and which refuses to open a new figure if there is not enough space left? 更新使用下面的答案,我得到了一个很好的图表,说明了有多少可用内存Java:Using the answers below, I get a nice graph for how much free Memory Java has:这是使用for i=1:inf java.lang.Runtime.getRuntime.gc fprintf('%3.0f: %1.0f Byte free\n',i,java.lang.Runtime.getRuntime.freeMemory); figure;end我认为开头的增加意味着垃圾收集只做了一定的努力每次我打电话给它?I assume the increase in the beginning means that garbage collection only does a certain effort every time I call it? 更新2 - 我的解决方案使用帮助我到了这里,我实现了以下解决方案作为 figure.m ,它重载并调用内置数字命令:Using the help I got here, I implemented the following solution as a figure.m which overloads and calls the build-in figure command:function varargout=figure(varargin)memcutoff = 10E6; % keep at least this amount of bytes freememkeyboard= 3E6; % if memory drops below this, interrupt execution and go to keyboard modeglobal refuse_new_figuresif refuse_new_figures warning('jb:fig:lowjavamem2','Java WAS memory low -> refusing to create a new figure. To reset, type "global refuse_new_figures ; refuse_new_figures = [];"'); returnendfreemem=java.lang.Runtime.getRuntime.freeMemory;if freemem < memcutoff fprintf('Free memory is low (%1.0f Bytes) -> running garbace collector...\n',freemem); java.lang.Runtime.getRuntime.gcendfreemem=java.lang.Runtime.getRuntime.freeMemory;% fprintf('Free memory is %1.0f Bytes.\n',freemem);if freemem < memkeyboard warning('jb:fig:lowjavamem','Java memory very low -> going into interactive mode. Good luck!'); keyboard;endif freemem < memcutoff warning('jb:fig:lowjavamem','Java memory low -> refusing to create a new figure!'); refuse_new_figures=true;else if nargin > 0 if nargout > 0 varargout{1}=builtin('figure',varargin{:}); else builtin('figure',varargin{:}); end else if nargout > 0 varargout{1}=builtin('figure'); else builtin('figure'); end endend推荐答案一般情况下,我建议将最大Java堆内存设置为可用RAM的25%左右,这样可以打开大量数字(但不是无限数字)。如果您不能在首选项中执行此操作(例如,b / c,您有像我一样的Mac),此解决方案将有所帮助 - 它会覆盖首选项设置。In general, I'd suggest setting maximum Java Heap Memory to about 25% of the available RAM, which allows you to open lots of figures (but not infinite numbers). If you cannot do this in the preferences (e.g. b/c you have a Mac like mine), this solution will help - it overrides the preference settings.链接的解决方案还会告诉您剩余的免费Java内存量,以及可用的总数:运行以下命令:The linked solution also tells you how much free java memory you have left, and how much total is available: Run the following commands:java.lang.Runtime.getRuntime.maxMemoryjava.lang.Runtime.getRuntime.totalMemoryjava.lang.Runtime.getRuntime.freeMemory不幸的是,一个数字不占用固定数量的Java内存,空数比显示10k点的数字少得多,而最小化的数字占用的内存少于最大数据。但是,如果你可以估计每个数字所需的平均内存,你确实可以为数字编写一个包装器来检查这个数字是否可能是最后一个。或者/另外,您可以使包装函数最小化所有其他数字(参见未记录的Matlab for this)。Unfortunately, a figure doesn't take a fixed amount of Java memory, an empty figure takes much less than one displaying 10k points, and a minimized figure takes less memory than a maximized one. However, if you can estimate the average memory needed per figure, you can indeed write a wrapper for figure that checks whether it's likely that this figure will be the last. Alternatively/additionally, you could make the wrapper function minimize all other figures (see Undocumented Matlab for this). 编辑正如 @ Peter Lawrey ,你也可以在检查可用内存之前尝试执行垃圾收集 - 虽然我不知道Matlab是否会尝试,无论如何。EDIT As pointed out by @Peter Lawrey, you may also try and perform garbage collection before checking how much memory is available - though I don't know whether Matlab would try that, anyway. 这篇关于打开太多数字时如何避免MATLAB崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-14 20:57