|
|
|||
| H o m e | |||
|
|
M e m o r y M a n a g e m e n t : D e r e f e r e n c i n g O b j e c t s By Michael McIntyre
The .NET Runtime’s Garbage Collector (GC) feature keeps track of the memory that each object is allocated, and automatically releases that memory when the running program no longer references the object.
It is your responsibility as a programmer to ensure your code releases references to an object as soon as your program is finished with an object.
If your code leaves at least one reference to an object hanging around after your program is finished with an object, the GC will not destroy the object. As a result your program will use more memory than it should. This can lead to performance issues and application failure.
What is an Object Reference?
An object reference is the address of an object in your application’s memory that is held in a reference type variable declared in your code.
An object reference is created when you declare a reference type variable and assign an object to it e.g.
' Declare a variable of type RSACryptoServiceProvider named RSA. ' Use a RSACryptoServiceProvider constructor to create a new RSACryptoServiceProvider object. ' Assign the address (reference) of the new object ' to the RSA variable. Dim RSA As New RSACryptoServiceProvider()
The RSA variable provides a way for you to communicate with the RSACryptoServiceProvider object. It is the way by which your code will reference the object to set its properties or call its methods.
NOTE: It is possible for more than one variable in your program to reference the same object.
Why Must Objects be Dereferenced?
The GC will not clear an object from memory if the object is referenced by one or more variables in a running program, even when the object is no longer needed by the running program.
If the running code contains at least one variable that references an object (an active reference to the object) the GC sees the object as active and will leave it in memory. The GC has no way other than checking for active object references in the running code to know your program is finished with an object.
It is up to you to “tell” the GC when your program is finished with an object by ensuring your code dereferences each object as soon as it is no longer needed.
How is an Object Dereferenced?
1. Automatically - An object will be dereferenced “automatically” if all variables referencing the object go out of scope during the normal course of program execution. How do you know when your variables will automatically go out of scope? Learn how scope works. Scope is very important in programming and especially so when managing objects. To learn more about scope visit the link below.
2. Forced - An object may be explicitly dereferenced by code that assigns a value of NOTHING to all active variables which reference that object. How do you know when to forcefully dereference an object through code? Check your code. If the program is finished with an object and you have determined that the variable(s) referencing the object will NOT go out of scope automatically you must use code to dereference the object. Assign the value Nothing to the variable(s) that reference the object. ' Example: Dereference an object by assigning ' a value of NOTHING to the variable(s) that reference it. ' ' In this example the code dereferences a DataSet object ' by assigning NOTHING to the myDataSet variable ' so that it no longer contains a reference to the ' DataSet object.
myDataSet = NOTHING
Note: Setting the value of a reference type variable to NOTHING does not destroy the variable. It only releases its reference to the object. If a variable remains in scope it can be reused with a different object of the same type.
|
||
|
Copyright © 2001-2003 aZ Software Developers. All rights reserved. |
|||