在内存中对象的大小对象、大小、内存

由网友(抓住青春的尾巴)分享简介:可能重复: 如何获得对象的大小在内存中? 是否有可能知道,很明显在运行时,所采取的一个对象的内存?怎么样?我特别想知道RAM的数量占据。Is it possible to know, obviously at runtime, the memory taken by an object? How? Specifi...

可能重复:   如何获得对象的大小在内存中?

是否有可能知道,很明显在运行时,所采取的一个对象的内存?怎么样?我特别想知道RAM的数量占据。

Is it possible to know, obviously at runtime, the memory taken by an object? How? Specifically I'd like to know the amount of RAM occupied.

推荐答案

对于值类型,使用的sizeof(对象的值)

有关非托管对象使用 Marshal.SizeOf(obj对象)

遗憾的是上述两种不会得到你所引用对象的大小。的

有关管理对象:有没有直接的方法来获得的RAM,他们使用的管理对象的大小,请参见:http://blogs.msdn.com/cbrumme/archive/2003/04/15/51326.aspx

For managed object: There is no direct way to get the size of RAM they use for managed objects, see: http://blogs.msdn.com/cbrumme/archive/2003/04/15/51326.aspx

或替代方案:

System.GC.GetTotalMemory

long StopBytes = 0;
foo myFoo;

long StartBytes = System.GC.GetTotalMemory(true);
myFoo = new foo();
StopBytes = System.GC.GetTotalMemory(true);
GC.KeepAlive(myFoo); // This ensure a reference to object keeps object in memory

MessageBox.Show("Size is " + ((long)(StopBytes - StartBytes)).ToString());

来源:http://blogs.msdn.com/b/mab/archive/2006/04/24/582666.aspx

概述

使用分析器将是最好的。

Using a profiler would be the best.

阅读全文

相关推荐

最新文章