找出 .NET 对象的大小 [英] Find out the size of a .NET object

查看:24
本文介绍了找出 .NET 对象的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出我的对象占用了多少内存,以查看其中有多少会出现在 大对象堆(超过 85,000 字节的任何东西).

I'm trying to find out how much memory my objects take to see how many of them are ending up on the Large Object Heap (which is anything over 85,000 bytes).

是否像为 int 添加 4、为 long 添加 8、为每个对象的任何引用类型等添加 4(或 8,如果您使用 64 位)一样简单,或者是否存在方法、属性等的开销.?

Is it as simple as adding 4 for an int, 8 for a long, 4 (or 8 if you're on 64 bit) for any reference types etc for each object, or are there overheads for methods, properties, etc.?

推荐答案

不要忘记实际对象的大小不包括它引用的任何对象的大小.

Don't forget that the size of an actual object doesn't include the size of any objects it references.

可能最终出现在大对象堆上的唯一东西是数组和字符串——其他对象本身往往相对较小.即使具有(例如)10 个引用类型变量(x86 上每个 4 个字节)和 10 个 GUID(每个 16 个字节)的对象也只会占用大约 208 个字节(类型引用和同步块有一些开销).

The only things which are likely to end up on the large object heap are arrays and strings - other objects tends to be relatively small in themselves. Even an object with (say) 10 reference type variables (4 bytes each on x86) and 10 GUIDs (16 bytes each) is only going to take up about 208 bytes (there's a bit of overhead for the type reference and sync block).

同样,在考虑数组的大小时,不要忘记如果元素类型是引用类型,那么只有引用的大小才对数组本身计数.换句话说,即使你有一个包含 20,000 个元素的数组,即使它引用了更多的数据,数组对象本身的大小也只会超过 80K(在 x86 上).

Likewise when thinking about the size of an array, don't forget that if the element type is a reference type, then it's only the size of the references that count for the array itself. In other words, even if you've got an array with 20,000 elements, the size of the array object itself will only be just over 80K (on x86) even if it references a lot more data.

这篇关于找出 .NET 对象的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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