Java (23)-Java memory analysis, java

Source: Internet
Author: User

Java (23)-Java memory analysis, java

We often say that Java memory is mainly divided into four major blocks (register is not considered, and we cannot use code to manipulate it): stack and heap) data segment and code segment ). Their main purposes are shown in:




Among the four above, 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 the stack and heap processes.


Let's take a look at the following code:

Public static void main (String [] args) {TestReference testReference = new TestReference (); int age = 1; Person xiaoqiang = new Person ("xiaoqiang", 21 ); person xiaoming = new Person ("James", 22); testReference. selfPlus (age); System. out. println ("age is processed by the selfPlus method:" + age); testReference. changeName (xiaoqiang); System. out. println ("the name of xiaoqiang after being processed by the changeName method is:" + xiaoqiang. getName (); testReference. changeAge (xiaoming); System. out. println ("the age after Xiao Ming's changeAge method is:" + xiaoming. getAge ();} public void selfPlus (int I) {I = I + 1;} public void changeName (Person person) {person = new Person ("Xiaogang ");} public void changeAge (Person person) {person. setAge (25 );}


After executing the above Code, what will be printed? If you have a good Java foundation, you can easily know what will be output and what the above Code will print, you need to understand how Java code works in stack and heap. Let's take a look at the following images:

Int I = 1; Person xiaoqiang = new Person ("Xiao Qiang", 21); Person xiaoming = new Person ("Xiao Ming", 22 );

When the above three lines of code are executed, the memory conditions are shown in:



We know that stack is used to store variables. Therefore, age, xiaoqiang, and xiaoming variables are stored in the stack, and age is int type, therefore, the value is stored in the stack. Xiaoqiang and xiaoming are reference variables of the Person type. Therefore, stack only stores one of their references, that is, the memory address of the corresponding objects, and their real content is stored in heap.


When it is executed to testReference. when selfPlus (I); is used, the selfPlus (int I) method is called. In this case, a memory space is opened for the form parameter I in the stack and its value is set to 1, the memory is as follows:


When selfPlus (int I) is called, age is passed to this method as a form parameter, which is equivalent to copying the value of age to I. Therefore, the value of I is 1, then execute I = I + 1. At this time, the I value is changed to 2,


At this time, only a copy of age is modified, not the age itself. Therefore, age is still 1. When the selfPlus (int I) method is executed, I is destroyed, and age remains unchanged. Next, let's take a look at xiaoqiang. When the changeName (Person person) method is called, a space will be allocated for person in the stack, which stores the address pointed to by xiaoqiang,


Then execute to person = new Person ("Xiaogang");, because there is another Person object, a new person will be created in the heap, whose name is Xiaogang, and the person will be executed by Xiaogang's address,

At this time, the person points to the object from xiaoqiang to Xiaogang, but xiaoqiang points to the object is still xiaoqiang, and there is no change. After the changeName method is executed, the person is destroyed, and Xiaogang will reference it because there is no object, and then be recycled by the garbage collector.


The following is the last method. When testReference is executed. when changeAge (xiaoming); is called, The changeAge (Person person) method will also allocate a space for person in the stack. This time, it stores the memory address corresponding to xiaoming,



Then execute person. setAge (25); at this time, the person points to the object Xiao Ming, so when you execute the setAge method, the value of the Small and small characters in heap is modified. At this time, Xiao Ming's age is changed to 25 ,. After the method is executed, the person is destroyed. The final result in the memory is as follows:


The final situation in the memory is shown in. Of course, after the main method is executed, all previously created objects will be destroyed. OK. Do you forget some basic things when you go to work on projects every day? If yes, you can just try it out. There are still a lot of advantages.



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.