Dear all,
I would like to ask if any API or efficient way for making GC run more frequently, without calling GC.Collect()
What I am doing is I am checking if the objects are referenced by other objects, by using weak references. Unfortunately, it was found that as the latest made forms had not been garbage collected yet, and the Target in the weak reference still showed the object. I tried to increase the memory pressure by composing new garbages, but not successful.
I found some posts saying that it is possible to increase CLR memory pressure for acheive this Is it right If yes, how to implement
Thx a lot!
Gabriel

Increase Memory Pressure for Garbage Collection
jereroy
Out of curiosity, why do you think you need to do this Most of the material I have read on garbage collection says that calling GC.Collect is rarely a good choice. The algorithm on when memory is under pressure has not been revealed, so anyone making the call doesn't really know if they're making things better or worse. There are instances where manually calling GC.Collect is reasonable (such as when you have just created and destroyed a large number of strings), but that is vastly in the minority of cases.
But putting that aside, you're going a step further and trying to avoid GC.Collect So that you can force weak references to be collected I find myself asking why you'd want to go through these steps
Mediorco
Hi Gabriel,
Try to dispose the objects once you finished using it.You can use
using{} which will dispose automatically.
Refer http://msdn.microsoft.com/practices/Topics/perfscale/default.aspx pull=/library/en-us/dnpag/html/scalenet.asp
--Arabinda
sraddhan
GC.Collect() obiously a better choice to free up ur memory. If still you are not satisfied with the .net garbage collection algorithm then you can call object.Dispose() method. I will suggest you to read .net garbage collection architecture for better understanding.
As much I can understand you are worried about Performance. Read how clr and gc works.
TheRookie
Use GC.Collect to force the garbage collection, you can specify the generation.
Use GC.WaitForPenidingFinalizers to suspend the current thread until the thread processing the queue of finalizers had emptied the queue.
But as many said before, normally you don't need this only for you own or existing objects that have unmanaged resources that should be released.
Why do you want this Normally tracking memory-leaks and disposing unmanaged resources when it isn't need is enough and you can trust on the Garbage Collector.
You can frees all substructures pointed by a unamanged memory block with Marshal.DestroyStructure or for unmanaged COM tasks memory you use Marshal.FreeCoTaskMem. Releasing COM Objects can be done with by decrementing a reference count with Marshal.ReleaseComObject and with the Marshal.Release.
But this is only needed when you are working with COM or other unmanaged objects.
Normally you implement the IDisposable interface and release everything there. When a existing object implements the IDisposable interface you can trust this and have to call it when you don't need the object any more.