本文介绍了需要静态变量及其在jvm上的开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据静态成员的概念,当第一次调用它的类时,它们被创建/加载到内存中。它们在该类的所有实例中都很常见。意味着它们不会被重新创建或重新初始化等。此外,它们只能通过类名访问。没有必要为该类创建对象只是为了访问它们。

As per the concept about static members, they are created/loaded into the memory when there is first call made to its class. And they are common among all instances of that class. Means they are not re-created or re-itialized etc. In addition, They can be accessed by the class name only. There is no need to create object for that class just to access them.

现在我的问题是;


  1. 在应用程序运行
    之前,静态成员是否曾在
    内存中?即使该类的所有实例
    都是由
    GC(垃圾收集器)收集的。

  2. 对于一个大型项目,8-10个团队
    正在合作,他们不关心其他团队编码的b $ b。
    他们可以根据需要创建静态成员
    。如果所有成员
    都缓存在内存中,是不是
    会在JVM上创建开销?

  3. 默认情况下,接口的所有成员都是STATIC并且使用在许多情况下,接口都很好。但如果我记住上述问题,我还应该使用接口吗?


推荐答案

1静态成员只有在定义它们的类本身被收集时才被垃圾收集;这反过来只有在收集定义的ClassLoader时才会发生。这在Web应用程序容器和插件体系结构中很常见。

1) Static members are garbage collected only when the class that defines them is itself collected; this in turn can only happen if the defining ClassLoader is collected. This is common in web application containers and plugin architectures.

2)是的,定义大量静态数据可能不是一个好主意。但它就像很多其他的东西:如果你需要它会很好,如果你滥用它会很糟糕。只需使用常识。

2) Yes, defining a large amount of static data can be a bad idea. But it's like a lot of other things: it's good if you need it, and bad if you abuse it. Just use common sense.

3)同样,定义一千个字符串数组的接口也是一个坏主意,但当然这通常不是人们所做的。只需使用常识。一般来说,没有(与内存相关)的理由来避免静态变量。

3) Again, an interface that defined an array of a thousand Strings would be a bad idea, but of course that's not normally what people do. Just use common sense. There's no (memory-related) reason to avoid static variables in general.

这篇关于需要静态变量及其在jvm上的开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 01:08