-
Notifications
You must be signed in to change notification settings - Fork 5k
Reduce background GC allocations #114320
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
base: main
Are you sure you want to change the base?
Reduce background GC allocations #114320
Changes from all commits
92559e7
116083d
23b3064
5c22178
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,7 @@ | |
public static partial class GC | ||
{ | ||
[MethodImpl(MethodImplOptions.InternalCall)] | ||
private static extern void GetMemoryInfo(GCMemoryInfoData data, int kind); | ||
private static extern unsafe void GetMemoryInfo(GCMemoryInfoData* data, int kind); | ||
|
||
/// <summary>Gets garbage collection memory information.</summary> | ||
/// <returns>An object that contains information about the garbage collector's memory usage.</returns> | ||
|
@@ -69,7 +69,7 @@ | |
/// <summary>Gets garbage collection memory information.</summary> | ||
/// <param name="kind">The kind of collection for which to retrieve memory information.</param> | ||
/// <returns>An object that contains information about the garbage collector's memory usage.</returns> | ||
public static GCMemoryInfo GetGCMemoryInfo(GCKind kind) | ||
public static unsafe GCMemoryInfo GetGCMemoryInfo(GCKind kind) | ||
{ | ||
if ((kind < GCKind.Any) || (kind > GCKind.Background)) | ||
{ | ||
|
@@ -80,8 +80,8 @@ | |
GCKind.Background)); | ||
} | ||
|
||
var data = new GCMemoryInfoData(); | ||
GetMemoryInfo(data, (int)kind); | ||
GCMemoryInfoData data = default; | ||
GetMemoryInfo(&data, (int)kind); | ||
return new GCMemoryInfo(data); | ||
} | ||
|
||
|
@@ -414,7 +414,7 @@ | |
|
||
|
||
/// <summary> | ||
/// Get a count of the bytes allocated over the lifetime of the process. | ||
Check failure on line 417 in src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
|
||
/// </summary> | ||
/// <param name="precise">If true, gather a precise number, otherwise gather a fairly count. Gathering a precise value triggers at a significant performance penalty.</param> | ||
public static long GetTotalAllocatedBytes(bool precise = false) => precise ? GetTotalAllocatedBytesPrecise() : GetTotalAllocatedBytesApproximate(); | ||
|
@@ -599,12 +599,12 @@ | |
return true; | ||
} | ||
|
||
// We need to take a snapshot of s_notifications.Count, so that in the case that | ||
Check failure on line 602 in src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
|
||
// s_notifications[i].Notification() registers new notifications, we neither get rid | ||
Check failure on line 603 in src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
|
||
// of them nor iterate over them. | ||
Check failure on line 604 in src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
|
||
int count = s_notifications.Count; | ||
|
||
// If there is no existing notifications, we won't be iterating over any and we won't | ||
Check failure on line 607 in src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
|
||
// be adding any new one. Also, there wasn't any added since we last invoked this | ||
// method so it's safe to assume we can reset s_previousMemoryLoad. | ||
if (count == 0) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #53296 for a long discussion about why it is not the best idea to expose struct enumerator for ConditionalWeakTable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I see you have mentioned in a comment - but that does not make it a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm well aware of the dangers, which is why it's internal only. I left a lengthy comment about it on the
EnumerateOnStack()
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are not interested in spreading more unsafe or otherwise dangerous code throughout the libraries. We believe we have too much of it and we are actively working on reducing it.