I have a WPF desktop application that loads a very large serialised object from file and performs some data processing on it. Once finished, the user can discard this data and load another file for processing, and so on. As a precaution, due to the size of the objects being created, I compact the large object heap after each "run" using:-
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();
For interest, I'm displaying memory usage on the app's window using GC.GetTotalMemory(false)
. After loading and processing the first file, memory usage jumps to 2.2Gb (yes the data is that big). If I then process the same file again, memory usage jumps to 4.4Gb, suggesting the previous data isn't being freed/disposed (although I am 99.99% certain that it should be).
Anyway, here's the strange thing. If I run the app using the VS2019 performance profiler in "memory usage" mode, I can see its chart ramp up to 2.2Gb on the first processing run, then to 4.4Gb on the second. But, if I then take a snapshot, memory usage at that moment drops to 2.2Gb on both the profiler chart and the value displayed in my app.
If I don't use the performance profiler (or do but not take a snapshot), the memory usage displayed in my app remains at 4.4Gb and never drops. Why would the profiler (or more specifically, taking a snapshot) be having an effect on memory usage?