如何获取内存中对象的大小?

发布于 2024-07-14 14:23:59 字数 131 浏览 5 评论 0原文

我需要知道我的对象在内存中消耗了多少字节(在 C# 中)。 例如我的 HashtableSortedListList 的数量。

I need to know how much bytes my object consumes in memory (in C#). for example how much my Hashtable, or SortedList, or List<String>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笑忘罢 2024-07-21 14:23:59

这可能不准确,但对我来说足够接近了

long size = 0;
object o = new object();
using (Stream s = new MemoryStream()) {
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(s, o);
    size = s.Length;
}

this may not be accurate but its close enough for me

long size = 0;
object o = new object();
using (Stream s = new MemoryStream()) {
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(s, o);
    size = s.Length;
}
你的笑 2024-07-21 14:23:59

我认为你不能直接获取它,但有几种方法可以间接找到它。

一种方法是使用 < code>GC.GetTotalMemory 方法来测量创建对象之前和之后使用的内存量。 这并不完美,但只要您控制应用程序的其余部分,您就可以获得您感兴趣的信息。

除此之外,您可以使用探查器来获取信息,或者您可以使用 分析 api 以获取代码中的信息。 但我认为这并不容易使用。

请参阅 了解有多少内存正在被 C# 中的对象使用吗? 对于类似的问题。

I don't think you can get it directly, but there are a few ways to find it indirectly.

One way is to use the GC.GetTotalMemory method to measure the amount of memory used before and after creating your object. This won't be perfect, but as long as you control the rest of the application you may get the information you are interested in.

Apart from that you can use a profiler to get the information or you could use the profiling api to get the information in code. But that won't be easy to use I think.

See Find out how much memory is being used by an object in C#? for a similar question.

凯凯我们等你回来 2024-07-21 14:23:59

非托管对象:

  • Marshal.SizeOf(object yourObj);

值类型:

  • sizeof(object val)

托管对象:

Unmanaged object:

  • Marshal.SizeOf(object yourObj);

Value Types:

  • sizeof(object val)

Managed object:

红玫瑰 2024-07-21 14:23:59

好的,这个问题已经得到解答,答案已被接受,但有人让我写下我的答案,所以你就可以了。

首先,不能肯定地说。 这是一个内部实现细节,没有记录。 但是,基于包含在其他对象中的对象。 现在,我们如何计算缓存对象的内存需求?

我之前曾在这篇文章中触及过这个主题:

现在,我们如何计算缓存的内存需求
物体? 好吧,正如你们大多数人所知,Int32 和 float 是四种
bytes、double和DateTime是8个字节,char实际上是两个字节(不是
一个字节),等等。 字符串有点复杂,2*(n+1),其中n
是字符串的长度。 对于对象来说,这取决于它们的
成员:只需总结其所有成员的内存需求,
记住所有对象引用只是 32 上的 4 字节指针
位框。 现在,这实际上并不完全正确,我们没有注意到
堆中每个对象的开销。 我不确定你是否需要
担心这个,但我想,如果你会使用很多
对于小物体,你必须将开销纳入
考虑。 每个堆对象的成本与其原始类型一样多,
加上四个字节用于对象引用(在 32 位机器上,尽管
BizTalk 在 64 位机器上也运行 32 位),加上 4 个字节
类型对象指针,我认为4个字节用于同步块索引。 为什么
这个额外的开销重要吗? 好吧,让我们想象一下我们有一个
具有两个 Int32 成员的类; 在这种情况下,内存需求是
16 个字节而不是 8 个字节。

OK, this question has been answered and answer accepted but someone asked me to put my answer so there you go.

First of all, it is not possible to say for sure. It is an internal implementation detail and not documented. However, based on the objects included in the other object. Now, how do we calculate the memory requirement for our cached objects?

I had previously touched this subject in this article:

Now, how do we calculate the memory requirement for our cached
objects? Well, as most of you would know, Int32 and float are four
bytes, double and DateTime 8 bytes, char is actually two bytes (not
one byte), and so on. String is a bit more complex, 2*(n+1), where n
is the length of the string. For objects, it will depend on their
members: just sum up the memory requirement of all its members,
remembering all object references are simply 4 byte pointers on a 32
bit box. Now, this is actually not quite true, we have not taken care
of the overhead of each object in the heap. I am not sure if you need
to be concerned about this, but I suppose, if you will be using lots
of small objects, you would have to take the overhead into
consideration. Each heap object costs as much as its primitive types,
plus four bytes for object references (on a 32 bit machine, although
BizTalk runs 32 bit on 64 bit machines as well), plus 4 bytes for the
type object pointer, and I think 4 bytes for the sync block index. Why
is this additional overhead important? Well, let’s imagine we have a
class with two Int32 members; in this case, the memory requirement is
16 bytes and not 8.

世俗缘 2024-07-21 14:23:59

以下代码片段应返回传递给它的任何对象的大小(以字节为单位),只要它可以序列化即可。
我从 Quixant 的一位同事那里得到了这个,以解决在游戏平台上写入 SRAM 的问题。 希望它能有所帮助。
感谢并感谢 Carlo Vittuci。

/// <summary>
/// Calculates the lenght in bytes of an object 
/// and returns the size 
/// </summary>
/// <param name="TestObject"></param>
/// <returns></returns>
private int GetObjectSize(object TestObject)
{
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    byte[] Array;
    bf.Serialize(ms, TestObject);
    Array = ms.ToArray();
    return Array.Length;
}

The following code fragment should return the size in bytes of any object passed to it, so long as it can be serialized.
I got this from a colleague at Quixant to resolve a problem of writing to SRAM on a gaming platform. Hope it helps out.
Credit and thanks to Carlo Vittuci.

/// <summary>
/// Calculates the lenght in bytes of an object 
/// and returns the size 
/// </summary>
/// <param name="TestObject"></param>
/// <returns></returns>
private int GetObjectSize(object TestObject)
{
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    byte[] Array;
    bf.Serialize(ms, TestObject);
    Array = ms.ToArray();
    return Array.Length;
}
蓝天 2024-07-21 14:23:59

在调试模式下

加载SOS

并执行dumpheap命令。

In debug mode

load SOS

and execute dumpheap command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文