A series of memory analysis columns for the Java execution Program

Source: Internet
Author: User

In the recent period of time, we studied the memory allocation in the program execution of Java, and gained a lot, which solved many puzzles in my time learning java. The main purpose of writing the Java Memory Analysis series is two, one is to want to give Java beginners like me a certain inspiration, hope also can solve the Java about memory confusion points. The second is also to make a summary, after all, only their own personal writing out of things, is the real thing. Start today the Java program execution Process memory Analysis series the first article.

First, the Java execution program when the memory partition

Generally speaking, as a beginner, only the Java program's memory is only divided into the stack area, and the heap area. It is true that this division can be easy for us to understand, but with the increase in learning Java time, if the memory division is only a simple stack and heap two areas, there is a lot of things can not be deeply understood, take a commonly used characters, it is difficult to understand the character of the string. So I'm going to add a partition of the memory area so that I can understand more than I used to. What is this new area of memory? In fact, it is not new, that is, the former heap memory is divided into methods and other areas. The method area inside of what things, the method area is mainly stored three things, the first is the code of the program, the second is a constant pool, where some Java programs are mainly stored in some constants and string constants, the third is a static variable. Heap memory partitioning.

Ii.. Memory analysis during the execution of a Java program

First I defined a people class with a name and gender two member variables and a speaking member method. There is also a test class, Test people class, well, everything is ready, only owed the East wind. The following formally begins the memory analysis.

1  Public class people {2     String name; // define a person's name 3     String sex; 4      Public void speak () {5         System.out.println ("Hi Hello"); 6     }7     8 }
 1  public  class   Test { 2   static  void   main (string[] args) { 3  people p = new< /span> people ();  4   System.out.println (p.name);  5   System.out.println (p.sex);  6   P.speak ();  7   8  }

When the program starts running, the command that executes is Java test (which is the first time that Eclipse is executing the command, that is, executing Java test when executing the Run command), when the JVM is the Java virtual opportunity to load the Test class into the method area, Then find the program's test class inside of the main method, as the entire program entry, start to run the entire program. First executes the third line of command people afraid = new people (); When executing this line of code, what happens in memory, when the JVM encounters people p first executes the JVM will go to his management path under the People class, if not found, will error, If it is found, it will load the people class into the method area, before the method area of the main store some code and some constants, then the stack memory to save the P reference variable, and initialize to null, after loading the people class immediately after the execution of New people (), The JVM encounters the new keyword into the heap memory to open up a space to hold the object instantiated by the people class. and assign the first address of this memory to P. Specific.

Here the value referenced by P is the first address of the memory space of the New people () object. It is important to note that the member variable has an initial value, that the initial value of all reference types is null, and that another class creates multiple objects that share a member method.

Third, conclusion

Analysis here, the Memory Analysis series is here, I will continue to go deep into memory analysis, about This,super and static,string, and inherited memory analysis, hope to be helpful to everyone.

A series of memory analysis columns for the Java execution Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.