This question already has an answer here:
这个问题已经有了答案:
- Find size of object instance in bytes in c# 12 answers
- 在c# 12回答中以字节为单位查找对象实例的大小
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>
.
我需要知道我的对象在内存(c#)中消耗了多少字节。例如我的Hashtable、SortedList或List
6 个解决方案
#1
144
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;
}
#2
91
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.
一种方法是使用GC。GetTotalMemory方法来度量创建对象之前和之后使用的内存数量。这并不完美,但只要您控制应用程序的其余部分,您就可能获得感兴趣的信息。
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.
除此之外,还可以使用分析器来获取信息,或者可以使用分析api来获取代码中的信息。但我认为这并不容易使用。
See Find out how much memory is being used by an object in C#? for a similar question.
请查看c#中对象使用了多少内存?类似的问题。
#3
22
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.
现在,我们如何计算缓存对象的内存需求呢?大多数人都知道,Int32和float是四个字节,double和DateTime 8字节,char实际上是两个字节(不是一个字节),等等。字符串有点复杂,2*(n+1)其中n是字符串的长度。对于对象,它将依赖于它们的成员:只需要总结所有成员的内存需求,记住所有的对象引用仅仅是一个32位框上的4个字节指针。实际上,这并不完全正确,我们没有考虑堆中每个对象的开销。我不确定您是否需要关注这个问题,但是我想,如果您将使用大量的小对象,您将不得不考虑开销。每个堆对象的成本和它的基本类型一样多,加上对象引用的4个字节(在32位机器上,尽管BizTalk在64位机器上运行32位),加上类型对象指针的4个字节,我认为同步块索引的4字节。为什么额外的开销很重要?假设我们有一个有两个Int32成员的类;在这种情况下,内存需求是16字节而不是8字节。
#4
20
Unmanaged object:
非托管对象:
Marshal.SizeOf(object yourObj);
- 元帅。SizeOf(对象yourObj);
Value Types:
值类型:
sizeof(object val)
- sizeof(对象val)
Managed object:
管理对象:
- Looks like there is no direct way to get for managed objects, Ref: http://blogs.msdn.com/cbrumme/archive/2003/04/15/51326.aspx
- 看起来没有直接的方法来获取托管对象,Ref: http://blogs.msdn.com/cbrumme/archive/2003/04/15/51326.aspx。
#5
14
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.
下面的代码片段应该返回传递给它的任何对象的字节大小,只要它可以被序列化。我从Quixant的一个同事那里得到这个答案,是为了解决在游戏平台上给SRAM写信的问题。希望它能帮助。谢谢卡罗·维图西。
/// <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;
}
#6
1
In debug mode
在调试模式下
load SOS
负载SOS
and execute dumpheap command.
和执行dumpheap命令。
#1
144
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;
}
#2
91
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.
一种方法是使用GC。GetTotalMemory方法来度量创建对象之前和之后使用的内存数量。这并不完美,但只要您控制应用程序的其余部分,您就可能获得感兴趣的信息。
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.
除此之外,还可以使用分析器来获取信息,或者可以使用分析api来获取代码中的信息。但我认为这并不容易使用。
See Find out how much memory is being used by an object in C#? for a similar question.
请查看c#中对象使用了多少内存?类似的问题。
#3
22
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.
现在,我们如何计算缓存对象的内存需求呢?大多数人都知道,Int32和float是四个字节,double和DateTime 8字节,char实际上是两个字节(不是一个字节),等等。字符串有点复杂,2*(n+1)其中n是字符串的长度。对于对象,它将依赖于它们的成员:只需要总结所有成员的内存需求,记住所有的对象引用仅仅是一个32位框上的4个字节指针。实际上,这并不完全正确,我们没有考虑堆中每个对象的开销。我不确定您是否需要关注这个问题,但是我想,如果您将使用大量的小对象,您将不得不考虑开销。每个堆对象的成本和它的基本类型一样多,加上对象引用的4个字节(在32位机器上,尽管BizTalk在64位机器上运行32位),加上类型对象指针的4个字节,我认为同步块索引的4字节。为什么额外的开销很重要?假设我们有一个有两个Int32成员的类;在这种情况下,内存需求是16字节而不是8字节。
#4
20
Unmanaged object:
非托管对象:
Marshal.SizeOf(object yourObj);
- 元帅。SizeOf(对象yourObj);
Value Types:
值类型:
sizeof(object val)
- sizeof(对象val)
Managed object:
管理对象:
- Looks like there is no direct way to get for managed objects, Ref: http://blogs.msdn.com/cbrumme/archive/2003/04/15/51326.aspx
- 看起来没有直接的方法来获取托管对象,Ref: http://blogs.msdn.com/cbrumme/archive/2003/04/15/51326.aspx。
#5
14
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.
下面的代码片段应该返回传递给它的任何对象的字节大小,只要它可以被序列化。我从Quixant的一个同事那里得到这个答案,是为了解决在游戏平台上给SRAM写信的问题。希望它能帮助。谢谢卡罗·维图西。
/// <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;
}
#6
1
In debug mode
在调试模式下
load SOS
负载SOS
and execute dumpheap command.
和执行dumpheap命令。