有没有办法找出特定对象使用了多少内存?例如List。考虑全部因素,如字符串插值和编译器/运行环境等所做的任何操作。
有没有办法找出特定对象使用了多少内存?例如List。考虑全部因素,如字符串插值和编译器/运行环境等所做的任何操作。
您需要明确定义“特定对象使用了多少内存”,例如,您可能指的是“如果此对象被垃圾回收,将释放多少内存” - 或者您可能指的是“此对象及其所有关联对象占用了多少内存”。
您提到字符串池是一个很好的例子。假设您执行以下操作:
List<string> internedStrings = new List<string>();
List<string> nonInternedStrings = new List<string>();
for (int i=0; i < 1000; i++)
{
string tmp = new string(' ', i+1);
nonInternedStrings.Add(tmp);
tmp = tmp.Intern();
internedStrings.Add(tmp);
}
你试过 CLR Profiler 2.0 吗?