View. NET code execution mechanism from memory changes (III)

Source: Internet
Author: User
1 static void Main (string [] args)
2 {
3 person p; // person is the class and a new reference is p.
4 p = new person (zhangsan); // create an instance-zhangsan
5 P. name = Li Si; // create an instance-Li Si
6 p = new person (Wang Wu); // create an instance-Wang Wu
7 person p2; // person is the class and a new reference is p2.
8p2 = new person (Zhao 6); // create an instance-zhao 6
9 p = p2; // value of mutual payment
10p2 = null;
11 p = null;
12}
The above pseudocode represents the following changes in memory:
When running the third line of person p;, apply for a piece of memory in the stack, the name is p, the content is blank, assuming the address is 10336

After the fourth statement is run, apply for a piece of memory in the heap. The memory content is "James", the heap address is 8610, and fill 8610 in the p content in the stack.

Run the fifth code, p. name = Li Si; it indicates that the content in heap address 8610 is changed from Zhang San to Li Si.

Run the sixth code, p = new person (Wang Wu); apply for a new memory in the heap, the content is Wang Wu, the address is 13910, and replace the p content in the stack.
 
Run the seventh sentence code, "person p2". Apply for a new memory in the stack. The name is p2, the content is empty, and the address is 8861937.

Run the eighth sentence p2 = new person (Zhao 6); apply for a new memory in the heap, the content is Zhao 6, the address is 1119, and fill in 8861937 of the content in the stack to 1119.

In the ninth sentence, p = p2 refreshes 8861937 of the stack content to 10336 of the stack. In the heap, 13910 is reclaimed by clr due to the loss of the reference of p in the stack.

In the Tenth sentence, p2 = null, 8861937 of the content in the stack is cleared, but 1119 of the content in the stack is still referenced by stack 10336.

Eleven sentences p = null; 10336 of the stack content is cleared, and 1119 of the stack content is removed from all references and recycled.


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.