我怎样才能知道一个句柄对象在Matlab中使用了多少内存 [英] How can I tell how much memory a handle object uses in matlab

查看:107
本文介绍了我怎样才能知道一个句柄对象在Matlab中使用了多少内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我声明一个对象为handle的子类 classdef obj<处理 我的对象现在实质上是某个地方的某些内存的指针".如何找出我的对象用完了多少内存?

If I declare an object to be a subclass of handle classdef obj < handle my object is now essentially a "pointer" to some memory somewhere. How do I find out how much memory my object is using up?

例如,说我有一个带有栏的foo类

For example, say I have a class foo with a field bar

classdef foo < handle 
properties
    bar = randn(1000);
end

条占用8兆字节(8个字节* 100万个数字)

bar takes up 8 megabytes (8 bytes * 1 million numbers)

但是如果我输入

obj = foo();
whos('obj');

我知道

Name      Size            Bytes  Class    Attributes

  obj      1x1                60  foo                

我如何找出obj指向多少内存?

How do I find out how much total memory obj points to?

推荐答案

作为一种技巧,将其转换为结构并查看占用了多少空间.我认为这将公开常规"对象字段中的所有数据.

As a hack, convert it to a struct and see how much space that takes up. I think that will expose all the data in "regular" object fields.

f = foo();
origWarn = warning();
warning off 'MATLAB:structOnObject'
s = builtin('struct', f); % use 'builtin' in case @foo overrides struct()
warning(origWarn);

然后您可以在Whos中看到它.

Then you can see it in whos.

>> whos
  Name      Size              Bytes  Class     Attributes

  f         1x1                  60  foo                 
  s         1x1             8000124  struct       

这只是一阶近似值.它会告诉您其字段正在使用多少内存.如果它们中的任何一个包含句柄对象,则需要递归向下递归该结构的字段,并将任何其他句柄对象转换为该结构以计数其字段. (如果要包括Java对象的内存,则还需要一个单独的函数来估计它们的存储大小.可能不那么麻烦.)现在,Matlab具有闭包,函数句柄中可能还包含数据;因此,请参见参考资料.如果您要计算封闭数据,则需要使用functions()进行插补.

This is just a first order approximation. It will tell you how much memory its fields are using. If any of them contain handle objects, you need to recurse down the fields of that struct and convert any other handle objects to struct to count their fields. (If you want to include Java objects' memory, you'll also need a separate function to estimate their storage size. Probably not worth the bother.) Now that Matlab has closures, function handles may also contain data; you'll need to punch in to those using functions() if you want to count closed-over data.

如果要使用句柄对象,则可能在M代码级别具有别名甚至循环引用,因此在递归时需要注意这一点. (对不起,我不知道如何在新的OO系统中处理该问题.)

If you're working with handle objects, you may have aliasing and even circular references at the M-code level, so you'd need to watch out for that when recursing. (Sorry, I don't know how to handle that in the new OO system.)

whos中的内存显示还将对通过Matlab的写时复制优化共享内存的数组进行两次计数.这是一个具体的例子.

The memory display in whos will also double-count arrays that are sharing memory via Matlab's copy-on-write optimization. Here's a concrete example.

x = NaN(1,10000);
s.x = x;
s.y = x;
s.z = x;


>> whos
  Name      Size                Bytes  Class     Attributes

  s         1x1                240372  struct              
  x         1x10000             80000  double              

实际上,s仅消耗约80K;它只包含三个指向x的指针. 80K与x本身消耗的80K相同.除非您修改其中任何一个;否则,然后分配一个新的数组. Whos()不会让您区分这些情况.处理起来很困难; AFAIK唯一的方法是使用MEX文件获取mxarray的数据指针,然后自己遍历对象树,检测别名指针并计算别名字节.

In reality, s is only consuming about 80K; it just contains three pointers to x. And that 80K is the same 80K that x itself is consuming. Unless you modify any of them; then a new array is allocated. Whos() won't let you differentiate those cases. Handling this is hard; AFAIK the only way to do this is to use a MEX file to get the mxarray's data pointer(s) and walk the object tree yourself, detecting aliased pointers and counting the aliased bytes.

这是在共享对象的组件时测量内存中对象大小的一个普遍问题.它们不是离散的物理对象.至少您不是C语言,而是使用指向任意内存块的指针.

This is a general problem with measuring the size of objects in memory when their components could be shared. They're not discrete physical objects. At least you're not in C, working with pointers to arbitrary memory blocks.

这篇关于我怎样才能知道一个句柄对象在Matlab中使用了多少内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆