simple java program memory analysis

Original link: http://www.cnblogs.com/liuzhiyi7288/archive/2009/05/05/1450160.html

First, the overall memory is divided into four parts, including the stack segment, heap segment, code segment, data segment;

We used the program in which new keywords out of things that are stored in the heap segment;

Program local variables in a stack segment, these local variables is performed after the end of a specific method, the system automatically releases the memory resources (the heap segment resources required to process the java garbage collection);

The method of the program, the memory in the code segment, and a plurality of objects share a code space region;

static static variables need to be placed in the data segment in memory,

The following is an example of a program memory analysis

code:

     class Demo{

            private int firistNum;

            private int secondNum;

            public static int temp  =  3;

            public Demo(int firstNum,int secondNum){

                  this.firstNum = firstNum;      

                  this.secondNum = secondNum;

            }

      }

      public class Test{

           Public static void main(String [] args){

                 Demo test = new Demo(3,4);      
           }

      }

code

Here is the process memory space allocation

 

The second step execution

This is the memory space throughout the execution of the main method of distribution, a good memory is important for later analysis of program logic error, hope the exhibitions analysis.

Reproduced in: https: //www.cnblogs.com/liuzhiyi7288/archive/2009/05/05/1450160.html

Guess you like

Origin blog.csdn.net/weixin_30414305/article/details/94796379