了解.NET对象的大小 [英] Find out the size of a .net object

查看:153
本文介绍了了解.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).

是不是简单的添加4类型为int,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).

同样,当考虑数组的大小,不要忘记,如果元素类型是引用类型,那么它的的引用仅大小的是计数数组本身。换句话说,即使你有一个数组20000元,数组对象的大小本身只是刚刚超过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天全站免登陆