在内存分配方面,类中的静态方法和普通方法有什么区别。 [英] What is difference between static method and normal method in class, in terms of memory allocation.

查看:195
本文介绍了在内存分配方面,类中的静态方法和普通方法有什么区别。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现静态和普通方法有很多不同,但大多数答案都是围绕成员的可访问性。但是也会对内存和生命周期产生影响。

任何人都可以告诉我一个很好的例子,当我们创建静态方法和普通方法时,RAM和静态方法的生命周期会发生什么。

先谢谢。



我的尝试:



已经知道静态类和普通类,静态函数/成员和普通属性之间的区别。

I have found many difference in static and normal method, but most of the answers were around accessibility of members. But there are impacts on memory and lifespan as well.
Can any one tell me with a good example that what happens with RAM and lifespan of static methods when we create static method and normal method.
Thanks in advance.

What I have tried:

Already know the difference between static class and normal class, static function/members and normal properties.

推荐答案

静态方法和实例都只是代码的一部分,不同之处在于实例方法总是至少使用一个参数'this'。



因此,就方法本身而言,它们占用的RAM数量相近。这个RAM将在模块加载时设置(首先作为MSIL,然后作为Jitted之后的本机代码),并且通常会在那里进行处理,直到进程死掉它的RAM。



在讨论方法时考虑内存方面是不寻常的,它传递给它们的数据以及它们通常关注的行为。
Both static methods and instance are just bits of code, the difference being that instance methods always take at least one parameter 'this'.

So, in terms of the methods themselves, they occupy similar amounts of RAM. This RAM will be set when the module loads (first as MSIL, then as native code after its Jitted) and will be there typically until the process dies taking its RAM with it.

It's unusual to think in terms of memory when talking about methods, its the data that gets passed to them and they act upon which is usually the concern.


静态方法没有一生。可以在不实例化类的情况下调用类的静态方法。换句话说,静态方法几乎可以随时调用。



静态成员的问题是必须实例化它们并在程序入口点之前初始化它们叫做。这是在调用各种DllMains并初始化库之后。但是,这些事件的确切时间是未指定的,因此出于这个原因,应该避免使用RTL函数来初始化静态成员,因为它们可能是不可预测的。这也适用于全局对象。
Static methods do not have a lifetime. A static method of a class can be called without instantiating the class. In other words, static methods can be called practically any time you want.

The gotcha with static members is they must be instantiated and they are initialized before the program entry point is called. This is just after the various DllMains are called and libraries are initialized. However, the exact timing of these events is unspecified so for this reason one should avoid using RTL functions to initialize static members because they can be unpredictable. This applies to global objects also.


主要区别是



1)静态变量存在于类型上,所以如果你在MyObject上有一个静态变量并且有100个MyObject对象,那么你只有一个静态变量的实例。如果变量不是静态的,那么就会有100个实例。



2)静态变量总是在堆上,即使它们在堆栈上也是如此是非静态的,所以即使你的静态变量是一个int,它仍然在堆中并像访问变量一样被访问,这样就会在内存管理等方面产生连锁效应。
The main differences are

1) The static variable exists on the type so if you have a static variable on MyObject and have 100 MyObject objects then you only have one instance of the static variable. If the variable wasn't static there would be 100 instances of it.

2) Static variables are always on the heap even if they would be on the stack if they were non-static, so even if your static variable is an int it is still in the heap and accessed like a reference variable so that will have a knock-on effect in terms of memory management etc.


这篇关于在内存分配方面,类中的静态方法和普通方法有什么区别。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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