Java中的静态和动态内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19361888/
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
Static and Dynamic memory in Java
提问by user162114
int x;
int y=10;
What type of memory is allocated in Java? I heard that everything in Java is allocated dynamic memory. That is true for objects, but does the same rule follow for primitive data types also (like int
, float
, etc.)?
Java 中分配了什么类型的内存?我听说 Java 中的所有内容都分配了动态内存。这是真正的对象,但不相同的规则对后续主数据类型也(如int
,float
等)?
回答by Narendra Pathai
In one line it depends on where the variable is declared.
在一行中,它取决于变量的声明位置。
Local variables (variables declared in method) are stored on the stackwhile instance and static variables are stored on the heap.*
局部变量(方法中声明的变量)存储在堆栈中,而实例和静态变量存储在堆中。*
NOTE:Type of the variable does not matter.
注意:变量的类型无关紧要。
class A{
private int a = 10; ---> Will be allocated in heap
public void method(){
int b = 4; ----> Will be allocated in stack
}
}
回答by Tom
primitive variables and function calls are stored in the stack. objects are stored in the heap.
原始变量和函数调用存储在堆栈中。对象存储在堆中。
回答by fjammes
Main difference between heap and stack is that stack memory is used to store local variables and function call, while heap memory is used to store objects in Java. No matter, where object is created in code e.g. as member variable, local variable or class variable, they are always created inside heap space in Java.
堆和堆栈的主要区别在于堆栈内存用于存储局部变量和函数调用,而堆内存用于存储 Java 中的对象。无论对象在代码中的何处创建,例如作为成员变量、局部变量或类变量,它们总是在 Java 中的堆空间内创建。
Read more: http://javarevisited.blogspot.com/2013/01/difference-between-stack-and-heap-java.html#ixzz2hhlHV13c
阅读更多:http: //javarevisited.blogspot.com/2013/01/difference-between-stack-and-heap-java.html#ixzz2hhlHV13c
回答by Troubleshoot
The stack is where memory is allocated to primitives, and where local variables are stored; also references are passed around on the stack.
堆栈是为原语分配内存的地方,也是存储局部变量的地方;引用也在堆栈上传递。
The heap is where memory is allocated for objects, which in turn is referred to as heap memory. Static variables are stored on the heap along with instance variables.
堆是为对象分配内存的地方,这又称为堆内存。静态变量与实例变量一起存储在堆上。
回答by Victor R. Oliveira
This is the best article I ever read about java memory https://blog.codecentric.de/en/2010/01/the-java-memory-architecture-1-act/- you should that a look how its managed. Also there is another post regarding to this topic Memory allocation for primitive and reference variables
这是我读过的关于 Java 内存的最好的文章https://blog.codecentric.de/en/2010/01/the-java-memory-architecture-1-act/- 你应该看看它是如何管理的。还有关于这个主题的另一篇文章原始变量和引用变量的内存分配
And, as answer to your question: Local variables are stored on the stack while instance and static variables are stored on the heap.
并且,作为您问题的答案:局部变量存储在堆栈中,而实例和静态变量存储在堆中。
回答by msam
- The JVM stack stores local variables.
- All class instances and arrays are allocated on the JVM heap.
- The Method area stores per class structure
- The runtime constants pool stores constants
- JVM 堆栈存储局部变量。
- 所有类实例和数组都在 JVM 堆上分配。
- 方法区域存储每个类结构
- 运行时常量池存储常量