GC.TryStartNoGCRegion 方法

定义

在关键路径执行期间尝试禁止垃圾回收。Attempts to disallow garbage collection during the execution of a critical path.

重载

TryStartNoGCRegion(Int64)

如果指定数量的内存可用,则在关键路径执行期间尝试禁止垃圾回收。Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available.

TryStartNoGCRegion(Int64, Boolean)

如果指定数量的内存可用,则在关键路径执行期间尝试禁止垃圾回收;并在初始没有足够内存可用的情况下,控制垃圾回收器是否进行完整的阻碍性垃圾回收。Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available, and controls whether the garbage collector does a full blocking garbage collection if not enough memory is initially available.

TryStartNoGCRegion(Int64, Int64)

如果指定数量的内存可用于大对象堆和小对象堆,则在关键路径执行期间尝试禁止垃圾回收。Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available for the large object heap and the small object heap.

TryStartNoGCRegion(Int64, Int64, Boolean)

如果指定数量的内存可用大对象堆和小对象堆,则在关键路径执行期间尝试禁止垃圾回收;并在初始没有足够内存可用的情况下,控制垃圾回收器是否进行完整的阻碍性垃圾回收。Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available for the large object heap and the small object heap, and controls whether the garbage collector does a full blocking garbage collection if not enough memory is initially available.

注解

重要

不能将调用嵌套到 TryStartNoGCRegion 方法,只应在运行时当前处于无 GC 区域延迟模式时调用 EndNoGCRegion 方法。You cannot nest calls to the TryStartNoGCRegion method, and you should only call the EndNoGCRegion method if the runtime is currently in no GC region latency mode. 换句话说,您不应多次调用 TryStartNoGCRegion (在第一次调用方法后,后续调用将不会成功),并且您不应该只是因为对 TryStartNoGCRegion 的第一次调用成功,因此不应指望 EndNoGCRegion 成功。In other words, you should not call TryStartNoGCRegion multiple times (after the first method call, subsequent calls will not succeed), and you should not expect calls to EndNoGCRegion to succeed just because the first call to TryStartNoGCRegion succeeded.

TryStartNoGCRegion(Int64)

如果指定数量的内存可用,则在关键路径执行期间尝试禁止垃圾回收。Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available.

public:
 static bool TryStartNoGCRegion(long totalSize);
[System.Security.SecurityCritical]
public static bool TryStartNoGCRegion (long totalSize);
static member TryStartNoGCRegion : int64 -> bool
Public Shared Function TryStartNoGCRegion (totalSize As Long) As Boolean

参数

totalSize
Int64

在不会触发垃圾回收的情况下分配的内存量(以字节为单位)。The amount of memory in bytes to allocate without triggering a garbage collection. 它必须小于或等于临时段的大小。It must be less than or equal to the size of an ephemeral segment. 有关暂时段大小的信息,请参阅垃圾回收基础知识一文中“暂时代和暂时段”一节。For information on the size of an ephemeral segment, see the "Ephemeral generations and segments" section in the Fundamentals of Garbage Collection article.

返回

如果运行时能够调配所需数量的内存,且垃圾回收器能够进入无 GC 区域延迟模式,则为 true;否则为 falsetrue if the runtime was able to commit the required amount of memory and the garbage collector is able to enter no GC region latency mode; otherwise, false.

属性

异常

totalSize 超出了暂时段大小。totalSize exceeds the ephemeral segment size.

进程已进入无 GC 区域延迟模式。The process is already in no GC region latency mode.

注解

TryStartNoGCRegion(Int64) 方法尝试将垃圾回收器置于无 GC 区域延迟模式,这在应用执行关键的代码区域时不允许垃圾回收。The TryStartNoGCRegion(Int64) method attempts to place the garbage collector in no GC region latency mode, which disallows garbage collection while an app executes a critical region of code. 如果运行时无法最初分配所请求的内存量,垃圾回收器将执行完全阻止性垃圾回收,尝试释放更多内存。If the runtime is unable to initially allocate the requested amount of memory, the garbage collector performs a full blocking garbage collection in an attempt to free additional memory. 如果垃圾回收器可以分配所需的内存量(在本例中为 2 * totalSize 字节,则它将尝试为小对象堆分配 totalSize 字节,并为大型对象堆 totalSize 字节),则不会输入 GC 区域延迟模式。The garbage collector enters no GC region latency mode if it is able to allocate the required amount of memory, which in this case is actually 2 * totalSize bytes (it attempts to allocate totalSize bytes for the small object heap and totalSize bytes for the large object heap).

totalSize 必须足够大才能处理在关键路径中发生的所有内存分配。totalSize must be large enough to handle all memory allocations that occur in the critical path. 这包括应用的分配以及运行时代表应用程序进行的分配。This includes allocations by the app, as well as allocations that the runtime makes on the app's behalf.

重要

不能将调用嵌套到 TryStartNoGCRegion 方法,只应在运行时当前处于无 GC 区域延迟模式时调用 EndNoGCRegion 方法。You cannot nest calls to the TryStartNoGCRegion method, and you should only call the EndNoGCRegion method if the runtime is currently in no GC region latency mode. 换句话说,您不应多次调用 TryStartNoGCRegion (在第一次调用方法后,后续调用将不会成功),并且您不应该只是因为对 TryStartNoGCRegion 的第一次调用成功,因此不应指望 EndNoGCRegion 成功。In other words, you should not call TryStartNoGCRegion multiple times (after the first method call, subsequent calls will not succeed), and you should not expect calls to EndNoGCRegion to succeed just because the first call to TryStartNoGCRegion succeeded.

可以通过调用 EndNoGCRegion 方法退出无 GC 区域延迟模式。You exit the no GC region latency mode by calling the EndNoGCRegion method.

另请参阅

TryStartNoGCRegion(Int64, Boolean)

如果指定数量的内存可用,则在关键路径执行期间尝试禁止垃圾回收;并在初始没有足够内存可用的情况下,控制垃圾回收器是否进行完整的阻碍性垃圾回收。Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available, and controls whether the garbage collector does a full blocking garbage collection if not enough memory is initially available.

public:
 static bool TryStartNoGCRegion(long totalSize, bool disallowFullBlockingGC);
[System.Security.SecurityCritical]
public static bool TryStartNoGCRegion (long totalSize, bool disallowFullBlockingGC);
static member TryStartNoGCRegion : int64 * bool -> bool
Public Shared Function TryStartNoGCRegion (totalSize As Long, disallowFullBlockingGC As Boolean) As Boolean

参数

totalSize
Int64

在不会触发垃圾回收的情况下分配的内存量(以字节为单位)。The amount of memory in bytes to allocate without triggering a garbage collection. 它必须小于或等于临时段的大小。It must be less than or equal to the size of an ephemeral segment. 有关暂时段大小的信息,请参阅垃圾回收基础知识一文中“暂时代和暂时段”一节。For information on the size of an ephemeral segment, see the "Ephemeral generations and segments" section in the Fundamentals of Garbage Collection article.

disallowFullBlockingGC
Boolean

true 表示如果垃圾回收器初始无法分配 totalSize 字节,则忽略完整的阻碍性垃圾回收;否则为 falsetrue to omit a full blocking garbage collection if the garbage collector is initially unable to allocate totalSize bytes; otherwise, false.

返回

如果运行时能够调配所需数量的内存,且垃圾回收器能够进入无 GC 区域延迟模式,则为 true;否则为 falsetrue if the runtime was able to commit the required amount of memory and the garbage collector is able to enter no GC region latency mode; otherwise, false.

属性

异常

totalSize 超出了暂时段大小。totalSize exceeds the ephemeral segment size.

进程已进入无 GC 区域延迟模式。The process is already in no GC region latency mode.

注解

TryStartNoGCRegion(Int64, Boolean) 方法尝试将垃圾回收器置于无 GC 区域延迟模式,这在应用执行关键的代码区域时不允许垃圾回收。The TryStartNoGCRegion(Int64, Boolean) method attempts to place the garbage collector in no GC region latency mode, which disallows garbage collection while an app executes a critical region of code. 如果运行时无法最初分配所请求的内存量,并且 false``disallowFullBlockingGC 参数,垃圾回收器将执行完全阻止性垃圾回收,尝试释放更多内存;否则,分配将失败,并且方法将返回 falseIf the runtime is unable to initially allocate the requested amount of memory and the disallowFullBlockingGC argument is false, the garbage collector performs a full blocking garbage collection in an attempt to free additional memory; otherwise, the allocation fails, and the method returns false. 如果垃圾回收器能够分配所需的内存量(在这种情况下,实际上是 2 * totalSize (它会尝试为小对象堆分配 totalSize,并为大型对象堆 totalSize),则垃圾回收器不会输入 GC 区域延迟模式。The garbage collector enters no GC region latency mode if it is able to allocate the required amount of memory, which in this case is actually 2 * totalSize (it attempts to allocate totalSize for the small object heap and totalSize for the large object heap).

totalSize 必须足够大才能处理在关键路径中发生的所有内存分配。totalSize must be large enough to handle all memory allocations that occur in the critical path. 这包括应用的分配以及运行时代表应用程序进行的分配。This includes allocations by the app, as well as allocations that the runtime makes on the app's behalf.

disallowFullBlockingGC 设置为 "true 以防止完全阻止垃圾回收的情况下,如果最初没有足够的内存可用,则在负载平衡方案中最有用:一个系统可以调用此方法,并将其报告为准备好在返回 true的情况下接受请求,并让负载均衡器将请求重定向到其他系统(如果返回 false)。Setting disallowFullBlockingGC to true to prevent a full blocking garbage collection if not enough memory is initially available is most useful in load balancing scenarios: one system can call this method and report itself as ready to accept requests if it returns true, and have the load balancer redirect requests to other systems if it returns false. 然后,在通过调用 Collect(Int32, GCCollectionMode, Boolean, Boolean) 方法来处理请求时,它可以执行完全阻止性垃圾回收。It can then do a full blocking garbage collection when it's not handling requests by calling the Collect(Int32, GCCollectionMode, Boolean, Boolean) method.

重要

不能将调用嵌套到 TryStartNoGCRegion 方法,只应在运行时当前处于无 GC 区域延迟模式时调用 EndNoGCRegion 方法。You cannot nest calls to the TryStartNoGCRegion method, and you should only call the EndNoGCRegion method if the runtime is currently in no GC region latency mode. 换句话说,您不应多次调用 TryStartNoGCRegion (在第一次调用方法后,后续调用将不会成功),并且您不应该只是因为对 TryStartNoGCRegion 的第一次调用成功,因此不应指望 EndNoGCRegion 成功。In other words, you should not call TryStartNoGCRegion multiple times (after the first method call, subsequent calls will not succeed), and you should not expect calls to EndNoGCRegion to succeed just because the first call to TryStartNoGCRegion succeeded.

可以通过调用 EndNoGCRegion 方法退出无 GC 区域延迟模式。You exit the no GC region latency mode by calling the EndNoGCRegion method.

另请参阅

TryStartNoGCRegion(Int64, Int64)

如果指定数量的内存可用于大对象堆和小对象堆,则在关键路径执行期间尝试禁止垃圾回收。Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available for the large object heap and the small object heap.

public:
 static bool TryStartNoGCRegion(long totalSize, long lohSize);
[System.Security.SecurityCritical]
public static bool TryStartNoGCRegion (long totalSize, long lohSize);
static member TryStartNoGCRegion : int64 * int64 -> bool
Public Shared Function TryStartNoGCRegion (totalSize As Long, lohSize As Long) As Boolean

参数

totalSize
Int64

在不会触发垃圾回收的情况下分配的内存量(以字节为单位)。The amount of memory in bytes to allocate without triggering a garbage collection. totalSize -lohSize 必须小于或等于临时段的大小。totalSize -lohSize must be less than or equal to the size of an ephemeral segment. 有关暂时段大小的信息,请参阅垃圾回收基础知识一文中“暂时代和暂时段”一节。For information on the size of an ephemeral segment, see the "Ephemeral generations and segments" section in the Fundamentals of Garbage Collection article.

lohSize
Int64

totalSize 中用于大对象堆 (LOH) 分配的字节数。The number of bytes in totalSize to use for large object heap (LOH) allocations.

返回

如果运行时能够调配所需数量的内存,且垃圾回收器能够进入无 GC 区域延迟模式,则为 true;否则为 falsetrue if the runtime was able to commit the required amount of memory and the garbage collector is able to enter no GC region latency mode; otherwise, false.

属性

异常

totalSize - lohSize 超出了暂时段大小。totalSize - lohSize exceeds the ephemeral segment size.

进程已进入无 GC 区域延迟模式。The process is already in no GC region latency mode.

注解

TryStartNoGCRegion(Int64, Int64) 方法尝试将垃圾回收器置于无 GC 区域延迟模式,这在应用执行关键的代码区域时不允许垃圾回收。The TryStartNoGCRegion(Int64, Int64) method attempts to place the garbage collector in no GC region latency mode, which disallows garbage collection while an app executes a critical region of code. 如果运行时无法最初分配所请求的内存量,垃圾回收器将执行完全阻止性垃圾回收,尝试释放更多内存。If the runtime is unable to initially allocate the requested amount of memory, the garbage collector performs a full blocking garbage collection in an attempt to free additional memory. 如果垃圾回收器能够为小对象堆(SOH)的 LOH 和 totalSize - lohSize 分配 lohSize,则垃圾回收器不会输入 GC 区域延迟模式。The garbage collector enters no GC region latency mode if it is able to allocate lohSize for the LOH and totalSize - lohSize for the small object heap (SOH).

lohSize 必须足够大才能处理 LOH 的关键路径中发生的所有内存分配,并且 totalSize - lohSize 必须足够大才能处理 SOH 关键路径中发生的所有内存分配。lohSize must be large enough to handle all memory allocations that occur in the critical path for the LOH, and totalSize - lohSize must be large enough to handle all memory allocations that occur in the critical path for the SOH. 这包括应用的分配以及运行时代表应用程序进行的分配。This includes allocations by the app, as well as allocations that the runtime makes on the app's behalf.

重要

不能将调用嵌套到 TryStartNoGCRegion 方法,只应在运行时当前处于无 GC 区域延迟模式时调用 EndNoGCRegion 方法。You cannot nest calls to the TryStartNoGCRegion method, and you should only call the EndNoGCRegion method if the runtime is currently in no GC region latency mode. 换句话说,您不应多次调用 TryStartNoGCRegion (在第一次调用方法后,后续调用将不会成功),并且您不应该只是因为对 TryStartNoGCRegion 的第一次调用成功,因此不应指望 EndNoGCRegion 成功。In other words, you should not call TryStartNoGCRegion multiple times (after the first method call, subsequent calls will not succeed), and you should not expect calls to EndNoGCRegion to succeed just because the first call to TryStartNoGCRegion succeeded.

可以通过调用 EndNoGCRegion 方法退出无 GC 区域延迟模式。You exit the no GC region latency mode by calling the EndNoGCRegion method.

另请参阅

TryStartNoGCRegion(Int64, Int64, Boolean)

如果指定数量的内存可用大对象堆和小对象堆,则在关键路径执行期间尝试禁止垃圾回收;并在初始没有足够内存可用的情况下,控制垃圾回收器是否进行完整的阻碍性垃圾回收。Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available for the large object heap and the small object heap, and controls whether the garbage collector does a full blocking garbage collection if not enough memory is initially available.

public:
 static bool TryStartNoGCRegion(long totalSize, long lohSize, bool disallowFullBlockingGC);
[System.Security.SecurityCritical]
public static bool TryStartNoGCRegion (long totalSize, long lohSize, bool disallowFullBlockingGC);
static member TryStartNoGCRegion : int64 * int64 * bool -> bool
Public Shared Function TryStartNoGCRegion (totalSize As Long, lohSize As Long, disallowFullBlockingGC As Boolean) As Boolean

参数

totalSize
Int64

在不会触发垃圾回收的情况下分配的内存量(以字节为单位)。The amount of memory in bytes to allocate without triggering a garbage collection. totalSize -lohSize 必须小于或等于临时段的大小。totalSize -lohSize must be less than or equal to the size of an ephemeral segment. 有关暂时段大小的信息,请参阅垃圾回收基础知识一文中“暂时代和暂时段”一节。For information on the size of an ephemeral segment, see the "Ephemeral generations and segments" section in the Fundamentals of Garbage Collection article.

lohSize
Int64

totalSize 中用于大对象堆 (LOH) 分配的字节数。The number of bytes in totalSize to use for large object heap (LOH) allocations.

disallowFullBlockingGC
Boolean

true 表示如果垃圾回收器初始无法分配小对象堆 (SOH) 和 LOH 上的指定内存,则忽略完整的阻碍性垃圾回收;否则为 falsetrue to omit a full blocking garbage collection if the garbage collector is initially unable to allocate the specified memory on the small object heap (SOH) and LOH; otherwise, false.

返回

如果运行时能够调配所需数量的内存,且垃圾回收器能够进入无 GC 区域延迟模式,则为 true;否则为 falsetrue if the runtime was able to commit the required amount of memory and the garbage collector is able to enter no GC region latency mode; otherwise, false.

属性

异常

totalSize - lohSize 超出了暂时段大小。totalSize - lohSize exceeds the ephemeral segment size.

进程已进入无 GC 区域延迟模式。The process is already in no GC region latency mode.

注解

TryStartNoGCRegion(Int64, Int64, Boolean) 方法尝试将垃圾回收器置于无 GC 区域延迟模式,这在应用执行关键的代码区域时不允许垃圾回收。The TryStartNoGCRegion(Int64, Int64, Boolean) method attempts to place the garbage collector in no GC region latency mode, which disallows garbage collection while an app executes a critical region of code. 如果运行时无法最初分配所请求的内存量,并且 false``disallowFullBlockingGC 参数,垃圾回收器将执行完全阻止性垃圾回收,尝试释放更多内存;否则,分配将失败,并且方法将返回 falseIf the runtime is unable to initially allocate the requested amount of memory and the disallowFullBlockingGC argument is false, the garbage collector performs a full blocking garbage collection in an attempt to free additional memory; otherwise, the allocation fails, and the method returns false. 如果垃圾回收器能够为小对象堆(SOH)的 LOH 和 totalSize - lohSize 分配 lohSize,则垃圾回收器不会输入 GC 区域延迟模式。The garbage collector enters no GC region latency mode if it is able to allocate lohSize for the LOH and totalSize - lohSize for the small object heap (SOH).

lohSize 必须足够大才能处理 LOH 的关键路径中发生的所有内存分配,并且 totalSize - lohSize 必须足够大才能处理 SOH 关键路径中发生的所有内存分配。lohSize must be large enough to handle all memory allocations that occur in the critical path for the LOH, and totalSize - lohSize must be large enough to handle all memory allocations that occur in the critical path for the SOH. 这包括应用的分配以及运行时代表应用程序进行的分配。This includes allocations by the app, as well as allocations that the runtime makes on the app's behalf.

disallowFullBlockingGC 设置为 "true 以防止完全阻止垃圾回收的情况下,如果最初没有足够的内存可用,则在负载平衡方案中最有用:一个系统可以调用此方法,并将其报告为准备好在返回 true的情况下接受请求,并让负载均衡器将请求重定向到其他系统(如果返回 false)。Setting disallowFullBlockingGC to true to prevent a full blocking garbage collection if not enough memory is initially available is most useful in load balancing scenarios: one system can call this method and report itself as ready to accept requests if it returns true, and have the load balancer redirect requests to other systems if it returns false. 然后,在通过调用 Collect(Int32, GCCollectionMode, Boolean, Boolean) 方法来处理请求时,它可以执行完全阻止性垃圾回收。It can then do a full blocking garbage collection when it's not handling requests by calling the Collect(Int32, GCCollectionMode, Boolean, Boolean) method.

重要

不能将调用嵌套到 TryStartNoGCRegion 方法,只应在运行时当前处于无 GC 区域延迟模式时调用 EndNoGCRegion 方法。You cannot nest calls to the TryStartNoGCRegion method, and you should only call the EndNoGCRegion method if the runtime is currently in no GC region latency mode. 换句话说,您不应多次调用 TryStartNoGCRegion (在第一次调用方法后,后续调用将不会成功),并且您不应该只是因为对 TryStartNoGCRegion 的第一次调用成功,因此不应指望 EndNoGCRegion 成功。In other words, you should not call TryStartNoGCRegion multiple times (after the first method call, subsequent calls will not succeed), and you should not expect calls to EndNoGCRegion to succeed just because the first call to TryStartNoGCRegion succeeded.

可以通过调用 EndNoGCRegion 方法退出无 GC 区域延迟模式。You exit the no GC region latency mode by calling the EndNoGCRegion method.

另请参阅

适用于