Game Development Reference
In-Depth Information
// Recreate the object
instance = new MyClass();
}
Explicit Control
The automated memory management of the .NET platform is very efficient and
optimized, but certain situations may require direct control over the garbage col-
lector to improve performance. Every application has a fairly unique allocation
profile in terms of memory requirements and the intervals or patterns in which
memory is managed, presenting some opportunities for performance tuning using
explicit control over the garbage collector.
The System.GC type provides functionality for your application to interface with
the garbage collector directly. There are a variety of things that can be done with
this class, such as the ability to induce a collection, wait for the finalization thread
to complete a pass, and query the garbage collector for some useful statistics, like
the maximum number of generations.
Garbage collection is a fairly multi-threaded process, so performance optimiza-
tions typically come in the form of timing. For example, if you run a process that
allocates a substantial number of objects, it is fair to say that you should explicitly
invoke the garbage collector before returning control back to the user. If the user
is already waiting for a long-running process to complete, where is the harm in
running a collection when the process completes, so that the application does not
hit a random pause when the GC finally fires up to release memory that was used?
Your application knows more about how it works than the garbage collector does,
so some strategically placed calls can offer some performance boosts. It is impor-
tant to keep in mind that collections also degrade performance, so use them spar-
ingly and wisely.
One of the most important operations you can do is invoke a collection. There are
two flavors of the GC.Collect method: one version that takes in an integer specifying
which generation to collect, and another version that invokes a collection across all
generations and is the equivalent of calling GC.Collect(GC.MaxGeneration) .
GC.Collect(Int32 Generation)
GC.Collect()
Note
Normally, you should avoid calling the collection methods explicitly, but as discussed earlier, there
is a definite need for direct control in certain situations.
 
Search Nedrilad ::




Custom Search