局部变量的Java内存(Stack)分配

时间:2022-12-24 16:49:26

I am learning java and right now i am stuck at memory allocation of object's and local variables. can any one illustrate or clear some of my doubts??

我正在学习java,现在我陷入了对象和局部变量的内存分配。任何人都可以说明或澄清我的一些疑惑吗?

  1. I read about Heap and Stack Memory for Object's instance Variable's and Local Variable's. I have question that weather a new STACK is being created for each method?? or for each class of a single stack is used by a whole class??
  2. 我读到了对象的实例变量和局部变量的堆和堆栈内存。我有问题,天气为每种方法创建一个新的STACK?或者对于整个类使用单个堆栈的每个类?
  3. I had read that ONE STACK is being created by every thread What is means
  4. 我读过每个线程正在创建ONE STACK是什么意思

Thanks Mahaveer

谢谢Mahaveer

3 个解决方案

#1


13  

  • Each thread has a private stack.
  • 每个线程都有一个私有堆栈。
  • Each method has a private stack frame within that thread's stack.
  • 每个方法在该线程的堆栈中都有一个私有堆栈帧。

Stacks are associated with thread in a one-to-one mapping. Stacks are absolutely not associated with methods and classes.

堆栈以一对一的映射与线程相关联。堆栈绝对不与方法和类相关联。

The way to reason about all this is that the local variables of a method are private to each invocation of that method.

推理所有这些的方法是方法的局部变量对于该方法的每次调用都是私有的。

#2


38  

Every thread has it's own stack.

每个线程都有自己的堆栈。

  • Whenever you use new, an object is created on the heap.
  • 无论何时使用new,都会在堆上创建一个对象。
  • Local variables are stored on the stack. That includes primitives (such as int) and the references to any objects created. The actual objects themselves aren't created on the stack, as I mentioned when you use new they'll be created on the heap.
  • 局部变量存储在堆栈中。这包括基元(例如int)和对创建的任何对象的引用。实际的对象本身并不是在堆栈上创建的,正如我在使用new时提到的那样,它们将在堆上创建。

I have question that weather a new STACK is being created for each method??

我有问题,天气为每种方法创建一个新的STACK?

The same stack is being used when a method is called. A method will create it's own little section on the stack called a "stack frame" that's used to hold it's local variables.

调用方法时使用相同的堆栈。一个方法将在堆栈上创建它自己的小部分,称为“堆栈帧”,用于保存它的局部变量。

It's just like a stack of plates, when a method is called a plate is added to the top of the stack (a stack frame), and when that method ends the plate is removed from the stack. All of that method's local variables will be destroyed with it, but the actual objects created with new won't.

它就像一堆板,当一个方法被称为板时,板被添加到堆栈的顶部(堆栈框架),当该方法结束时,板从堆栈中移除。所有该方法的局部变量都将随之被销毁,但使用new创建的实际对象则不会。

The JVM's garbage collector will look after destroying objects on the heap (the one's created with new) when it sees you no longer need them.

当JVM的垃圾收集器看到你不再需要它们时,它将会破坏堆上的对象(用new创建的对象)。

#3


0  

Ofcourse, java garbage collector always takes care of the Heap, when it gets a chance to be executed, so it only looks for orphan objects and wipes them out, that's why NEW keyword in java always creates new objects on the Heap memory.

当然,java垃圾收集器总是处理堆,当它有机会被执行时,所以它只查找孤立对象并将它们擦掉,这就是为什么java中的NEW关键字总是在堆内存上创建新对象的原因。

#1


13  

  • Each thread has a private stack.
  • 每个线程都有一个私有堆栈。
  • Each method has a private stack frame within that thread's stack.
  • 每个方法在该线程的堆栈中都有一个私有堆栈帧。

Stacks are associated with thread in a one-to-one mapping. Stacks are absolutely not associated with methods and classes.

堆栈以一对一的映射与线程相关联。堆栈绝对不与方法和类相关联。

The way to reason about all this is that the local variables of a method are private to each invocation of that method.

推理所有这些的方法是方法的局部变量对于该方法的每次调用都是私有的。

#2


38  

Every thread has it's own stack.

每个线程都有自己的堆栈。

  • Whenever you use new, an object is created on the heap.
  • 无论何时使用new,都会在堆上创建一个对象。
  • Local variables are stored on the stack. That includes primitives (such as int) and the references to any objects created. The actual objects themselves aren't created on the stack, as I mentioned when you use new they'll be created on the heap.
  • 局部变量存储在堆栈中。这包括基元(例如int)和对创建的任何对象的引用。实际的对象本身并不是在堆栈上创建的,正如我在使用new时提到的那样,它们将在堆上创建。

I have question that weather a new STACK is being created for each method??

我有问题,天气为每种方法创建一个新的STACK?

The same stack is being used when a method is called. A method will create it's own little section on the stack called a "stack frame" that's used to hold it's local variables.

调用方法时使用相同的堆栈。一个方法将在堆栈上创建它自己的小部分,称为“堆栈帧”,用于保存它的局部变量。

It's just like a stack of plates, when a method is called a plate is added to the top of the stack (a stack frame), and when that method ends the plate is removed from the stack. All of that method's local variables will be destroyed with it, but the actual objects created with new won't.

它就像一堆板,当一个方法被称为板时,板被添加到堆栈的顶部(堆栈框架),当该方法结束时,板从堆栈中移除。所有该方法的局部变量都将随之被销毁,但使用new创建的实际对象则不会。

The JVM's garbage collector will look after destroying objects on the heap (the one's created with new) when it sees you no longer need them.

当JVM的垃圾收集器看到你不再需要它们时,它将会破坏堆上的对象(用new创建的对象)。

#3


0  

Ofcourse, java garbage collector always takes care of the Heap, when it gets a chance to be executed, so it only looks for orphan objects and wipes them out, that's why NEW keyword in java always creates new objects on the Heap memory.

当然,java垃圾收集器总是处理堆,当它有机会被执行时,所以它只查找孤立对象并将它们擦掉,这就是为什么java中的NEW关键字总是在堆内存上创建新对象的原因。