C# 如何获取内存中的对象大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/605621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get object size in memory?
提问by Lukas ?alkauskas
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<String>
。
回答by NileshChauhan
Unmanaged object:
非托管对象:
Marshal.SizeOf(object yourObj);
Marshal.SizeOf(object yourObj);
Value Types:
值类型:
sizeof(object val)
sizeof(object val)
Managed object:
管理对象:
- Looks like there is no direct way to get for managed objects, Ref: https://devblogs.microsoft.com/cbrumme/size-of-a-managed-object/
- 看起来没有直接获取托管对象的方法,参考:https: //devblogs.microsoft.com/cbrumme/size-of-a-managed-object/
回答by Rune Grimstad
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 apito 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# 中的对象使用了多少内存?对于类似的问题。
回答by Rune Grimstad
In debug mode
在调试模式
load SOS
加载求救
and execute dumpheap command.
并执行 dumpheap 命令。
回答by Rush Frisby
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;
}
回答by Kevin Hirst
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 的问题。希望它有所帮助。感谢 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;
}
回答by Aliostad
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 实际上是两个字节(不是一个字节),依此类推。String 有点复杂,2*(n+1),其中 n 是字符串的长度。对于对象,它将取决于它们的成员:只需总结其所有成员的内存需求,记住所有对象引用只是 32 位框上的 4 字节指针。现在,这实际上并不完全正确,我们没有考虑堆中每个对象的开销。我不确定您是否需要关注这一点,但我想,如果您将使用大量小对象,则必须考虑开销。每个堆对象的成本与其原始类型一样多,加上用于对象引用的四个字节(在 32 位机器上,尽管 BizTalk 在 64 位机器上也运行 32 位),加上用于类型对象指针的 4 个字节,我认为 4 个字节用于同步块索引。为什么这个额外的开销很重要?好吧,让我们假设我们有一个有两个 Int32 成员的类;在这种情况下,内存要求是 16 字节而不是 8 字节。