Rookie learn Java (23)--java Memory analysis

Source: Internet
Author: User

we often say that Java memory is mainly divided into four chunks (the register is not considered, we can not manipulate it with code): Stack (stack), heap, data segment, code segment (area). Their main uses are as follows:




And in the top four, we often talk about the two guys on the right,--stack and heap. Today, let's talk about what the Java code looks like in stack and heap while it's running.


Let's look at the following section of code:

     Public Static voidMain (string[] args) {testreference testreference=Newtestreference (); intAge = 1; Person Xiaoqiang=NewPerson ("Xiao Qiang", 21); Person Xiaoming=NewPerson ("xiaoming", 22);        Testreference.selfplus (age); System.out.println ("Age passes the Selfplus method after processing:" +Age );        Testreference.changename (Xiaoqiang); System.out.println ("Jack Bauer after the ChangeName method of processing the name is:" +xiaoqiang.getname ());        Testreference.changeage (xiaoming); System.out.println ("Xiaoming after the treatment of the Changeage method of age:" +xiaoming.getage ()); }         Public voidSelfplus (inti) {i= i + 1; }         Public voidchangename (person person) { person=NewPerson ("Xiao Gang"); }         Public voidchangeage (person person) {Person.setage (25); }



What will be printed after executing the above code? If you have a Java base, then it's easy to know what to export, to know what the above code will print, and you need to understand how Java code works in stack and heap. Let's take a look at the following graphs:

        int i = 1;         New Person ("Xiao Qiang", +)        ; New Person ("xiaoming", 22);


When we're done with the three lines of code above, the memory situation looks like this:



We know that stack is used to store variables, so the age, Xiaoqiang, and xiaoming three variables are stored in the stack, and the age is the int type, so its value is also stored in the stack. Xiaoqiang and xiaoming are reference variables for the person type, so the stack will only hold one of their references, that is, the memory address of the corresponding object, and their real content is stored in the heap.


When executed to Testreference.selfplus (i), the selfplus (int i) method is called, at which time a memory space is opened for parameter I in the stack and its value is set to 1, in which case the memory is as follows:


Because the selfplus (int i) method is called, the age is passed as a parameter to the method, which is equivalent to copying the value of age to I, so now I has a value of 1, then i = i + 1, and the value of I is modified to 2.


At this point, only the copy of age is modified, not the age itself, so age is still 1, when the selfplus (int i) method is executed, I is destroyed, and age is unchanged. Next look at Xiaoqiang, when the changename (person person) method is called, a space is allocated to the person in the stack, which holds the address that the Xiaoqiang points to.


then executes to person = new Person ("Xiao Gang"), and because another person object is new, a person is created in the heap, named small, and the person executes the address of the small just

At this point, the object pointed to by the person from Xiao Qiang to small just, but Xiaoqiang pointed to the object is still a cockroach, and nothing has changed. When the changename method finishes executing, the person is destroyed, and the small gang is then reclaimed by the garbage collector because there are no objects to reference it.


The following is the last method, when the Testreference.changeage (xiaoming) is executed, the Changeage (person) method is called, and the person is also assigned a space in the stack. This time, the memory address of the xiaoming is stored.



Then execute Person.setage (25), when the person pointed to the object is xiaoming, so in the execution of the Setage method, the change is the heap of Xiao Ming's attribute value, when Xiaoming's age was modified to 25. After the method is executed, the person is destroyed and the final result in memory is as follows:


The final situation in memory is as shown, of course, when the main method finishes executing, all previously created objects are destroyed. OK, do you go to work every day to do the project you have to forget some of the basic things? If it is, then hurry up and fill it, the benefits are many.



Rookie learn Java (23)--java Memory analysis

Related Article

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.