-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add an API to reduce GC memory usage #50902
Comments
Tagging subscribers to this area: @dotnet/gc Issue DetailsBackground and MotivationIn some cases, it's desirable for GC to reduce memory usage at the possible expense of increased GC pause times. The best tradeoff between memory usage and GC pause time will depend on the scenario, so what is proposed below is a dial where a value of 0 specifies the default behavior, and higher values specify increasingly aggressive measures to keep GC heap size low. Proposed API public static partial class GCOptimizationGoal
{
/// <summary>
/// This property is a dial that tells GC to try to reduce memory usage.
/// A value of 0 specifies the default behavior.
/// Values 1 through 9 specify increasingly aggressive behavior to reduce
/// memory usage, at the possible expense of increased GC pause time.
/// </summary>
public static int Memory { get; set; }
} Usage Examples// Example 1:
// Keep heap size fairly low
GCOptimizationGoal.Memory = 7;
// Example 2:
// save setting
int savedMemorySetting = GCOptimizationGoal.Memory;
try
{
// Relax memory goal for the following to reduce pause times
GCOptimizationGoal.Memory = 0;
// do work
}
finally
{
// restore setting
GCOptimizationGoal.Memory = savedMemorySetting;
} --> Alternative DesignsA plausible alternative would be to add a static property to the GCSettings class instead, for example: public static partial class GCSettings
{
// ... existing members ...
/// <summary>
/// This property is a dial that tells GC to try to reduce memory usage.
/// A value of 0 specifies the default behavior.
/// Values 1 through 9 specify increasingly aggressive behavior to reduce
/// memory usage, at the possible expense of increased GC pause time.
/// </summary>
public static int OptimizeMemory { get; set; }
} RisksThe risks can be kept fairly low by making sure the default behavior stays the same. As mentioned above, at the higher settings GC pause times will increase.
|
GCOptimizationGoal.Memory isn't a very a clear name - I would in fact assume that higher amounts meant use more memory. I think an enum would be clearer. What if you want to go the opposite way? Use as much memory as you want to increase performance? I presume the server GC is tuned for that anyways so there's no need. I think with 10 levels it's very difficult to decide which one to pick. Maybe 2 modes are all that's really necessary? Optimize space vs Optimize performance? |
that's interesting, when we talk about memory performance, it generally has 3 aspects, space (or more commonly referred to as memory size) is one of them. the other 2 are throughput/time and pause duration. this is explained here. when folks talk about optimizing for an aspect, the convention from most folks I've worked with seems to be - memory -> lower is better so when someone talks about "I want to optimize for memory" they mean "I want to use less memory". interested to hear what other people think of this. |
@richlander since this is related to dotnet/core#5488. Perhaps we can broaden the knobs. |
I think that having a dial is preferable over an enum because it allows for more fine-grained adjustment. Of course, some experimentation and measurement will be required to arrive at a suitable setting for each scenario, but a setting in the middle of the range (say 4 to 6) will be a reasonable starting point in most cases. I am open to imroving the naming to reduce potential misunderstanding. |
since there haven't been many comments I think we can take this to API review folks to figure out the naming/convention/etc so I've set it to ready for review. |
This is interesting. The problem with these sorts of APIs is ensuring that it fits within an actual functioning app. For example, what's the data that would inform an app that it should |
Would it be possible to actually reduce gc pauses at the expense of bigger memory consumption compared to current default? I mean lets say increase allowed level to 11-15 and default level would be 2-5 and that would mean current behaviour while values lower than default would make gc "lazier" than now. Or there wont be any meangiful gains because gc is already as "lazy" as possible? |
This looks better to me as it is similar to the existing |
namespace System.Runtime
{
partial static class GCSettings
{
// NaN represents "there is no goal"
// Setter throws for things outside the range [0, 1] union NaN
public static double MemoryOptimizationGoal { get; set; }
}
} |
Doesn't seem like this is implemented yet. @PeterSolMS is this still on track of .NET 6? |
Any update ?, is it planned for .NET 7 |
@madhub It needs real implementation in GC. We don't want to expose such API in advance with no actual effect.
I don't think they should be related. There are more behavior difference between workstation and server GC. |
Wouldn't a |
Possibly; but I doubt that the GC's backing integer values will ever exceed 24 bits of granularity, so System.Single probably isn't limiting; but it's a bit unnatural, and this API shouldn't really be on any hot path that would warrant the squeeze. |
Can any of you explain roughly what this change involves on the GC side? If this is a simple topic for beginners, it would certainly be interesting for me, because I have not had much to do with the GC. |
Background and Motivation
In some cases, it's desirable for GC to reduce memory usage at the possible expense of increased GC pause times.
The best tradeoff between memory usage and GC pause time will depend on the scenario, so what is proposed below is a dial where a value of 0 specifies the default behavior, and higher values specify increasingly aggressive measures to keep GC heap size low.
Proposed API
Usage Examples
-->
Alternative Designs
A plausible alternative would be to add a static property to the GCSettings class instead, for example:
Risks
The risks can be kept fairly low by making sure the default behavior stays the same. As mentioned above, at the higher settings GC pause times will increase.
The text was updated successfully, but these errors were encountered: