30.06.2013 Views

Under the Hood of .NET Memory Management - Simple Talk

Under the Hood of .NET Memory Management - Simple Talk

Under the Hood of .NET Memory Management - Simple Talk

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3: A Little More Detail<br />

An obvious use <strong>of</strong> LatencyMode is to change it for a short period during execution <strong>of</strong><br />

critical code that needs maximum UI or batch processing performance, and <strong>the</strong>n change<br />

it back on completion.<br />

using System.Runtime;<br />

…<br />

// Store current latency mode<br />

GCLatencyMode mode = GCSettings.LatencyMode;<br />

// Set low latency mode<br />

GCSettings.LatencyMode = GCLatencyMode.LowLatency;<br />

try<br />

{<br />

// Do some critical animation work<br />

}<br />

finally<br />

{<br />

// Restore latency mode<br />

GCSettings.LatencyMode = mode;<br />

}<br />

Listing 3.4: Using GC LatencyMode.<br />

GC Notifications<br />

.<strong>NET</strong> Framework 3.5.1 added notification features which allow you to determine when<br />

a full GC is about to take place, allowing you to take action if necessary. A server<br />

application, for example, could redirect requests to o<strong>the</strong>r servers. Basically, with <strong>NET</strong><br />

Framework 3.5.1, you can do <strong>the</strong> following things:<br />

• register for full GC notifications<br />

• determine when a full GC is approaching<br />

• determine when a full GC has finished<br />

• unregister for full GC notifications.<br />

70

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!