none
What memory issues do I have to consider when writing my scripts or programs? RRS feed

  • Question

  • What memory issues do I have to take into consideration when writing my programs?How do I prevent memory wasting or leaking in the program?  Thank You.
    Friday, October 28, 2011 6:40 PM

Answers

  • In .NET, leaking memory is fairly rare.  The main case you have to watch for is subscribing to events on long lived objects.  If an object subscribes to an event on a longer lived object, and forgets to unsubscribe, it can be "leaked" as the event subscription itself holds a reference.

     

    Other than that, there are few cases where you have to worry about leaked memory in .NET, as the garbage collector does a fantastic job of managing memory for you.

     

    That being said, if you're writing something where performance is critical, you'll want to watch your GC usage and memory usage in order to reduce the number of Gen2 garbage collections, as they can take a "long" time.  This is typically done by keeping objects rooted as short as possible (to prevent them from creeping up into generations unnecessarily), etc.  This is rarely needed in most day to day development, though.

     

    Is there anything in particular you're worried about here?


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, October 28, 2011 7:23 PM
    Moderator

All replies

  • In .NET, leaking memory is fairly rare.  The main case you have to watch for is subscribing to events on long lived objects.  If an object subscribes to an event on a longer lived object, and forgets to unsubscribe, it can be "leaked" as the event subscription itself holds a reference.

     

    Other than that, there are few cases where you have to worry about leaked memory in .NET, as the garbage collector does a fantastic job of managing memory for you.

     

    That being said, if you're writing something where performance is critical, you'll want to watch your GC usage and memory usage in order to reduce the number of Gen2 garbage collections, as they can take a "long" time.  This is typically done by keeping objects rooted as short as possible (to prevent them from creeping up into generations unnecessarily), etc.  This is rarely needed in most day to day development, though.

     

    Is there anything in particular you're worried about here?


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, October 28, 2011 7:23 PM
    Moderator
  • No, I did not really see a topic on this in the Help manual and wanted to learn about it. I think that in C, you have to allocate memory and remember to release if after using it or cancel a process after you are doing it so that the allocation amount will be returned to the stack and can be used by other programs. I wanted to know if the same principles applied. Where can I go to learn more about memory considerations and VB?
    Thursday, April 19, 2012 6:55 PM
  • Hey Akua,

    The same principles do not apply since memory is automatically managed for you in VB. The .NET framework has a garbage collector which will automatically clean up unused objects, see the following page for in-depth information on how this works:

    http://msdn.microsoft.com/en-us/library/0xy59wtx(v=vs.110).aspx


    • Proposed as answer by Kris444 Saturday, April 28, 2012 3:06 AM
    Friday, April 27, 2012 7:14 PM
  • Hi Akua, 

    And also, 

    The pain of allocating / releasing memory will be handled by CLR's garbage collector, read more from here about Automatic Memory Management,

    http://msdn.microsoft.com/en-us/library/f144e03t.aspx

    Hope this helps you..


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".



    • Edited by Kris444 Saturday, April 28, 2012 3:05 AM
    Saturday, April 28, 2012 3:05 AM