Replies: 4 comments 25 replies
-
I believe this is more a runtime question. The C# language specification does not specify what kind of garbage collection algorithm the runtime should provide, only that objects may be eventually deallocated when there are no more references. That does not seem to preclude a reference counting algorithm that would free immediately. |
Beta Was this translation helpful? Give feedback.
-
This does not appear to be a C# issue afaict. You could consider opening a discussion with teh runtime. They are the team that would have some concept of if an instance is 'referenced' and if/when those references are 'lost'. |
Beta Was this translation helpful? Give feedback.
-
If these are common operations for you, you should just write some extension methods. public static Image BlurAndDisposeCaller(this Image caller)
{
var result = caller.blur();
caller.Dispose();
return result;
} |
Beta Was this translation helpful? Give feedback.
-
As mentioned:
Regardless, this is not something the language cares about unless the runtime wants to do something special and believes there should be some special syntax to work with the ref counting system. My recommendation is that if you want to continue this, take it to the dotnet/runtime repo. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The era of immutable data has begun.
What does that mean?
For example immutable object Image() has allocated memory.
in C# If I call a transform chain like
.blur(...).contrast(...).resize(...)
it will create 3 copies of the modified images, and the allocated memory from.blur()
will not be freed aftercontrast()
on reference loss, but will accumulate, and will be freed maybe after an hour.This is not acceptable in 2k25.
The Image example is not the only one. Similarly, you can wrap GPU memory (tensors as in ML libraries), and perform operations. In GPU there is no way to wait even 1 second for memory to be freed, because memory is small and will quickly run out in hundreds of operations.
The code as
.blur(...).contrast(...).resize(...)
looks minimalistic and clear.Even python calls
__del__()
on reference loss, allowing you to free memory on the fly without waiting for GC.Currently in C# you can only handle this by explicitly calling
using
ordispose
, which is bullshit because 3 one-line operations.blur(...).contrast(...).resize(...)
would then take 9 lines.This is why C# lost the ML race without even entering it because of this serious flaw.
You should have 5 years ago added the ability to call code when references are lost to zero, for example
override Object.OnReferenceLose()
.Beta Was this translation helpful? Give feedback.
All reactions