Home > Back-end >  Java instance variables are stored in a heap area or stack area?
Java instance variables are stored in a heap area or stack area?

Time:12-31

I know local variables are stored in the stack area, but the instance variables (including my master data types) to store where, stack area? Or heap area? , that reference variables? In asking god taught!!!!!

CodePudding user response:

Object on the heap, stack save values of basic types, object reference (which can be understood as object on the heap of address).

CodePudding user response:

All the instance variables and reference variables, and so on are in the stack memory (both instance and are the object of the deposit of the corresponding address, rather than the object itself).
The object itself as well as the object of data members in the heap memory.

CodePudding user response:

Objects in the heap, a local variable object in the heap, a local variable is only reference

CodePudding user response:

Object itself is in a heap, quoted in the stack, stack operation is actually a method invocation, corresponds to the stack frame into and out of the stack

CodePudding user response:

Reference type variables can also be a local variables, local variables stored in the stack area, but it the referenced object stored in the heap or constant pool,

Member variables of an object, that is, you're an instance variable, with the object, the object in the heap, so it is also in the heap,

CodePudding user response:

reference 5 floor m2200 reply:
reference type variables can also be a local variables, local variables stored in the stack area, but its the referenced object stored in the heap or constant pool,

Member variables of an object, that is, you're an instance variable, with the object, the object in the heap, so it is also in the heap,
I think what you said is right, there are so many misleading the problem now

CodePudding user response:

Recommended reading "deep understanding of the Java virtual machine" zhi-ming zhou

CodePudding user response:

Instance variable heap area

CodePudding user response:

Does not recommend the couple in many common grammar semantics are not familiar with the topic of in-depth,
Before discussing this problem, need to want to know another question: what is the distinguishing feature of the stack and heap structure in both of?
On the basis of want to understand this, why Java put some data in the stack, and some problems in the data on the heap are easier to understand,

CodePudding user response:

Is the advantage of stack, access speed faster than the heap, second only to directly in the CPU registers,
But the disadvantage is that there is data in a stack size and survival period must be determined, the lack of flexibility,
In addition, the stack data in multiple threads or is not Shared between multiple stacks, but inside the stack multiple values equal variables can be pointing to an address,
Is the advantage of pile can be dynamically allocated memory size, lifetime also don't have to tell the compiler beforehand, the Java garbage collector will take these no longer USES the data automatically,
But the disadvantage is that due to dynamically allocated memory at run time, access to slow,

CodePudding user response:

The program execution process



1. When the program starts running, to Load into memory area
2. The operating system also has some code in memory, so the operating system code will find the main method of this section of the program code and begin to execute,
3. Perform in the process of memory management is divided into four parts:
Code segment: code area, used to store the code, application runtime will code the load to the area,
Datasegment: to hold static variables, string constants,
Deposit stack: the stack for local variables, 1. The basic data type is used to store the values; 2. Save the instance of the class, namely heap object reference (pointer), can also be used to save the frame when loading method,
Heap, the heap, which is used to dynamically generate memory, to store the new things, pay attention to create the object contains only belong to their respective member variable, does not include members of the method, because the same class of objects with their own member variables, are stored in their respective heap, but they share the class methods, not every create an object is copy a member method,

CodePudding user response:

Instance variables are on the heap, refer to "Head First Java" constructor and the garbage collector section, speak about stack inside pretty understand
Screenshot below
  • Related