C#方法会占用内存吗? [英] Does C# method take memory?

查看:427
本文介绍了C#方法会占用内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当有一个包含50个方法的类并且我们创建该类的50个对象实例时,内存中会发生什么? object具有很多功能且内存使用较少的最佳解决方案是什么?

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?

推荐答案

是的,C#/.Net方法需要基于每个AppDomain的内存,而方法/属性没有按实例的成本.

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

费用来自:

  • 方法元数据(类型的一部分)和IL.我不确定IL会保持加载多长时间,因为它真的只需要JIT,所以我猜它会根据需要加载并丢弃.
  • 在方法被JIT之后,机器代码会一直保留到卸载AppDomain(或者如果编译为中性,直到进程终止)

因此使用50个方法实例化1个或50个对象将不需要为方法使用不同的内存量.

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

这篇关于C#方法会占用内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆