CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2006
    Posts
    83

    Question Freeing memory of array out of a Subroutine

    Hi,

    I have a global variable that I want to "erase" from within a subroutine to free memory space. How can I do that?
    Code:
    'this is the main part of the program
    ...
    DIM A(1000) as Double
    ...
    
    FillA(100)
    ...
    
    ClearAndFree()
    FillA(20)
    ...
    
    Private Sub ClearAndFree()
      'here I want to free the memory
      'but the array must remain a valid variable for future use
    
      'something like:
      'clear A
      'gc.collect()
    End Sub
    
    Private Sub FillA(ByVal n As Integer)
      'fills the array A() with numbers up to "n"
      For i = 0 To n
        A(i)=i/3
      Next
    End Sub
    Any idea how ClearAndFree() must look like???

    Thanks for your help!!!

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Freeing memory of array out of a Subroutine

    What are you trying to do? You could create a new instance, and erase that.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Freeing memory of array out of a Subroutine

    David is right. You need to provide more information on WHAT you are trying to do [not HOW].

    Also calling GC.Collect() (especially without a GC.WaitForPEndingFinalizers()) is a "good" idea about the same percentage of time as firing a .44 Magnum inside an airplane while at altitudes over 30,000 feet.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Aug 2006
    Posts
    83

    Question Re: Freeing memory of array out of a Subroutine

    Thanks for your replies so far. I hope I can provide the information to explain what I try to achieve:

    There are several subs that write into this array A(). Therefore I defined it in the main part of my program so that it is globally accessible.

    After another subroutine has worked with the data it is no longer needed. Since it is a large array I want to free the memory again. That's my goal.

    Although I don't need the data anymore I still need the definition of this array so that the subs that write into A() have a place to store their information. That's what I tried to show with my example:
    first write 100 numbers
    free the memory
    write again into the array (this time 20 numbers, but it could also be up to 1000)

    I hope that makes it clear...

    What was your comment about GC.Collect() supposed to mean? Sorry I'm a noob and probably using the wrong tools...

    Thanks!

  5. #5
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Freeing memory of array out of a Subroutine

    You should just set each element of the array to Nothing and allow the .NET garbage collector to worry about when to reclaim memory.

    That said, if your array contains structures, as Double is, then doing so is pointless. Each element contains a value and whether that value is zero or something else it still takes up the same amount of space.

    Only if the array contains class instance would there be any benefit to setting each element to Nothing. In that case you remove the reference to the object so the system can reclaim the memory it occupies, but only if and when the system decides it is necessary.

    I would probably suggest that you only create an array object if and when you need one. Creating an array that can hold 1000 elements just in case you might need that many is a bit pointless. If you need an array that can store 20 objects then create one of that size. When you're done with that just get rid of it, then create another one of the appropriate size when you need another one.
    Last edited by jmcilhinney; December 16th, 2007 at 09:32 PM.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  6. #6
    Join Date
    Aug 2006
    Posts
    83

    Question Re: Freeing memory of array out of a Subroutine

    Hmmm,

    my problem is that I don't know in advance how much data the subroutines that fill the variable will put in (they do some kind of search and put the "hits" into the array).

    I tried ReDim, but I get an error telling me that I can't do that on an array. Is there an other way of clearing the array prior to a "refill"?
    If you need an array that can store 20 objects then create one of that size. When you're done with that just get rid of it, then create another one of the appropriate size when you need another one.
    Would you mind giving me a piece of code that does that? That's exactly what I try to achieve and don't know how:
    a) get rid of the array
    b) create a new one (ideally with an unlimited boundary)

    Thanks!

  7. #7
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Freeing memory of array out of a Subroutine

    just use dynamic array and forget about the "Redim". if you are using .Net 2.0 or above, it is favored to use "Generics". below is a sample code that instantiate a dynamic array with an initial capacity of 1K, there's no problem though if you exceed that capacity..

    Code:
    Private Sub TheSubRoutine()
    Dim a As new System.Collections.ArrayList(1024)
    
          a.Add("value1")
          a.Add("value2")
          a.Add("value3")
    
          a.Clear()
    
    End Sub
    Busy

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

Featured