静态方法总是保存在内存中吗? [英] Are static methods always held in memory?

查看:39
本文介绍了静态方法总是保存在内存中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的整个开发团队都认为,使用静态方法很糟糕.

My whole development team thinks, static methods are a terrible thing to use.

在某些情况下,我真的没有看到任何缺点.以前当我需要一个无状态方法时,我总是为此使用静态方法.

I really don't see any disadvantages in some cases. When I needed a stateless method before, I always used static methods for that purpose.

我同意他们的一些观点,例如我知道它们很难测试(尽管并非不可能).

I agree with some of their points, e.g. I know they are quite hard to test (although it's not impossible).

我不明白的是,他们声称,静态方法始终保存在内存中,并将填充基本内存使用量.因此,如果您在程序中使用 100 个静态方法,则当程序启动时,所有方法都会加载到内存中,并且会不必要地填充内存.此外,静态方法会增加内存泄漏的风险.

What I don't get is, that they claim, static methods are always held in memory and will fill the basic memory usage. So, if you are using 100 static methods in your program, when the program starts all methods are loaded into memory and will fill the memory unnecessarily. Furthermore static methods increase the risk of memory leaks.

这是真的吗?

为了调用方法而不得不创建一个类的新实例是很不方便的.但这就是他们现在所做的,在方法中间创建一个实例并调用该方法,这可能只是一个静态方法.

It's quite inconvenient to have to create a new instance of a class just to call the method. But thats how they do it right now, the create a instance in mid of a method and call that method, that could be just a static one.

推荐答案

就内存而言,静态方法和实例方法之间没有区别.实例方法只有一个额外的参数,this.其他一切完全相同.此外,扩展方法很容易添加到 C# 的基本方式,所需要的只是语法来公开隐藏的 this 参数.

There is no distinction between static and instance methods as far as memory is concerned. Instance methods only have an extra argument, this. Everything else is exactly the same. Also the basic way in which extension methods were easy to add to C#, all that was needed was syntax to expose that hidden this argument.

方法占用机器代码的空间,处理器执行的实际代码.以及描述该方法如何存储对象的表格,它有助于垃圾收集器发现保存在局部变量和 CPU 寄存器中的对象根.此空间取自加载程序堆",这是 CLR 创建的与 AppDomain 关联的内部数据结构.只发生一次,当方法第一次执行时,及时.释放该空间需要卸载该应用程序域.静态变量也在加载器堆中分配.

Methods occupy space for their machine code, the actual code that the processor executes. And a table that describes how the method stores objects, that helps the garbage collector to discover object roots held in local variables and CPU registers. This space is taken from the "loader heap", an internal data structure that the CLR creates that is associated with the AppDomain. Happens just once, when the method first executes, just-in-time. Releasing that space requires unloading that appdomain. Static variables are also allocated in the loader heap.

不要抛弃静态方法的巨大优势.它们可以大大提高代码的可读性和可维护性.由于他们的合同,他们无法改变对象状态.因此,它们几乎没有副作用,因此很容易推理它们的作用.然而,如果他们让你添加静态变量,那么他们做的恰恰相反,全局变量是邪恶的.

Do not throw away the big advantage of static methods. They can greatly improve the readability and maintainability of code. Thanks to their contract, they cannot alter the object state. They can therefore have very few side-effects, makes it really easy to reason about what they do. However, if they make you add static variables then they do the exact opposite, global variables are evil.

这篇关于静态方法总是保存在内存中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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