• 周六. 10 月 5th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

It turns out that Java only has value passing — Reference passing stack and heap of Java objects

King Wang

1 月 3, 2022

    Recently, I met some problems in development bug, Yes Java The object reference mechanism of

One . What is heap memory , What is stack memory

Stack memory

      Defined in function Some basic types of variables and object reference variables are allocated in the function’s stack memory . When you define a variable in a block of code ,java This variable is allocated memory space in the stack , When the scope of a variable is exceeded ,java The memory space allocated for the variable will be released automatically , This memory space can be immediately used for other purposes .

Heap memory

     Heap memory is used to store the new Created objects and arrays . Memory allocated in the heap , from java Virtual machine automatic garbage collector to manage . After generating an array or object in the heap , You can also define a special variable in the stack , The value of this variable is equal to the first address of the array or object in heap memory , This particular variable in the stack becomes a reference variable for an array or object , Later, you can use reference variables in stack memory to access arrays or objects in the heap , A reference variable is equivalent to an alias for an array or object , Or code name .

    Reference variables are ordinary variables , Allocate memory in the stack when defining , Reference variables are released outside the scope of the program . And arrays & The object itself is allocated in the heap , Even if the program runs to use new Outside the code block where the statements that generate arrays and objects are located , The heap memory occupied by arrays and objects themselves will not be released , Arrays and objects when there is no reference variable pointing to it , Before it becomes garbage , It can no longer be used , But it still takes up memory , It is released by the garbage collector at an uncertain time . This is also java Compare the main reason for memory , actually , Variables in the stack point to variables in heap memory , This is it. Java The pointer in !

   JVM Is a stack based virtual machine .JVM Assign a stack to each newly created thread . in other words , For one Java Procedure , It’s run by operating on the stack . Stack saves the state of the thread in frames .JVM There are only two operations on the stack : Stack pressing and stack out operation based on frame .

We know , The method that a thread is executing is called the current method of this thread . We may not know , The frame used by the current method is called the current frame . When a thread activates a Java Method ,JVM It will be in the thread Java A new frame is pushed into the stack . This frame naturally becomes the current frame . During execution of this method , This frame will be used to save the parameters , local variable , Intermediate calculation process and other data . This frame here is similar to the concept of activity record in compilation principle .

from Java From the perspective of this distribution mechanism , The stack can be understood in this way : Stack (Stack) When the operating system establishes a process or thread ( In a multithreaded operating system, thread ) Storage area established for this thread , This area has the characteristics of “in and out” .

every last Java Applications all correspond to one JVM example , Each instance uniquely corresponds to a heap . All instances or arrays of classes created by the application at run time are placed in this heap , And shared by all threads of the application . Follow C/C++ Different ,Java Allocation of heap memory in is automatically initialized .Java The storage space for all objects in is allocated in the heap , But the reference of this object is allocated in the stack , That is, when an object is created, memory is allocated from both places , The memory allocated in the heap actually creates this object , The memory allocated in the stack is just a pointer to the heap object ( quote ) nothing more .

Related reference examples of stacks

      Stack has a very important particularity , Data stored in the stack can be shared . Suppose we define it at the same time :

int a = 3;

int b = 3;

The compiler processes int a = 3; First, it creates a variable in the stack as a References to , Then find out if there is any in the stack 3 This value , If not , will 3 Store in , And then a Point to 3. And then deal with int b = 3; After creating b After referencing variables for , Because there are already 3 This value , It will be b Direct to 3. such , And that’s what happened a And b At the same time, they all point to 3 The situation of . At this time , If you order again a=4; Then the compiler will re search the stack for any 4 value , without , Will 4 Store in , And order a Point to 4; If there are already , Will directly a Point to this address . therefore a Changes in values do not affect b Value . Note that this sharing of data is different from the sharing of two references to one object at the same time , Because of this situation a The modification of will not affect b, It’s done by the compiler , It helps save space . An object reference variable modifies the internal state of the object , Affects another object reference variable

 

Stack and heap

    All are Java Used in Ram Where data is stored in .

  And C++ Different ,Java Automatic management of stacks and heaps , Programmers cannot set stacks or heaps directly .

Java The heap of is a runtime data area , Class ( Object to allocate space from . These objects pass new、newarray、anewarray and multianewarray Etc , They don’t need program code to explicitly release . The heap is in the charge of garbage collection , The advantage of heap is that memory size can be allocated dynamically , You don’t have to tell the compiler the lifetime , Because it dynamically allocates memory at run time ,Java The garbage collector of will automatically collect the data that is no longer used . But the disadvantage is , Due to dynamic memory allocation at run time , Access speed is slow .

The advantage of the stack is that , Access speed is faster than heap , Next to registers , Stack data can be shared . But the disadvantage is , The size and lifetime of the data in the stack must be determined , inflexibility . The stack mainly stores some basic types of variables (,int, short, long, byte, float, double, boolean, char) And object handles .

 

 

发表回复