I have an application which works something like this in pseudocode:
DisplayPrettyUI()
DoABunchOfReportingWorkThatAllocatesHundredsOfMB()
GC.Collect() //Free up the memory used in generating the report
DisplayReport()
Now, I can’t get rid of the collect call outright, because if I do that the process holds on to more than a GB of memory after the report is generated, despite the app hosting only the UI components to display the report at that time. At the same time, the call to Collect seems “smelly”; and it seems like there has to be some way of handling this case without going there.
Is removing this smell possible? (I’ve heard references to things like AppDomain for solving this, but I’ve never used AppDomain before and don’t really know what semantics it has for the garbage collector…)
You could call GC.Collection with the GCColletionMode flag of Optimized so it isn’t necessarily forcing a Garbage Collection at that point….the GC can delay it if it’s not necessary.
Also look at the AddMemoryPressure, there’s some ideas here: