|
-
May 1st, 2006, 12:31 AM
#1
Thread Starter
Addicted Member
[2005] Memory Usage
Anyone have any pointers or tips for reducing memory usage of an application written in VB.NET 2.0?
I understand that try/catch statements slow down an app. And that dimensioning variables takes memory.
I'm looking for like "good practices"
It is also my understanding that setting a variable equal to nothing is ill advised and may be counter-productive with the garbage collector. Any truth to that?
Dreaming men are haunted men.
-
May 1st, 2006, 12:48 AM
#2
Re: [2005] Memory Usage
You should call its Dispose() method instead, if it has one.
Also, what do you mean by "dimensioning" variables?
http://msdn.microsoft.com/msdnmag/is...ryOptimization
Last edited by RobDog888; May 1st, 2006 at 12:58 AM.
Reason: Merged Posts :)
-
May 1st, 2006, 12:59 AM
#3
Thread Starter
Addicted Member
Re: [2005] Memory Usage
thanks for the article. and i mean the Dimensioning of a variable... like dim MyString as string = "lala"
Dreaming men are haunted men.
-
May 1st, 2006, 01:06 AM
#4
Re:
Ah, "declaring" rather The CLR handles all that so I wouldn't worry, unless usage is an issue in your app?
-
May 1st, 2006, 01:14 AM
#5
Re: [2005] Memory Usage
"Dim" is short for "dimension" but the general term for that is "declaring" a variable. A variable is just a small area of memory on the stack. There's no need to go declaring variables willy-nilly if you don't need them but it's generally no big deal. For instance, you may choose to declare a single variable outside a loop:
VB Code:
Dim myString As String
Do Until myStreamReader.Peek() = -1
myString = myStreamReader.ReadLine()
Loop
or repeatedly declare a variable inside a loop:
VB Code:
Do Until myStreamReader.Peek() = -1
Dim myString As String = myStreamReader.ReadLine()
Loop
As far as memory usage goes neither is better than the other because there is only ever one String variable in scope at a time and the number of String objects is the same regardless. The general rules will be:
Always call the Dispose method of all objects that support it when you are finished using them.
Always implement IDisposable in your own classes if they hold unmanaged resources or have member variables that have a Dispose method.
Don't declare variables or create objects unnecessarily unless you have a valid reason for doing so, like if it streamlines your code considerably.
Always set variables to Nothing if you have finished with them and they do not, or may not, lose scope for some time. If they do lose scope very soon then setting them to Nothing is of no benefit.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|