Section 9.9. Garbage Collection


9.9. Garbage Collection

Every object you create uses various system resources, including the memory that holds the object itself. We need a disciplined way to give resources back to the system when they are no longer needed, so as to avoid "resource leaks." The Common Language Runtime (CLR) performs automatic garbage collection to reclaim the memory occupied by objects that are no longer in use. When there are no more references to an object, the object is marked for garbage collection by the CLR. The memory for such an object can be reclaimed when the runtime executes its garbage collector, which is responsible for retrieving the memory of objects that are no longer used, so that the memory can be used for other objects. Therefore, whereas memory leaks are common in other languages like C and C++ (because memory is not automatically reclaimed in those languages), they are much less likely in Visual Basic (but some can still happen in subtle ways). Resources like memory that are allocated and reclaimed by the CLR are known as managed resources.

Other types of resource leaks can occur. For example, an application could open a file on disk to modify the file's contents. If the application does not close the file, no other application may be allowed to use the file until the application that opened the file finishes. An application that no longer needs a file should close it immediately so that other programs can access the file. Resources like files, network connections and database connections that the programmer must manage are known as unmanaged resources (because they are not managed by the CLR). Such resources are typically scarce and should be released as soon as they are no longer needed by a program.

To help prevent resource leaks for unmanaged resources, the garbage collector calls a special method named Finalize on each object before it is removed from memory. Recall from Section 9.2 that all classes inherit the methods of class Object, one of which is Finalize. You will learn more about class Object in Chapter 10, Object-Oriented Programming: Inheritance. The Finalize method is called by the garbage collector to perform termination housekeeping on an object (such as releasing unmanaged resources used by the object) just before the garbage collector reclaims the object's memory. Method Finalize does not take parameters and does not return a value. In Section 9.10, we demonstrate a situation in which method Finalize is called by the garbage collector.

Unfortunately, Finalize is not ideal for releasing unmanaged resources because the garbage collector is unpredictableit is not guaranteed to execute at a specified time. In some cases the garbage collector may never execute before a program terminates. Thus, it is unclear if, or when, method Finalize will be called. Unmanaged resources might not be released for a long time, which would prevent these scarce resources from being used by other programs. For this reason, most programmers should avoid method Finalize.

In Chapter 11, we discuss interfaces. As will be explained, an interface describes methods that a class must implement. The FCL provides interface IDisposable, which contains a method named Dispose that a client can invoke to immediately release the resources used by an object. When a program creates an object of a class that implements interface IDisposable, the client should call that object's Dispose method as soon as the client is done using the object. This ensures that the resources used by the object are released promptly so they can be reused. The following URL links to a Microsoft Developer Network (MSDN) TV interview describing IDisposable:

msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20030501CLRBA/manifest.xml

The following URL provides detailed information on memory management in .NET:

www.gotdotnet.com/team/libraries/whitepapers/resourcemanagement/resourcemanagement.aspx

Software Engineering Observation 9.6

A class that uses unmanaged resources, such as files on disk, should provide a method that clients of the class can call explicitly to release the resources. Many Visual Basic library classes provide Close or Dispose methods for this purpose.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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