如何获取内存中的对象大小? [英] How to get object size in memory?
本文介绍了如何获取内存中的对象大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道我的对象在内存中消耗了多少字节(在 C# 中).例如,我的 Hashtable
或 SortedList
或 List
有多少.
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>
.
推荐答案
这可能不准确,但对我来说已经足够了
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;
}
这篇关于如何获取内存中的对象大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文