C# 是否存在使用未引用对象进行垃圾收集的风险
有时我不想写一行新行来声明Regex对象,所以我编写以下代码C# 是否存在使用未引用对象进行垃圾收集的风险,c#,.net,garbage-collection,C#,.net,Garbage Collection,有时我不想写一行新行来声明Regex对象,所以我编写以下代码 MatchCollection matchCollection = new Regex("example").Matches(someText); 以我有限的知识,我认为新的正则表达式(“示例”)可能会在匹配开始之前被垃圾收集,但这几乎是不可能的,因为操作太快了 我错了吗?这种编码是一种我应该避免的糟糕做法吗?否。对象在实际使用之前不可能得到GC'ed 事实上,它(几乎)与: var r = new Regex("example")
MatchCollection matchCollection = new Regex("example").Matches(someText);
以我有限的知识,我认为新的正则表达式(“示例”)可能会在匹配开始之前被垃圾收集,但这几乎是不可能的,因为操作太快了
我错了吗?这种编码是一种我应该避免的糟糕做法吗?否。对象在实际使用之前不可能得到GC'ed 事实上,它(几乎)与:
var r = new Regex("example");
MatchCollection matchCollection = r.Matches(someText);
作为证明:以下是来自控制台应用程序的IL,其中包含上述代码(1)和一个oneliner(2):
独立变量:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 27 (0x1b)
.maxstack 2
.locals init ([0] string someText,
[1] class [System]System.Text.RegularExpressions.Regex r,
[2] class [System]System.Text.RegularExpressions.MatchCollection matchCollection)
IL_0000: nop
IL_0001: ldstr "s"
IL_0006: stloc.0
IL_0007: ldstr "example"
IL_000c: newobj instance void [System]System.Text.RegularExpressions.Regex::.ctor(string)
IL_0011: stloc.1
IL_0012: ldloc.1
IL_0013: ldloc.0
IL_0014: callvirt instance class [System]System.Text.RegularExpressions.MatchCollection [System]System.Text.RegularExpressions.Regex::Matches(string)
IL_0019: stloc.2
IL_001a: ret
} // end of method Program::Main
Oneliner:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 25 (0x19)
.maxstack 2
.locals init ([0] string someText,
[1] class [System]System.Text.RegularExpressions.MatchCollection matchCollection)
IL_0000: nop
IL_0001: ldstr "s"
IL_0006: stloc.0
IL_0007: ldstr "example"
IL_000c: newobj instance void [System]System.Text.RegularExpressions.Regex::.ctor(string)
IL_0011: ldloc.0
IL_0012: call instance class [System]System.Text.RegularExpressions.MatchCollection [System]System.Text.RegularExpressions.Regex::Matches(string)
IL_0017: stloc.1
IL_0018: ret
} // end of method Program::Main
您将看到实际的代码不同。从堆栈中再推和弹出一个变量,实际调用略有不同,但仅此而已。他们仍然调用同一个对象,只是它存在的位置不同。否。不可能在实际使用之前对该对象进行GC'ed 事实上,它(几乎)与:
var r = new Regex("example");
MatchCollection matchCollection = r.Matches(someText);
作为证明:以下是来自控制台应用程序的IL,其中包含上述代码(1)和一个oneliner(2):
独立变量:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 27 (0x1b)
.maxstack 2
.locals init ([0] string someText,
[1] class [System]System.Text.RegularExpressions.Regex r,
[2] class [System]System.Text.RegularExpressions.MatchCollection matchCollection)
IL_0000: nop
IL_0001: ldstr "s"
IL_0006: stloc.0
IL_0007: ldstr "example"
IL_000c: newobj instance void [System]System.Text.RegularExpressions.Regex::.ctor(string)
IL_0011: stloc.1
IL_0012: ldloc.1
IL_0013: ldloc.0
IL_0014: callvirt instance class [System]System.Text.RegularExpressions.MatchCollection [System]System.Text.RegularExpressions.Regex::Matches(string)
IL_0019: stloc.2
IL_001a: ret
} // end of method Program::Main
Oneliner:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 25 (0x19)
.maxstack 2
.locals init ([0] string someText,
[1] class [System]System.Text.RegularExpressions.MatchCollection matchCollection)
IL_0000: nop
IL_0001: ldstr "s"
IL_0006: stloc.0
IL_0007: ldstr "example"
IL_000c: newobj instance void [System]System.Text.RegularExpressions.Regex::.ctor(string)
IL_0011: ldloc.0
IL_0012: call instance class [System]System.Text.RegularExpressions.MatchCollection [System]System.Text.RegularExpressions.Regex::Matches(string)
IL_0017: stloc.1
IL_0018: ret
} // end of method Program::Main
您将看到实际的代码不同。从堆栈中再推和弹出一个变量,实际调用略有不同,但仅此而已。他们仍然调用同一个对象,只是它存在的位置不同。否。不可能在实际使用之前对该对象进行GC'ed 事实上,它(几乎)与:
var r = new Regex("example");
MatchCollection matchCollection = r.Matches(someText);
作为证明:以下是来自控制台应用程序的IL,其中包含上述代码(1)和一个oneliner(2):
独立变量:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 27 (0x1b)
.maxstack 2
.locals init ([0] string someText,
[1] class [System]System.Text.RegularExpressions.Regex r,
[2] class [System]System.Text.RegularExpressions.MatchCollection matchCollection)
IL_0000: nop
IL_0001: ldstr "s"
IL_0006: stloc.0
IL_0007: ldstr "example"
IL_000c: newobj instance void [System]System.Text.RegularExpressions.Regex::.ctor(string)
IL_0011: stloc.1
IL_0012: ldloc.1
IL_0013: ldloc.0
IL_0014: callvirt instance class [System]System.Text.RegularExpressions.MatchCollection [System]System.Text.RegularExpressions.Regex::Matches(string)
IL_0019: stloc.2
IL_001a: ret
} // end of method Program::Main
Oneliner:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 25 (0x19)
.maxstack 2
.locals init ([0] string someText,
[1] class [System]System.Text.RegularExpressions.MatchCollection matchCollection)
IL_0000: nop
IL_0001: ldstr "s"
IL_0006: stloc.0
IL_0007: ldstr "example"
IL_000c: newobj instance void [System]System.Text.RegularExpressions.Regex::.ctor(string)
IL_0011: ldloc.0
IL_0012: call instance class [System]System.Text.RegularExpressions.MatchCollection [System]System.Text.RegularExpressions.Regex::Matches(string)
IL_0017: stloc.1
IL_0018: ret
} // end of method Program::Main
您将看到实际的代码不同。从堆栈中再推和弹出一个变量,实际调用略有不同,但仅此而已。他们仍然调用同一个对象,只是它存在的位置不同。否。不可能在实际使用之前对该对象进行GC'ed 事实上,它(几乎)与:
var r = new Regex("example");
MatchCollection matchCollection = r.Matches(someText);
作为证明:以下是来自控制台应用程序的IL,其中包含上述代码(1)和一个oneliner(2):
独立变量:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 27 (0x1b)
.maxstack 2
.locals init ([0] string someText,
[1] class [System]System.Text.RegularExpressions.Regex r,
[2] class [System]System.Text.RegularExpressions.MatchCollection matchCollection)
IL_0000: nop
IL_0001: ldstr "s"
IL_0006: stloc.0
IL_0007: ldstr "example"
IL_000c: newobj instance void [System]System.Text.RegularExpressions.Regex::.ctor(string)
IL_0011: stloc.1
IL_0012: ldloc.1
IL_0013: ldloc.0
IL_0014: callvirt instance class [System]System.Text.RegularExpressions.MatchCollection [System]System.Text.RegularExpressions.Regex::Matches(string)
IL_0019: stloc.2
IL_001a: ret
} // end of method Program::Main
Oneliner:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 25 (0x19)
.maxstack 2
.locals init ([0] string someText,
[1] class [System]System.Text.RegularExpressions.MatchCollection matchCollection)
IL_0000: nop
IL_0001: ldstr "s"
IL_0006: stloc.0
IL_0007: ldstr "example"
IL_000c: newobj instance void [System]System.Text.RegularExpressions.Regex::.ctor(string)
IL_0011: ldloc.0
IL_0012: call instance class [System]System.Text.RegularExpressions.MatchCollection [System]System.Text.RegularExpressions.Regex::Matches(string)
IL_0017: stloc.1
IL_0018: ret
} // end of method Program::Main
您将看到实际的代码不同。从堆栈中再推和弹出一个变量,实际调用略有不同,但仅此而已。他们仍然调用同一个对象,只是它存在的地方不同。我不希望得到如此详细的答案。谢谢你教我如何调查自己!我不希望得到如此详细的答复。谢谢你教我如何调查自己!我不希望得到如此详细的答复。谢谢你教我如何调查自己!我不希望得到如此详细的答复。谢谢你教我如何调查自己!