java 静态类变量存储在内存中的什么位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6655338/
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-30 16:49:16  来源:igfitidea点击:

Where are static class variables stored in memory?

javac++memorystatic-membersclass-variables

提问by tskuzzy

This is a follow-up question to How are static arrays stored in Java memory?.

这是静态数组如何存储在 Java 内存中的后续问题.

So global variables in C/C++ are stored in the static data segment of memory. But what about static class variables in Java/C++?

所以C/C++中的全局变量都存放在内存的静态数据段中。但是 Java/C++ 中的静态类变量呢?

It can't be the static data segment because you don't know what/how many classes are going to be referenced throughout the duration of your program (because of reflection). It's definitely not the stack because that makes no sense. Storing it on the heap is also kind of iffy.

它不能是静态数据段,因为您不知道在整个程序持续时间内将引用什么/多少类(因为反射)。这绝对不是堆栈,因为那毫无意义。将它存储在堆上也有点不确定。

采纳答案by bdonlan

In Java, at a low level, class static variables are indeed stored on the heap, along with all other class metadata. To Java, they look like globals, but to the JVM's low level heap management routines, they're dynamic data (although they may be treated slightly specially in order to improve GC efficiency, since they're likely to be long lived). After all, classes can be unloaded by unreferencing their classloader.

在 Java 中,在低级别,类静态变量确实与所有其他类元数据一起存储在堆中。对于 Java 来说,它们看起来像全局变量,但对于 JVM 的低级堆管理例程来说,它们是动态数据(尽管为了提高 GC 效率,它们可能会被稍微特殊对待,因为它们很可能是长期存在的)。毕竟,可以通过取消引用类加载器来卸载类。

As for whether it's the same as the C malloc(), not likely. Most JVMs take control of their heaps at a low level; they grab a chunk of memory from the OS and divvy it up themselves. As such, most Java data, including static data, are not stored in the malloc heap, but rather in a separate heap managed by the JVM.

至于是否和 C 一样malloc(),不太可能。大多数 JVM 在低级别控制它们的堆;他们从操作系统中获取一块内存并自行分配。因此,大多数 Java 数据(包括静态数据)并不存储在 malloc 堆中,而是存储在由 JVM 管理的单独堆中。

回答by antlersoft

Java has a "permanent" heap where it puts class metadata. So the "roots" of the static values are in the permanent heap. The values are reference values (class objects) the values themselves are in the regular heap.

Java 有一个“永久”堆,用于放置类元数据。所以静态值的“根”在永久堆中。这些值是引用值(类对象),这些值本身在常规堆中。

回答by satish reddy

Static variables will not be stored in Heap.. They are part of Data Segment. Local variables will be stored in - Stack; Instance variables will be stored in - Heap; Class variables(Static) will be stored in - Data Segment. These variables will be shared across all objects of that class.. Your final machine equivalent java code will be stored in - Code/text segment.

静态变量不会存储在堆中。它们是数据段的一部分。局部变量将存储在 - 堆栈中;实例变量将存储在 - 堆中;类变量(静态)将存储在 - 数据段中。这些变量将在该类的所有对象之间共享。您最终的机器等效 java 代码将存储在 - 代码/文本段中。