简体   繁体   English

C# 在运行时编译 finalize 方法?

[英]C# compile finalize method's on runtime?

As im reading through 3 books about GC, ive notice some strange fact:在阅读有关 GC 的 3 本书时,我注意到一些奇怪的事实:

C# via CLR C# 通过 CLR

CriticalFinalizerObject : the CLR treats this class and classes derived from it in a very special manner CriticalFinalizerObjectCLR 以非常特殊的方式对待这个 class 和从它派生的类

在此处输入图像描述

what???什么???

"not find enough memory to COMPILE a method? " IMHO - the code should be already compiled... no? “找不到足够的 memory 来编译一个方法? ”恕我直言——代码应该已经编译了……不是吗?

when Im writing c# code - the whole code is compiled to IL before its running... no?当我写 c# 代码时——整个代码在运行之前被编译成 IL……不是吗? but according to the text - at RUNTIME - he MAY find insufficient memory for compile ...但根据文本 - 在运行时 - 他可能会发现memory不足以编译......

Help?帮助?

The JIT compiler only compiles methods from IL to native code at runtime the first time they are executed. JIT 编译器仅在第一次执行时将方法从 IL 编译为本机代码。 As you might expect, this requires extra memory. So, normal finalizers are only compiled right before they are exeucted by the cleanup thread.如您所料,这需要额外的 memory。因此,普通终结器仅在清理线程执行之前才编译。

An object derived from CriticalFinalizerObject has it's finalizer compiled immediately, so no extra memory is required to execute it at program shutdown.CriticalFinalizerObject派生的 object 会立即编译它的终结器,因此在程序关闭时不需要额外的 memory 来执行它。 This is for objects that must have their finalizer executed if at all possible (nonwithstanding a power cut or similar)这适用于必须尽可能执行其终结器的对象(尽管断电或类似情况)

I think it is the backend of the 'Compiler'.我认为这是“编译器”的后端。 From the IL to the Machine code.从 IL 到机器代码。

With compiler is the JIT compiler meant to compile your finalizer methods eagerly and not to delay its compilation shortly before it is executed.使用编译器是 JIT 编译器,旨在急切地编译您的终结器方法,而不是在执行前不久延迟其编译。

The deeper reason behind it is that these finalizers are called during application shutdown after all normal finalizers were executed.其背后更深层次的原因是这些终结器在应用程序关闭期间在所有正常终结器执行后被调用。 But the CLR does execute all pending finalizers with a timeout of 2s (at least this was it with .NET 2.0 have not checked since then again).但是 CLR 确实执行所有挂起的终结器,超时为 2 秒(至少这是 .NET 2.0 从那时起就没有检查过)。 Then the critical finalizers are executed.然后执行关键终结器。

Critial finalizer have rare uses eg for handles which need to be closed in any case. Critial finalizer 很少使用,例如用于在任何情况下都需要关闭的句柄。 But you can also use them to hold resources open until all finalizers are executed to enable egtracing even inside finalizers .但是您也可以使用它们来保持资源打开,直到所有终结器都被执行以启用例如甚至在终结器内部的跟踪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM