Unveil the CLR mystery-Step 2 garbage collection)

Source: Internet
Author: User
Tags mscorlib

Before reading this chapter, you must have a good understanding of the value type and reference type.

For more information about the value type and reference type, see [. Net] In anytao. Article 8: taste type --- value type and reference type (on)-memory rational

---- When the. NET application is initialized, all reference types will be allocated to the managed heap. Only resources in the managed heap can be managed by the garbage collection mechanism. Therefore, we must understand that the CLR garbage collection mechanism does not target all data. We must manually release the instance when manually releasing the instance.

Where can we find this conclusion? How can this be demonstrated?

How does a program run in CLR? So how does the garbage collection mechanism in CLR run? Do we need further research? If we do not manually release value types, will they stay in our valuable memory space? I have to admit that this involves complicated algorithms, operating systems, and Win32 programming knowledge. This may require a large amount of text to illustrate these issues.

Since there is garbage collection, there must be garbage makers making garbage. How these spam are made is what we are trying to figure out today. It is hoped that the following text will clearly describe the memory usage status at each stage of the program running.

See the following code:

Class Program
{
Static void main (string [] ARGs)
{
// Value Type
Char I = new char ();
Char J = 'a ';
// Reference type
Stringbuilder sb = new stringbuilder ();
// Custom type
Myclass MC = new myclass ();
// System Enum type
Stringcomparison SC;
// Custom Enum type
Myenum me = myenum. test;
// Struct
Mystruct ms1;
Ms1.t = "hi ";
// Copy the object here
Mystruct MS2 = ms1;
Ms1.t = "hi2 ";
Mystruct ms3 = new mystruct ();
Char K = 'B ';
If (ms2.equals (ms1 ))
{
Console. writeline ("same instance" + ms2.t );

}
Console. writeline (ms1.t );
Console. writeline (ms2.t );
Console. writeline (ms3.t );
}
}
Class myclass
{

}
Enum myenum
{
Test,
}
Struct mystruct
{
Public String T;
}

Obviously, the above defines a class, an enumeration, and a structure. This is a seemingly common piece of code, but in fact it is quite simple. Let's start ildasm again to see its true nature.

As you can see, both classes have. ctor, that is, both classes have constructors. Click the meeting to find the constructor inherited from the object. This explains why the system automatically declares the constructor even if the declared constructor is not displayed during class definition. The credit comes from CLR.

Careful friends will find another problem, so here this structure clearly does not see the constructor, then why can it be new? For more information, see anytao's article about new.

Let me talk about my views here. Open the main function. The following content is displayed:



. Method private hidebysig static void main (string [] ARGs) cel managed
{
. Entrypoint
. Maxstack 2
. Locals Init ([0] char I,
[1] char J,
[2] class [mscorlib] system. Text. stringbuilder Sb,
[3] class CLR. myclass MC,
[4] valuetype [mscorlib] system. stringcomparison SC,
[5] valuetype CLR. myenum me,
[6] valuetype CLR. mystruct ms1,
[7] valuetype CLR. mystruct MS2,
[8] valuetype CLR. mystruct ms3,
[9] char K,
[10] bool CS $4 $0000)
Il_0000: NOP
Il_0001: LDC. i4.0
Il_0002: stloc.0
Il_0003: LDC. i4.s 97
Il_0005: stloc.1
Il_0006: newobj instance void [mscorlib] system. Text. stringbuilder:. ctor ()
Il_000b: stloc.2
Il_000c: newobj instance void CLR. myclass:. ctor ()
Il_0011: stloc.3
Il_0012: LDC. i4.0
Il_0013: stloc. s me
Il_0015: ldloca. s ms1
Il_0017: ldstr "hi"
Il_001c: stfld string CLR. mystruct: T
Il_0021: ldloc. s ms1
Il_0023: stloc. s MS2
Il_0025: ldloca. s ms1
Il_0027: ldstr "hi2"
Il_002c: stfld string CLR. mystruct: T
Il_0031: ldloca. s ms3
Il_0033: initobj CLR. mystruct
Il_0039: LDC. i4.s 98
Il_003b: stloc. S K
Il_003d: ldloca. s MS2
Il_003f: ldloc. s ms1
Il_0041: Box CLR. mystruct
Il_0046: Constrained. CLR. mystruct
Il_004c: callvirt instance bool [mscorlib] system. Object: equals (object)
Il_0051: LDC. i4.0
Il_0052: CEQ
Il_0054: stloc. s CS $4 $0000
Il_0056: ldloc. s CS $4 $0000
Il_0058: brtrue. s il_0073
Il_005a: NOP
Il_005b: ldstr bytearray (9e 5B 8B 4f F8 76 0C 54) //. [. O. V. T
Il_0060: ldloca. s MS2
Il_0062: ld1_string CLR. mystruct: T
Il_0067: Call string [mscorlib] system. String: Concat (string,
String)
Il_006c: Call void [mscorlib] system. Console: writeline (string)
Il_0071: NOP
Il_0072: NOP
Il_0073: ldloca. s ms1
Il_0075: ld1_string CLR. mystruct: T
Il_007a: Call void [mscorlib] system. Console: writeline (string)
Il_007f: NOP
Il_0080: ldloca. s MS2
Il_0082: ld1_string CLR. mystruct: T
Il_0087: Call void [mscorlib] system. Console: writeline (string)
Il_008c: NOP
Il_008d: ldloca. s ms3
Il_008f: ld1_string CLR. mystruct: T
Il_0094: Call void [mscorlib] system. Console: writeline (string)
Il_0099: NOP
Il_009a: Ret
} // End of method program: Main

We can see that during init, 11 parameters are passed in. These are the 11 objects used in our code. The init command is used to "init specifies that the variables must be initialized to the default values for their respective types". The variable specified by init must be initialized by the default values of the respective types-original address

Let's continue with the above question. Why can the structure be new without Constructors? After analyzing the Il code, we found that when the new class is the new reference type, the Il command is newobj, and the corresponding constructor is also attached to the instance, the command of Il is initobj. This seems to be the root cause of the problem.
Let's talk about the newobj command. When executing this command, it will instantiate the specified type and initialize the members of the new type once as needed, then, the object is sent to the hosting stack. Then, call the constructor according to the parameter. The constructor performs operations on this type in the managed heap. In the end, only the reference of this object is pushed to the stack on the thread stack. (There is actually a pointer on our hosting stack. This pointer is the highest prosecutor for garbage collection. We will describe him later .)

So what does our initobj do? The explanation on msdn is very simple. This command is the initialization value type. After compilation, the value type of non-primitive type already exists on the thread stack. However, initialization has not been performed. This step is required. All primitive types are initially set to 0 during compilation. That is why char and mystruct are of the same value type and char does not have init.

As described above, we can clearly see the running process in. Net CLR. Object creation is a recursive process. It is also a manifestation of the builer model that we usually call. So far, all our "garbage" has been manufactured. Next we can discuss how to recycle the generated garbage. The most important part of this step is initialization.
Download the msil Quick Reference Manual. Thank you for your selfless dedication.

Copyright Disclaimer: original technical article. If you need to repost it, please declare the source. It shall not be used for any commercial activities; otherwise, it shall be held legally liable.

PS: I am very grateful to sumtec @ Beijing for the comments raised by the two netizens.

 

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.