Home > Guides > Programming > .NET and Windows Programming

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

New Memory Management Functions in .NET 2.0

Last updated May 4, 2006.

Several new memory management facilities have been added to .NET 2.0. These facilities give you slightly more control over how the garbage collector works, and the ability to check for sufficient memory before continuing in a program.

New Garbage Collector Functionality

The runtime garbage collector is implemented in the System.GC class. Normally, your programs don’t have to do much if anything with this class. Most programs, if they access the GC class at all, only call GC.SuppressFinalize to prevent the garbage collector from calling Object.Finalize on a disposed object that doesn’t need it. See the .NET SDK topic Implementing a Dispose Method for more information.

The .NET 1.1 version of the GC class allowed programs to force garbage collection if necessary, but it didn’t give programs the ability to determine if garbage collection was needed. In 2.0, the GC.CollectionCount method returns the number of collections that have occurred for a given generation. If your application is forcing collections, it can save the value returned by GC.Collect and compare it later with the value returned by GC.CollectionCount. If the numbers are the same, then no collection has occurred since the last time your code called Collect, meaning that you might want to force a collection.

Note that it is not recommended that you force garbage collections unless you know exactly what you’re doing. Unnecessary collections do nothing but slow your program.

The other new functionality exposed by the GC class is the ability to inform the garbage collector of external--outside of the framework--memory allocations. Doing so can help the garbage collector run more efficiently because it has a better idea of how much of the system’s total memory is available.

In determining when to schedule garbage collections, the .NET runtime takes into account the amount of managed memory allocated. That’s all it can know about. However, if you have a small object that allocates a large amount of unmanaged memory, the garbage collector will underestimate the urgency of scheduling a collection. By giving you a way to notify the garbage collector of memory pressures, you help the system run more efficiently.

To notify the garbage collector of a large unmanaged memory allocation, call GC.AddMemoryPressure, passing to the function the incremental number of unmanaged bytes allocated. You should do this for any large allocation, or for small allocations if there will be many of them.

It’s very important that you make a corresponding call to GC.RemoveMemoryPressure whenever you deallocate memory for which you called GC.AddMemoryPressure. Failure to do so will almost certainly lead to decreased performance in applications that run for long periods of time.

Discussions

Copies of the array?
Posted Dec 23, 2008 03:40 PM by luige21
1 Replies
Hi
Posted Dec 5, 2008 05:10 AM by ajay2000bhushan
2 Replies
You have no clue.
Posted Jun 10, 2008 03:28 PM by theinternetmaster
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jim Mischel"Highly unlikely" does not mean "impossible"
By Jim MischelJuly 18, 2009 No Comments

One of my programs crashed the other day in a very unexpected place.  A call to System.Threading.ConcurrentQueue.TryDequeue (from the Parallel Extensions to .NET) resulted in an OverflowException being thrown.  Investigation revealed a pretty serious bug in the System.Random constructor.

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part II
By John TraenkenschuhMay 24, 2009 No Comments

In the last blog in this series, Traenk relates his first experiences with computers and with coding.  But now, some years have passed. . .

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part I
By John TraenkenschuhMay 24, 2009 No Comments

Traenk relates his past experience with Operating Systems that goes back 25 years, ok, more than that but he ain't tellin'

See More Blogs

Informit Network