The memory is divided into four parts: Stack segment, heap segment, code segment, and data segment;
The new keywords used in our program are stored in heap segment;
Local variables in the program are stored in the stack segment. These local variables are automatically released after the execution of the specific method (the resources in the heap segment need to be processed by the Java Garbage Collector );
The method in the program is in the code segment in the memory, and multiple objects share a code space area;
Static static variables must be placed in the data segment in the memory,
The following is an example of program memory analysis.
Class demo {<br/> private int firistnum; private int secondnum; <br/> Public static int temp = 3; </P> <p> Public demo (INT firstnum, int secondnum) {<br/> This. firstnum = firstnum; <br/> This. secondnum = secondnum; <br/>}</P> <p> public class test {<br/> Public static void main (string [] ARGs) {<br/> demo test = new demo (3, 4); <br/>}< br/>
The following describes the process of memory space allocation.
Step 2 Execution Process
This is the memory space allocation during the execution of the main method. It is very important to analyze the memory for programs with incorrect logic in the future.