简体   繁体   English

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

[英]Where java static variables are stored in memory?

class A{
 static int i = 10;
 static int j = 20;

 static void getname(){

   }

}

Where will these variable be stored in memory ? 这些变量将存储在内存中的哪个位置?

simply said , Static Variables are stored in HEAP . 简单地说, 静态变量存储在HEAP中 Classes and all of the data applying to classes (not instance data) is stored in the Permanent Generation section of the heap. 应用于类(而不是实例数据)的类和所有数据都存储在堆的“永久生成”部分中。

If you need elaborated answer , refer this 如果您需要详细解答,请参阅此处

static allocation in java - heap, stack and permanent generation java中的静态分配 - 堆,堆栈和永久生成

First, static member variables are stored in the Permanent Generation area of heap. 首先,静态成员变量存储在堆的Permanent Generation区域中。

Your example contains primitive type variables, they will be stored in the PermGen. 您的示例包含基本类型变量,它们将存储在PermGen中。

If those were object type variables, eg static Object x = new Object(); 如果那些是对象类型变量, 例如 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. ,然后引用x将存储在PermGen中,而Object本身将被放置在堆的Young Generation中。

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. 我认为对于某些JVMS的大多数实现它特别适用于PERM-GEM ...但我没有证据......事情的真相是......它取决于存储这些值的JVM。 It is a variable... it could be stored in many different fashions depending on the JVM implementation. 它是一个变量......它可以根据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. 让我们重新解释你的问题,我将重新发布一个更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM