Does C# method take memory?

≡放荡痞女 提交于 2020-01-11 09:50:07

问题


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 with a lot of functionality and less memory usage?


回答1:


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.




回答2:


Firstly the memory usage and functionality is not based on the object, its based on the class of which we are making an object... hence if you are making a object of a class you are able to access all the functionality of that class.. and according to me its a better option to use a object for accessing a class functionality



来源:https://stackoverflow.com/questions/48860851/does-c-sharp-method-take-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!