-1

As far as I know single .net application can allocate a lot from available memory. It will be released by GC at some point. I never have to care much about details. It just works.

But what is going to happen if native application is started while most/all memory is used by .net application? Will GC respects this and free memory before? Or will Windows "take care" and will swap memory of .net appplication into swap-file?


I have a single PC with slow HDD, where my WPF application (MVVM, bitmaps, database, pretty memory-intensive) occupy 200-2000 Mb (up to 80%) of RAM and I get reports what PC become slow when running Office, antivirus, etc.

In e.g. Photoshop there is a setting to limit amount of used RAM. Now I am thinking whenever such thing make sense in my WPF application.

Is uncontrollable GC memory allocation a problem or not? Should I limit amount of used by my application memory?

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • 1
    `Will GC respects this and free memory before?` The GC will _respond_ to a low memory situation. It obviously can't free memory **before** - because it doesn't know about the low memory situation until it occurs. – mjwills Feb 20 '18 at 10:47
  • @mjwills, I was hoping for something. Some magic from Microsoft (since they made both: Windows and .Net). Talking about multiple .net applications - they *probably* (are they?) share same instance of GC, which can be smart enough to collect garbage for application A before allocating something for B when on low memory? – Sinatr Feb 20 '18 at 11:02
  • @mjwills, because of slow HDD paging is problem: HDD is stalled and normal IO activity of application (e.g. logging) is blocked. It seems I want to avoid it (e.g. by limiting available RAM). I am surprised to realize that it needs to be. – Sinatr Feb 20 '18 at 11:21
  • This feels like a XY Problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem . You are asking about GC when you _perhaps_ should be asking "why is my program using 2GB of RAM"? – mjwills Feb 20 '18 at 12:24
  • @mjwills, I was sure it's a normal behavior. The application is big and depending on what user is doing the 200-2000 MB deviation doesn't seems abnormal to me. I think the answer what this assumption is wrong and how much is OK will still fit to my question. Sometimes we ask wrong questions, the answer pointing why question is wrong is still the answer. – Sinatr Feb 20 '18 at 12:42

1 Answers1

1

The problem described does not look related to .NET memory management/GC, assuming that application just holds the operation data (in use) in the memory. .NET app is not different from any other user app so the OS will treat them in the same way: move infrequently used memory blocks to the swap file.

If the application occupies 80% of RAM and works with memory intensively, competing with other applications, the whole process will generate a lot of page faults causing large traffic between swap file and memory. This leads to severe performance degradation, especially in case of slow HDD.

The .NET part in this game is just to clean the application memory from time to time from the data no longer in use. If there are just big amount of data required for the app to run and just adding more RAM is not an option, then application redesign (which limits the amount of loaded data somehow) is a considerable approach.

Alex Netkachov
  • 13,172
  • 6
  • 53
  • 85
  • In other words you suggest me to implement that "maximum allowed RAM" setting? – Sinatr Feb 20 '18 at 10:59
  • Not at all. I'm not suggesting here, I hope to provide you with some info to help you make your decision. – Alex Netkachov Feb 20 '18 at 11:05
  • When I say *memory intensive* I mean a lot of allocations: underlying USB communication, events, bitmaps, data templates - all of those generate many `new` requests. The application could work normally with just 200 MB, but because of no control from my side the GC can eat up to 2000 MB. I could just insert a timer which perform `GC.Collect()` every second or so, then I guess memory will never rise. Worth a test? Or what kind of info do you want to know? – Sinatr Feb 20 '18 at 11:10
  • It definitely will help identify memory usage pattern. If it helps, then the problem might be that GC thresholds actually are not hit, which can happen with bitmaps, for example - they are tiny managed wrappers and huge unmanaged memory blocks. – Alex Netkachov Feb 20 '18 at 11:22
  • Actually, I spot something "suspecious". Your hint with bitmaps was very helpful. It lack `GC.AddMemoryPressure` calls from certain changeset, while they [were proven necessary](https://stackoverflow.com/q/25450979/1997232). – Sinatr Feb 20 '18 at 11:44