dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.91k stars 4.63k forks source link

Low memory handling? #77618

Open mogmios opened 7 years ago

mogmios commented 7 years ago

In .NET or C# is there any standard cross-platform way to receive a low memory notice before actually running out of memory? With Xamarin you can use DidReceiveMemoryWarning and there seems to be a couple methods specific to Windows that appear to throw mystery errors when I try to use them.

I thought there might be some sort of event I could listen for but I'm not finding one. Simply marking fields with some sort of LowMemoryAction attribute seemed a possibility also but didn't find anything like that either.

For example, I have an object that computes an expensive operation for a FileSystemInfo object and which will run recursively to children if it represents a directory. I cache the result of the operation to speed up repeated calls but the operation can be redone without problem if need be. I'd like to be able to clear this cache if memory gets low. Currently using MemoryCache but it's not a very good solution.

A standard way to handle this and other system events seems important as .NET appears to be moving towards being much more cross-platform friendly.

iam3yal commented 7 years ago

This is the Roslyn repo aka ".NET Compiler Platform" and what you're asking is kinda irrelevant to this repo, however, you can try this repo CoreFx or even this CoreCLR.

I don't know why Xamarin throws, well you probably refer to Mono not Xamarin but on Windows there's a low-level API called CreateMemoryResourceNotification, you can try that.

mogmios commented 7 years ago

Ok, I'll look at those places. And will look at CreateMemoryResourceNotification.

Just for completeness, it was MemoryManager AppMemoryUsageDecreased and most of the related functionality that would compile ok but then throw errors at runtime that didn't seem to make any sense. Happened when registering any of the events and with most method calls.

dotnet-issue-labeler[bot] commented 1 year ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/gc See info in area-owners.md if you want to be subscribed.

Issue Details
In .NET or C# is there any standard cross-platform way to receive a low memory notice before actually running out of memory? With Xamarin you can use DidReceiveMemoryWarning and there seems to be a couple methods specific to Windows that appear to throw mystery errors when I try to use them. I thought there might be some sort of event I could listen for but I'm not finding one. Simply marking fields with some sort of LowMemoryAction attribute seemed a possibility also but didn't find anything like that either. For example, I have an object that computes an expensive operation for a FileSystemInfo object and which will run recursively to children if it represents a directory. I cache the result of the operation to speed up repeated calls but the operation can be redone without problem if need be. I'd like to be able to clear this cache if memory gets low. Currently using MemoryCache but it's not a very good solution. A standard way to handle this and other system events seems important as .NET appears to be moving towards being much more cross-platform friendly.
Author: mogmios
Assignees: -
Labels: `question`, `area-GC-coreclr`, `untriaged`
Milestone: -
mangod9 commented 1 year ago

@cshung