-
-
Notifications
You must be signed in to change notification settings - Fork 31
Work on GC
APIs
#243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Work on GC
APIs
#243
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,79 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
namespace System | ||
{ | ||
using Runtime.CompilerServices; | ||
|
||
/// <summary> | ||
/// Controls the system garbage collector, a service that automatically reclaims unused memory. | ||
/// </summary> | ||
public static class GC | ||
{ | ||
#pragma warning disable S4200 // Native methods should be wrapped | ||
|
||
/// <summary> | ||
/// Enables or disables the output of garbage collection messages. | ||
/// </summary> | ||
/// <param name="enable"><see langword="true"/> to enable the output of GC messages; otherwise, <see langword="false"/>.</param> | ||
/// <remarks> | ||
/// <para> | ||
/// Enabling GC messages may not always result in output, depending on the target build options. | ||
/// For example, RTM builds, which remove all non-essential features, may not output these messages. | ||
/// </para> | ||
/// <para> | ||
/// This method is specific of .NET nanoFramework implementation. There is no equivalent in full .NET API. | ||
/// </para> | ||
/// </remarks> | ||
[MethodImpl(MethodImplOptions.InternalCall)] | ||
private static extern bool AnyPendingFinalizers(); | ||
public static extern void EnableGCMessages(bool enable); | ||
|
||
/// <summary> | ||
/// Suspends the current thread until the thread that is processing the queue of finalizers has emptied that queue. | ||
/// 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. | ||
/// </summary> | ||
public static void WaitForPendingFinalizers() | ||
{ | ||
while (AnyPendingFinalizers()) Threading.Thread.Sleep(10); | ||
} | ||
/// <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> | ||
/// <returns>The heap size, in bytes, excluding fragmentation.</returns> | ||
public static long GetTotalMemory(bool forceFullCollection) => Run(forceFullCollection); | ||
|
||
/// <summary> | ||
/// Requests that the system not call the finalizer for the specified object. | ||
/// Requests that the system call the finalizer for the specified object for which <see cref="SuppressFinalize"/> has previously been called. | ||
/// </summary> | ||
/// <param name="obj">The object that a finalizer must not be called for. </param> | ||
/// <param name="obj">The object that a finalizer must be called for.</param> | ||
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception> | ||
[MethodImpl(MethodImplOptions.InternalCall)] | ||
public static extern void SuppressFinalize(Object obj); | ||
public static extern void ReRegisterForFinalize(object obj); | ||
|
||
/// <summary> | ||
/// Forces an immediate garbage collection of all generations. | ||
/// </summary> | ||
/// <remarks> | ||
/// Use this method to try to reclaim all memory that is inaccessible. It performs a blocking garbage collection of all generations. | ||
/// 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. | ||
/// </remarks> | ||
public static void Collect() => Run(true); | ||
|
||
/// <summary> | ||
/// Requests that the system call the finalizer for the specified object for which SuppressFinalize has previously been called. | ||
/// Requests that the common language runtime not call the finalizer for the specified object. | ||
/// </summary> | ||
/// <param name="obj">The object that a finalizer must be called for. </param> | ||
/// <param name="obj">The object whose finalizer must not be executed.</param> | ||
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception> | ||
[MethodImpl(MethodImplOptions.InternalCall)] | ||
public static extern void ReRegisterForFinalize(Object obj); | ||
public static extern void SuppressFinalize(object obj); | ||
|
||
/// <summary> | ||
/// Suspends the current thread until the thread that is processing the queue of finalizers has emptied that queue. | ||
/// </summary> | ||
public static void WaitForPendingFinalizers() | ||
{ | ||
while (AnyPendingFinalizers()) Threading.Thread.Sleep(10); | ||
} | ||
|
||
#pragma warning restore S4200 // Native methods should be wrapped | ||
|
||
[MethodImpl(MethodImplOptions.InternalCall)] | ||
private static extern bool AnyPendingFinalizers(); | ||
|
||
[MethodImpl(MethodImplOptions.InternalCall)] | ||
private static extern uint Run(bool compactHeap); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.