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.