Memory Management and the GC Class


The GC class encapsulates the C# garbage-collection facility. The methods defined by GC are shown in Table 20-15. It defines the read-only property shown here:

 public static int MaxGeneration { get; }

MaxGeneration contains the maximum generation number available to the system. A generation number indicates the age of an allocation. Newer allocations have a lower number than older ones. Generation numbers help improve the efficiency of the garbage collector.

Table 20-15: Methods Defined by GC

Method

Meaning

public static

void AddMemoryPressure(long size)

Indicates that size number of bytes of unmanaged memory have been allocated. (Added by C# 2.0.)

public static void Collect( )

Initiates garbage collection.

public static void Collect(int maxGen)

Initiates garbage collection for memory with generation numbers of 0 through maxGen.

public static int collectionCount(int gen)

Returns the number of garbage collections that have taken place for memory having the generation number specified by gen. (Added by C# 2.0.)

public static int GetGeneration(object o)

Returns the generation number for the memory referred to by o.

public static int

GetGeneration(WeakReference o)

Returns the generation number for the memory referred to by the weak reference specified by o. A weak reference does not prevent the object from being garbage-collected.

public static long

GetTotalMemory(bool collect)

Returns the total number of bytes currently allocated. If collect is true, garbage collection occurs first.

public static void KeepAlive(object o)

Creates a reference to o, thus preventing it from being garbage collected. This reference ends when KeepAlive( ) executes.

public static void

RemoveMemoryPressure(long size)

Indicates that size number of bytes of unmanaged memory have been released. (Added by C# 2.0.)

public static void

ReRegisterForFinalize(object o)

Causes the finalizer (i.e., the destructor) for o to be called. This method undoes the effects of SuppressFinalize( ).

public static void

SuppressFinalize(object o)

Prevents the finalizer (i.e., the destructor) for o from being called.

public static void

WaitForPendingFinalizers( )

Halts execution of the invoking thread until all pending finalizers (i.e., destructors) have been called.

For most applications, you will not use any of the capabilities of GC. However, in specialized cases, they can be very useful. For example, you might want to use Collect( ) to force garbage collection to occur at a time of your choosing. Normally, garbage collection occurs at times unspecified by your program. Since garbage collection takes time, you might not want it to occur during some time-critical task, or you might want to take advantage of idle time to perform garbage collection and other types of “housekeeping” chores.

C# 2.0 added two methods that might occasionally be very important: AddMemoryPressure( ) and RemoveMemoryPressure( ). These are used to indicate that a large amount of unmanaged memory has been allocated or released by the program. They are important because the memory management system has no oversight on unmanaged memory. If a program allocates a large amount of unmanaged memory, then performance might be affected because the system has no way of knowing that free memory has been reduced. By calling AddMemoryPressure( ) when allocating large amounts of unmanaged memory, you let the system know that memory has been reduced. By calling RemoveMemoryPressure( ), you let the system know the memory has been freed. Remember: RemoveMemoryPressure( ) must be called only to indicate that memory specified by a call to AddMemoryPressure( ) has been released.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net