Reputation: 429
I want to compare performance between my garbage collection at C++ and C#'s. It is possible to call GC.Collect()
manually? (I want to run it at specific moments.)
Upvotes: 0
Views: 3100
Reputation: 1091
There is no way to disable the garbage collection in C# outside of using unsafe code. You can force the garbage collector to run by calling System.GC.Collect()
or one of its various overloads.
Upvotes: 6
Reputation: 6717
You can trigger the disposal of one specific class.
A class that has IDisposeable
implemented and runs in using
context will be disposed when the using
context exits.
MyLogger logger = new MyLogger("time.dat");
logger.log("before using");
using (MyDisposableClass myClass = new MyDisposableClass())
{
logger.log("before execution");
myClass.doingSomething()
logger.log("after execution");
logger.log("before dispose");
}
logger.log("after dispose");
This is factually wrong - see comment.
Upvotes: -2