java静态变量存储在内存中的什么地方?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22730690/
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
Where java static variables are stored in memory?
提问by Deepak
class A{
static int i = 10;
static int j = 20;
static void getname(){
}
}
Where will these variable be stored in memory ?
这些变量将存储在内存中的何处?
回答by AnthonyJClink
I think for most implementations of some JVMS its particular to the PERM-GEM... but I have no proof.. the truth of the matter is... its up to the JVM where these values are stored. It is a variable... it could be stored in many different fashions depending on the JVM implementation.
我认为对于某些 JVM 的大多数实现来说,它是 PERM-GEM 特有的……但我没有证据……事情的真相是……这取决于存储这些值的 JVM。它是一个变量……它可以根据 JVM 实现以多种不同的方式存储。
If you are seeing memory problems, I would probably look at whats being assigned and not how its being assigned.
如果您看到内存问题,我可能会查看分配的内容而不是分配的方式。
If you need more info, or your question is more implementation specific; lets rephrase your question and I will repost a better answer.
如果您需要更多信息,或者您的问题更具体;让我们重新表述你的问题,我会重新发布一个更好的答案。
回答by AnthonyJClink
simply said , Static Variables are stored in HEAP. Classes and all of the data applying to classes (not instance data) is stored in the Permanent Generation section of the heap.
简单的说,静态变量存储在HEAP中。类和所有应用于类的数据(不是实例数据)都存储在堆的永久代部分。
If you need elaborated answer , refer this
如果您需要详细的答案,请参阅此
static allocation in java - heap, stack and permanent generation
回答by marcelv3612
First, static member variables are stored in the Permanent Generation area of heap.
首先,静态成员变量存放在堆的永久代区。
Your example contains primitive type variables, they will be stored in the PermGen.
您的示例包含原始类型变量,它们将存储在 PermGen 中。
If those were object type variables, e.g.static Object x = new Object();
, then the reference x
would be stored in PermGen whereas the Object
itself would be placed in Young Generation of the heap.
如果那些是对象类型变量,例如static Object x = new Object();
,则引用x
将存储在 PermGen 中,而其Object
本身将放置在堆的年轻代中。