MATLAB Community

MATLAB, community & more

从MATLAB调用Java,内存问题

跟进我以前有关使用的文章MATLAB中的Java对象,本周我将讨论可能出现的记忆问题。

当在MATLAB工作区中创建Java对象时,它实际上是在Java Heap空间中创建的,而不是MATLAB使用的主要内存。在32位Windows计算机上,MATLAB最多可用于创建矩阵和其他MATLAB对象,但只有64或128 MB(默认情况下)用于创建Java对象。为了找出可用于创建Java对象的内存,有三个重要性:免费内存,总内存和最大内存。

免费内存是用于创建新对象的堆空间中可用的字节数。总内存是分配给总堆空间的字节数,即创建对象的大小 +自由存储器。当自由存储器的数量开始降低时,Java将增加总内存,直到它等于最大内存为止。您可以在以下动画图中看到这一点,当我创建大型Java双打时,绿色(总内存)会碰到。蓝色的跳跃表示我操纵编辑器和命令窗口设置此演示时创建的对象,而倾角是当这些对象收集垃圾的时候,可以释放内存。实际上的双阵列在此图表上未显示,因为它的创建和清除速度要比图的采样时间快。

graph showing how MATLAB manages java memory

This is the demo that was running at the time of the jump: you can see the commands to discover the java memory, as well as the fact that the array creation ups the used java memory but does not affect the memory for MATLAB array creation:

free = java.lang.Runtime.getRuntime.freeMemory total = java.lang.Runtime.getRuntime.totalMemory max = java.lang.Runtime.getRuntime.maxMemory feature('memstats')x = javaarray('java.lang.Double',,,,400000);为了i=1:400000 x(i)=java.lang.Double(i);结尾
free = 13271784总计= 65470464最大= 130875392物理内存(RAM):在使用中:1911 MB(777755000)免费:1655 MB(677F0000)总计:总计:3567 MB(DEF45000)页面文件(SWAP空间):2205 MB(2205 MB(2205 MB)89dd3000) Free: 3243 MB (cab14000) Total: 5448 MB (1548e7000) Virtual Memory (Address Space): In Use: 585 MB (24928000) Free: 1462 MB (5b6b8000) Total: 2047 MB (7ffe0000) Largest Contiguous Free Blocks: 1. [at 320b5000] 459 MB (1cb9b000) 2. [at 4edf6000] 279 MB (117da000) 3. [at 21810000] 263 MB (107f0000) 4. [at 605d9000] 146 MB ( 9217000) 5. [at 6d346000] 71 MB ( 470a000) 6. [at 697f6000] 58 MB ( 3a3a000) 7. [at 71ce7000] 19 MB ( 1319000) 8. [at 73026000] 18 MB ( 126a000) 9. [at 7e4a1000] 18 MB ( 124f000) 10. [at 7d1d7000] 18 MB ( 1239000) ======= ========== 1352 MB (548cb000) ans = 481931264
free = java.lang.Runtime.getRuntime.freeMemory total = java.lang.Runtime.getRuntime.totalMemory max = java.lang.Runtime.getRuntime.maxMemory feature('memstats'
自由= 31137576总= 31137576 max = 130875392Physical Memory (RAM): In Use: 1919 MB (77f2a000) Free: 1648 MB (6701b000) Total: 3567 MB (def45000) Page File (Swap space): In Use: 2238 MB (8be27000) Free: 3210 MB (c8ac0000) Total: 5448 MB (1548e7000) Virtual Memory (Address Space): In Use: 584 MB (248b8000) Free: 1463 MB (5b728000) Total: 2047 MB (7ffe0000) Largest Contiguous Free Blocks: 1. [at 320b5000] 459 MB (1cb9b000) 2. [at 4edf6000] 279 MB (117da000) 3. [at 21810000] 263 MB (107f0000) 4. [at 605d9000] 146 MB ( 9217000) 5. [at 6d346000] 71 MB ( 470a000) 6. [at 697f6000] 58 MB ( 3a3a000) 7. [at 71ce7000] 19 MB ( 1319000) 8. [at 73026000] 18 MB ( 126a000) 9. [at 7e4a1000] 18 MB ( 124f000) 10. [at 7d1d7000] 18 MB ( 1239000) ======= ========== 1352 MB (548cb000) ans = 481931264

一个警告是,您可能创建的任何对象以及由桌面,编辑器,当前文件夹等创建的对象之间共享此空间。甚至可能没有足够的空间来创建一个失调的对话框。

有一个解决方法的解决方案通常是安全的,尽管如果数字太大,有时会产生意想不到的后果。那就是创建或修改java.optsfile in the MATLAB startup directory. This allows you to pass JVM options (such as the total and max memory for the heap) on MATLAB startup. Instructions can be found atthis solution

|
  • 打印
  • 发送电子邮件

Comments

要发表评论,请单击here登录您的数学帐户或创建一个新帐户。