|
1 |
| -// Licensed to the .NET Foundation under one or more agreements. |
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System.Runtime.CompilerServices; |
| 5 | + |
4 | 6 | namespace System
|
5 | 7 | {
|
6 |
| - using Runtime.CompilerServices; |
7 |
| - |
8 | 8 | /// <summary>
|
9 | 9 | /// Controls the system garbage collector, a service that automatically reclaims unused memory.
|
10 | 10 | /// </summary>
|
11 | 11 | public static class GC
|
12 | 12 | {
|
| 13 | +#pragma warning disable S4200 // Native methods should be wrapped |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Enables or disables the output of garbage collection messages. |
| 17 | + /// </summary> |
| 18 | + /// <param name="enable"><see langword="true"/> to enable the output of GC messages; otherwise, <see langword="false"/>.</param> |
| 19 | + /// <remarks> |
| 20 | + /// <para> |
| 21 | + /// Enabling GC messages may not always result in output, depending on the target build options. |
| 22 | + /// For example, RTM builds, which remove all non-essential features, may not output these messages. |
| 23 | + /// </para> |
| 24 | + /// <para> |
| 25 | + /// This method is specific of .NET nanoFramework implementation. There is no equivalent in full .NET API. |
| 26 | + /// </para> |
| 27 | + /// </remarks> |
13 | 28 | [MethodImpl(MethodImplOptions.InternalCall)]
|
14 |
| - private static extern bool AnyPendingFinalizers(); |
| 29 | + public static extern void EnableGCMessages(bool enable); |
15 | 30 |
|
16 | 31 | /// <summary>
|
17 |
| - /// Suspends the current thread until the thread that is processing the queue of finalizers has emptied that queue. |
| 32 | + /// Retrieves the heap size excluding fragmentation. For example if the total GC heap size is 1MB and fragmentation, ie, space taken up by free objects, takes up 400kB, this API would report 600kB. A parameter indicates whether this method can wait a short interval before returning, to allow the system to collect garbage and finalize objects. |
18 | 33 | /// </summary>
|
19 |
| - public static void WaitForPendingFinalizers() |
20 |
| - { |
21 |
| - while (AnyPendingFinalizers()) Threading.Thread.Sleep(10); |
22 |
| - } |
| 34 | + /// <param name="forceFullCollection"><see langword="true"/> to indicate that this method can wait for garbage collection and heap compaction to occur before returning; otherwise, <see langword="false"/>.</param> |
| 35 | + /// <returns>The heap size, in bytes, excluding fragmentation.</returns> |
| 36 | + public static long GetTotalMemory(bool forceFullCollection) => Run(forceFullCollection); |
23 | 37 |
|
24 | 38 | /// <summary>
|
25 |
| - /// Requests that the system not call the finalizer for the specified object. |
| 39 | + /// Requests that the system call the finalizer for the specified object for which <see cref="SuppressFinalize"/> has previously been called. |
26 | 40 | /// </summary>
|
27 |
| - /// <param name="obj">The object that a finalizer must not be called for. </param> |
| 41 | + /// <param name="obj">The object that a finalizer must be called for.</param> |
| 42 | + /// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception> |
28 | 43 | [MethodImpl(MethodImplOptions.InternalCall)]
|
29 |
| - public static extern void SuppressFinalize(Object obj); |
| 44 | + public static extern void ReRegisterForFinalize(object obj); |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Forces an immediate garbage collection of all generations. |
| 48 | + /// </summary> |
| 49 | + /// <remarks> |
| 50 | + /// Use this method to try to reclaim all memory that is inaccessible. It performs a blocking garbage collection of all generations. |
| 51 | + /// All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to try to reclaim the maximum amount of available memory. |
| 52 | + /// </remarks> |
| 53 | + public static void Collect() => Run(true); |
30 | 54 |
|
31 | 55 | /// <summary>
|
32 |
| - /// Requests that the system call the finalizer for the specified object for which SuppressFinalize has previously been called. |
| 56 | + /// Requests that the common language runtime not call the finalizer for the specified object. |
33 | 57 | /// </summary>
|
34 |
| - /// <param name="obj">The object that a finalizer must be called for. </param> |
| 58 | + /// <param name="obj">The object whose finalizer must not be executed.</param> |
| 59 | + /// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception> |
35 | 60 | [MethodImpl(MethodImplOptions.InternalCall)]
|
36 |
| - public static extern void ReRegisterForFinalize(Object obj); |
| 61 | + public static extern void SuppressFinalize(object obj); |
37 | 62 |
|
| 63 | + /// <summary> |
| 64 | + /// Suspends the current thread until the thread that is processing the queue of finalizers has emptied that queue. |
| 65 | + /// </summary> |
| 66 | + public static void WaitForPendingFinalizers() |
| 67 | + { |
| 68 | + while (AnyPendingFinalizers()) Threading.Thread.Sleep(10); |
| 69 | + } |
| 70 | + |
| 71 | +#pragma warning restore S4200 // Native methods should be wrapped |
| 72 | + |
| 73 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 74 | + private static extern bool AnyPendingFinalizers(); |
| 75 | + |
| 76 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 77 | + private static extern uint Run(bool compactHeap); |
38 | 78 | }
|
39 | 79 | }
|
0 commit comments