DividedByZero
DividedByZero

Reputation: 429

C# - Call GC.Collect() manually (Disable background GC)

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

Answers (2)

lordcheeto
lordcheeto

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.

Full MSDN Documentation

Upvotes: 6

Johannes
Johannes

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

Related Questions