gpt4 book ai didi

垃圾回收 : static variables not lasting through the applications lifetime

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:25 24 4
gpt4 key购买 nike

我一直被告知静态变量会持续应用程序的整个生命周期,但我刚刚读到这篇关于 javas GC 的文章让我对这个说法产生了疑问:

Classes may get collected (unloaded) if the JVM finds they are no longer needed and space may be needed for other classes. The permanent generation is included in a full garbage collection.

所以问题是什么会导致 JVM 执行此操作,这是否意味着所有静态类变量在某种意义上都被“重置”了?

最佳答案

So the question is what would cause the JVM to do this ...

当一个不再可达时,就会发生这种情况。实际上,只有当应用程序(或框架)动态创建类加载器、动态加载类,然后类加载器、类和这些类的实例都变得无法被应用程序的其余部分访问时,才会发生这种情况。

它不会发生在由应用程序的主要类加载器加载的类上,因为这样的类总是可以访问的。 (例如,通过使用主类加载器调用 ClassLoader.forName(...)。)

... would it mean that all static class variables are in a sense "reset"?

没有。更好的表征是,如果 static 变量属于已卸载的类,则它们已不复存在。 (就像局部变量在超出范围时不复存在一样。)

请注意,这只会在类被卸载时发生。如果应用程序仍然有可能观察有问题的静态变量,那么变量可达就足够了……这将抑制类卸载.

关于垃圾回收 : static variables not lasting through the applications lifetime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48804828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com