Results 1 to 5 of 5

Thread: [2005] Memory Usage

  1. #1

    Thread Starter
    Addicted Member dim_kevin_as_human's Avatar
    Join Date
    Oct 2005
    Location
    Wisconsin
    Posts
    183

    [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.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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 :)

  3. #3

    Thread Starter
    Addicted Member dim_kevin_as_human's Avatar
    Join Date
    Oct 2005
    Location
    Wisconsin
    Posts
    183

    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.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re:

    Ah, "declaring" rather The CLR handles all that so I wouldn't worry, unless usage is an issue in your app?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim myString As String
    2.  
    3. Do Until myStreamReader.Peek() = -1
    4.     myString = myStreamReader.ReadLine()
    5. Loop
    or repeatedly declare a variable inside a loop:
    VB Code:
    1. Do Until myStreamReader.Peek() = -1
    2.     Dim myString As String = myStreamReader.ReadLine()
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width