Determine how much memory a class uses?

后端 未结 3 477
醉梦人生
醉梦人生 2021-01-17 21:42

I am trying to find a way to determine at run-time how much memory a given class is using in .NET. Using Marshal.SizeOf() is out, as it only works on value typ

相关标签:
3条回答
  • 2021-01-17 22:17

    just link to related SO question:

    • sizeof() equivalent for reference types?
    0 讨论(0)
  • 2021-01-17 22:21

    I agree that a memory profiler is the easiest way to get the information you are looking for. In addition to the two previously mentioned, I recommend JetBrains dotTrace, which is both a performance profiler and a memory profiler.

    If you want to do it yourself, and are willing to get pretty deep into the guts of the CLR, you can use the .NET Profiling API, which is an unmanaged API that (as Microsoft says): "enables a profiler to monitor a program's execution by the common language runtime (CLR)." It's not exactly intended for casual use, but it does have an enormous amount of functionality.

    0 讨论(0)
  • 2021-01-17 22:39

    I've only recently started looking into this type of thing, but i have found that memory profilers can give quite detailed information regarding instances of objects within your application.

    Here are a couple that are worth trying:

    • ANTS Profiler
    • .NET Memory Profiler
    0 讨论(0)
提交回复
热议问题