matlab 声音 mwnumericarray,Is there a memory leak when working with MWStructArrays in MATLAB Builder J...

The problem here is with the anonymous MWArrays which you are creating to fill the struct array with. We will try to explain this graphically, but for this we slightly simplify the example: we use only one field: "Field1" and set KMAX = 1.

You start with creating the MWStructArray:

MWStructArraydata5 = new MWStructArray(1, KMAX, FIELDS);

38fe579f885aefd9f465006039213ca3.png

You then call:

data5.set(FIELDS[0], k + 1, new MWNumericArray(k * 1.13));

Where the new MWNumericArray(k * 1.13) part creates a new anonymous MWNumericArray with an underlying native MATLAB array with the actual data:

b92bca40786acd9ae0e88d36b63903fb.png

And the set operation makes, in accordance to the documentation"If element is of type MWArray, the cell at index set to a shared copy of the underlying MATLAB array", Field1 point to the underlying native MATLAB array:

304a007e608d09dd5c39f2519710f159.png

Then when your code continues, the anonymous array goes out of scope. However in Java going out of scope means that it gets marked for being disposed, it does not necessarily mean that the object is really deleted at that time; Java itself determines whether and when to run the object's finalizer.

It is important to note here that the Java Garbage Collector is not aware of the size of the Native MATLAB array to which the anonymous MWNumericArray is keeping a reference; it only sees the relatively small Java part of the MWNumericArray. This could lead to the Garbage Collector deciding to never release the anonymous MWNumericArray at all (i.e. it does not think it is worth the CPU cycles to clear the array which is using only a little amount of memory anyway).

So you will have a situation:

67cd4c5fc32815c67c713e215a076f3c.png

Then when you do specifically dispose of the MWStructArray with MWarray.disposeArray, data5 is really deleted and not only marked for deletion. This will also delete Field1 and its reference to the Native MATLAB Array, leaving:

6ac661f4278eafd364263327d4f30c19.png

But as you can see the anonymous MWNumericArray may still exist and if so, it does still have a reference to the Native MATLAB Array and as long as this is true, the Native MATLAB Array may not be deleted. This may lead to something which looks like a memory leak, but in fact it is not, it is how finalizers and garbage collection work in Java in combination with how MWArrays may have large underlying native data.

Now how to prevent this from happening?

There are two options:

Instead of using an anonymous array you can use a temporary named variable which you can specifically dispose:

MWNumericArraytemp = new MWNumericArray(k * 1.13);

data5.set(FIELDS[0], k + 1, temp);

MWArray.disposeArray(temp);

The following illustrations show what happens in this situation:

MWNumericArraytemp = new MWNumericArray(k * 1.13);

7543362cd0f6bc0d3d35ccfabf3f2c6f.png

data5.set(FIELDS[0], k + 1, temp);

afc871f4828c558fbe158ae613d88ece.png

MWArray.disposeArray(temp);

2c69f4b6d83a4c524f7cb393dcf33a19.png

If you then also call MWArray.disposeArray(data5); all references to the native MATLAB array are removed and then the native array will also be cleared immediately.

Or even easier: the set method directly accepts native Java datatypes:

data5.set(FIELDS[0], k + 1, k*1.13);

Which would basically directly create:

d9a47f4f4920173433e1d0da95cef59e.png

Where again MWArray.disposeArray(data5); would remove all references to the native array and clear it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值