Does C# method take memory?

后端 未结 2 1766
南方客
南方客 2021-01-19 14:28

What happens in memory when there is a class with 50 methods and we create 50 object instances of that class? What is the best solution for having an object wit

2条回答
  •  半阙折子戏
    2021-01-19 15:03

    Yes, C#/.Net methods require memory on per-AppDomain basis, there is no per-instance cost of the methods/properties.

    Cost comes from:

    • methods metadata (part of type) and IL. I'm not sure how long IL stays loaded as it really only needed to JIT so my guess it is loaded as needed and discarded.
    • after method is JITed machine code stays till AppDomain is unloaded (or if compiled as neutral till process terminates)

    So instantiating 1 or 50 objects with 50 methods will not require different amount of memory for methods.

提交回复
热议问题