java heap memory matlab,如何直接在Matlab中增加Java堆大小?

I would like to increase the Java heap size directly by Matlab command because I need dynamic control depending on the environment.

I know the thread How do I increase the heap space for the Java VM in MATLAB 6.0 (R12) and later versions where they say to use java.opts file but I would like to do the same with Matlab prompt directly.

There are many threads that offer to change the parameters only through Matlab GUI or the file, like Matlab's blog post Controlling the Java Heap Size.

I am interested in increasing these parameters dynamically

heapSizeMemoryDefault = 521142272; % 512 MB

assert(java.lang.Runtime.getRuntime.maxMemory > heapSizeMemoryDefault*2, 'Java heap size too small');

runTimeMemoryDefault = 90116624; % about 890 MB

assert(java.lang.Runtime.getRuntime.freeMemory > 90116624*2, 'Java free memory size too small');

How can you increase Java heap size directly in Matlab 2016a?

解决方案

There are a few ways to change java heap size programmatically, but all require Matlab restart, since heap size is allocated at startup.

To query max heap size setting from preferences file:

oldMaxHeapSize = com.mathworks.services.Prefs.getIntegerPref('JavaMemHeapMax'); % MB

Zero result means there is no setting in the preference file yet, which results in some default size allocated.

To set max heap size setting in preferences file:

com.mathworks.services.Prefs.setIntegerPref('JavaMemHeapMax', 2048); % MB

Remember to restart Matlab, e.g. use quit() to end current process. Tested and works on Matlab >= R2013b.

Note that these commands are basically just a programmatic way to edit the matlab.prf file, located in Windows installations at (use correct Matlab version) C:\Users\%USERNAME%\AppData\Roaming\MathWorks\MATLAB\R2013b\matlab.prf

E.g. in this case, we're editing the following setting (you may open the file after you execute the command to check new value is set)

JavaMemHeapMax=I2048

There are plenty of other useful settings in the file which can be queried / set in a similar fashion.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值