The processing of this part of memory is extremely important in the program, which is not only related to the speed of the program, but also to the ability of the program to run properly, as well as to the hardware devices that support the program's operation. Small series is also the recent contact with their written out, if there are errors, I implore everyone to treatise.
In the process of running the program, memory is divided into many parts, this article mainly on the stack problem.
What is a stack?
The run-time object area, which is all variables defined in the method, is the storage area of the variable, or stack memory. As the method ends, the stack memory is destroyed naturally.
What is a heap?
The run-time data area is the heap memory where the object is stored when all objects are created in the method. Objects in the heap memory can be reused and not destroyed by the end of the method.
The main explanation in this diagram is the processing of memory analysis when defining variables and assigning values to variables.
value Delivery and address delivery
Let's talk about value passing (value type) and address passing (reference type) combined with memory analysis.
Value Passing
-
Public classValue { Public Static voidMain (string[] args) {intA = 6; intB = 9; Swap (A, b); System.out.println (The value of a after Exchange is: "+ A +"; B's value is: "+b); } Public Static voidSwapintAintb) {inttemp =A; A=b; b=temp; System.out.println (The value of a in the swap method is: "+ A +"; The value of B is: "+b); } }
Run results
Memory analysis
Address Delivery
-
1 Public classaddress{2 3 Public Static voidswap (Datawrap DW) {4 inttemp =DW.A; 5DW.A =dw.b; 6DW.B =temp; 7System.out.println (the value of a in the swap method is: "+ DW.A +"; The value of B is: "+dw.b); 8 } 9 Public Static voidMain (string[] args) {TenDatawrap DW =NewDatawrap (); OneDW.A = 6; ADW.B = 9; - swap (DW); -System.out.println (the value of a after Exchange is: "+ DW.A +"; B is: "+dw.b); the } - } - classdatawrap{ - intA; + intb; -}
Run results
Memory analysis
The top two methods differ in that one pass is a value type, while another is a reference type, which accompanies the end of the method, and the stack that holds the data is destroyed, and the pointer to the heap area disappears. The former data is stored in the current method of the stack, still exist.
"Summary"
Mainly when the stack and heap memory will be generated, in-depth memory to slowly learn AH. About memory Recycling, Java garbage collection mechanism, when the object is no longer referenced, it is recommended to set the object to null, easy to identify the garbage collection mechanism, speed up the recovery.
"Java Basics"--talking about stack memory