I need to check how much memory is allocated in the heap. Is there a way to get this value programmatically with C#?
我需要检查堆中分配了多少内存。有没有办法用C#以编程方式获取此值?
I know about the System.Runtime.InteropServices.Marshal.SizeOf(...)
but that only tells me the size of an object.
我知道System.Runtime.InteropServices.Marshal.SizeOf(...),但它只告诉我一个对象的大小。
2 个解决方案
#1
Using a PerformanceCounter you can query the "# Bytes in all Heaps", from your own process, and even other processes.
使用PerformanceCounter,您可以从您自己的进程甚至其他进程中查询“所有堆中的#字节数”。
Use the Category ".Net CLR Memory" to see a lot of counters available.
使用类别“.Net CLR Memory”可以查看许多可用的计数器。
You have to see what the difference in system load is between the PerformanceCounter and the GC.GetTotalMemory that is proposed by Jon Skeet.
您必须了解PerformanceSounter与Jon Skeet提出的GC.GetTotalMemory之间的系统负载有何不同。
#2
Does GC.GetTotalMemory
do everything you need?
GC.GetTotalMemory能做你需要的一切吗?
(Note that SizeOf
only tells you the marshalled size, too - not necessarily the size in memory.)
(请注意,SizeOf只会告诉您编组的大小 - 不一定是内存中的大小。)
#1
Using a PerformanceCounter you can query the "# Bytes in all Heaps", from your own process, and even other processes.
使用PerformanceCounter,您可以从您自己的进程甚至其他进程中查询“所有堆中的#字节数”。
Use the Category ".Net CLR Memory" to see a lot of counters available.
使用类别“.Net CLR Memory”可以查看许多可用的计数器。
You have to see what the difference in system load is between the PerformanceCounter and the GC.GetTotalMemory that is proposed by Jon Skeet.
您必须了解PerformanceSounter与Jon Skeet提出的GC.GetTotalMemory之间的系统负载有何不同。
#2
Does GC.GetTotalMemory
do everything you need?
GC.GetTotalMemory能做你需要的一切吗?
(Note that SizeOf
only tells you the marshalled size, too - not necessarily the size in memory.)
(请注意,SizeOf只会告诉您编组的大小 - 不一定是内存中的大小。)