Sorry for running into the "why to collect?" again. In a C# process, it is generally discouraged to collect the memory manually in order to free RAM. I overall agree.
To do it you would need to:
- Run
GC.Collect()
for small objects (generations 0,1,2) - Compact the Large Object Heap (LOH)
Several reasons are invoked as to why this is useless (and possibly counter efficient). I overall agree but I can't help to wonder if this is dangerous.
If we don't do it, it happens that (at least with server GC and .NET 4.5), the system may dedicate several GBs of physical memory to your process that are not really used by living objects. Especially the LOH might never be compacted and use a lot of RAM that is actually free from the C# memory manager point of view. I experience this everyday with processes having 20 GB of physical memory dedicated by the system, used during a peak of RAM intensive computations hours ago, but only a small part is still in use.
As far as I can see, there is still no clear performance problem on the machines. It is usually argued that this is never a problem and I (almost) agree with it. The main point in the argumentation is that the "free" RAM in the LOH is not accessed by the process and thus moved to disk by the system (Windows) at least if the RAM runs low. This answer explains it very well: When is memory, allocated by .NET process, released back to Windows
From the point of view of the other processes... there is still some "worry".
The first point I see is that if another process has an emergency need for RAM, it needs time for the system do transfer the unused memory to disk. A preventive compaction could have prevented it.
But there is another point that is more worrying. Can the system really move the unused RAM to disk? What if the LOH is fragmented with 99% free space being interleaved with 1% used space. I guess the system works with large segments so that almost no segment would actually be "0% used". Maybe it's more complicated or maybe it's false. What do you know about it?
I'm interested in people having some experience with this. Have you observed cases when the theory "you don't need to collect" goes wrong and for some reason it has proven to be heathy to do it? (and you know why).