java 静态成员有助于提高内存效率吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1496629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:45:52  来源:igfitidea点击:

Do static members help memory efficiency?

javamemory-management

提问by Hanno Fietz

If I have a class that I expect to be used in thousands of instances in a memory-sensitive application, does it help if I factor out static functionality to static members?

如果我有一个类,我希望在内存敏感的应用程序中的数千个实例中使用它,如果我将静态功能分解为静态成员是否有帮助?

I imagine that static methods and variables are stored once per class while for non-static members there has to be something stored for each instance.

我想每个类都存储静态方法和变量一次,而对于非静态成员,必须为每个实例存储一些东西。

With member variables, it seems quite clear, but what kind of data is stored for methods?

有了成员变量,看起来很清楚,但是为方法存储什么样的数据呢?

I'm working in Java, but I imagine some general rules to apply in other managed environments (such as .NET), too.

我正在使用 Java,但我想一些一般规则也适用于其他托管环境(例如 .NET)。

回答by Michael Burr

The only difference between static methods and non-static (instance) methods behind the scenes is that an extra, hidden parameter (this) is passed to instance methods and that instance methods might be called using an indirect dispatch (if virtual). There is no additional code space taken.

静态方法和幕后的非静态(实例)方法之间的唯一区别是一个额外的隐藏参数 ( this) 被传递给实例方法,并且可以使用间接调度(如果是虚拟的)调用该实例方法。不占用额外的代码空间。

Edit:

编辑:



My answer focused on methods, but on closer reading I see that the question is more about static data. Yes, static data will in a sense save memory since there's only a single copy of it. Of course, whether or not data should be static is more a function of the meaning or use of the data, not memory savings.

我的回答侧重于方法,但仔细阅读后,我发现问题更多地与静态数据有关。是的,静态数据在某种意义上会节省内存,因为它只有一个副本。当然,数据是否应该是静态的更多的是数据的含义或用途的函数,而不是内存节省。

If you need to have a large number of objects and want to conserve memory, you may want to also investigate if using the 'Flyweight' patternis applicable.

如果您需要拥有大量对象并希望节省内存,您可能还需要调查使用“享元”模式是否适用。

回答by Jon Skeet

The decision shouldn't be made on the grounds of efficiency - it should be made on the grounds of correctness.

不应该基于效率做出决定——而应该基于正确性做出决定。

If your variable represents a distinct value for each instance, it should be an instance variable.

如果您的变量代表每个实例的不同值,则它应该是一个实例变量。

If your variable is a common value associated with the type rather than an individual instance of the type, it should be a static variable.

如果您的变量是与类型关联的公共值而不是类型的单个实例,则它应该是静态变量。

You're correct, however - if you have a static variable, you won't "pay" for that with every instance. That just adds an extra reason to make variables static where they don't represent part of the state of the object.

但是,您是对的 - 如果您有一个静态变量,则不会为每个实例“支付”费用。这只是增加了一个额外的理由,即在变量不代表对象状态的一部分的情况下将变量设为静态。

When you mention methods in your question, are you talking about local variables? You'll get a new set of local variables for every method call - including recursive calls. However, this doesn't create a new set of static or instance variables.

当您在问题中提到方法时,您是在谈论局部变量吗?您将为每个方法调用获得一组新的局部变量 - 包括递归调用。但是,这不会创建一组新的静态或实例变量。

回答by Steve314

If you make a member variable static, you save memory per-instance (assuming there's more than one instance), but the real gain is that you don't have to work at keeping all those non-static members consistent with each other, and you don't need a current instance in order to access the static member.

如果您将成员变量设为静态,则可以为每个实例节省内存(假设有多个实例),但真正的好处是您不必努力使所有非静态成员彼此保持一致,并且您不需要当前实例来访问静态成员。

If you make a method static, you save a few bytes on the stack for each nested call (there's no implicit 'this' parameter), but that's only relevant if you're doing veryheavy recursion. Of course if the function needs to know which instance you're dealing with, you'll need an explicit parameter to replace the implicit 'this' anyway, so you gain nothing.

如果将方法设为静态,则每次嵌套调用都会在堆栈上保存几个字节(没有隐式的“this”参数),但这仅在执行非常繁重的递归时才相关。当然,如果函数需要知道您正在处理哪个实例,则无论如何您都需要一个显式参数来替换隐式“this”,因此您一无所获。

There is no per-instance cost to having a static method, or for that matter, a non-static method. The costs occur on calls.

拥有静态方法或就此而言,非静态方法没有每个实例的成本。费用发生在通话中。

The real reason to use a static method is because there is no instance - at least when you call the method. For example, you might use a static method to create and initialise instances (one of the "factory" design patterns), or to reference a singleton instance.

使用静态方法的真正原因是因为没有实例——至少在调用方法时是这样。例如,您可以使用静态方法来创建和初始化实例(“工厂”设计模式之一),或引用单例实例。

回答by Jonas B

The simple answer is yes. Creating an instance everytime there are none equals recreating the entire object, static methods and variables generally consumes less memory depending on how they are used. Of course if you only need to create one instance throughout your entire program, there's no difference. And remember you can always pass instances along as references where you can't have static objects and you need to reuse them.

简单的答案是肯定的。每次没有时创建一个实例等于重新创建整个对象,静态方法和变量通常消耗较少的内存,具体取决于它们的使用方式。当然,如果您只需要在整个程序中创建一个实例,那就没有区别了。请记住,您始终可以将实例作为引用传递,而您不能拥有静态对象并且需要重用它们。