Adding and Removing Memory Pressure Application Sample
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
This sample demonstrates the interoperation of managed garbage collection with unmanaged code.
For information about using the samples, see the following topics:
To build the sample using the Command Prompt
Open the Command Prompt window and navigate to one of the language-specific subdirectories under the GCDemo directory. For information about required settings and the SDK Command Prompt, see How to: Set Sample Settings.
Type msbuild GCDemoCS.sln or msbuild GCDemoVB.sln at the command line, depending on your choice of programming language.
To build the sample using Visual Studio
Open Windows Explorer and navigate to one of the language-specific subdirectories under the GCDemo directory.
Double-click the GCDemoCS.sln or GCDemoVB.sln file, depending on your choice of programming language, to open the file in Visual Studio.
In the Build menu, select Build Solution.
To run the sample
Open the Command Prompt window and navigate to the directory that contains the new executable.
These samples require Windows headers which are available to your project if you build using Microsoft Visual Studio 2005, the Visual Studio 2005 Command Prompt, or if you have the Platform SDK installed and specified in an include path.
This sample demonstrates the new AddMemoryPressure and RemoveMemoryPressure methods of the GC class. You can use these methods to change the pattern of the garbage collection strategy for generation-zero collections. You should use them to give the garbage collector (GC) a hint, whenever the number of unmanaged resources being allocated is significantly higher than the managed footprint for the same resources. Without this hint, the GC is unaware of the unmanaged memory, and can therefore select a suboptimal collection strategy.
Simply run the sample, and observe the number of successful allocations with and without the memory pressure (you will notice that the allocations are more successful when you add pressure appropriately). Also, observe the effect the added pressure has on the number of generation-zero collections performed, indicating the fact that the GC is performing several more collections when pressure is added.
Always remember to pair any added pressure with an appropriate removal of the same amount of pressure.