none
VBScript array ReDim question about memory used RRS feed

  • Question

  • I have read that when you ReDim an array there is a copy made of the original array and it is actually the copy that used used from that point forward. So My question is, if that is true, is the original array still in memory or is it deleted? The reason for my question is that I wonder if I have some code that creates some arrays and then ReDims reach array can that lead to running out of memory?

    Below is an example of a situation, maybe this will illustrate for some people what I would be doing.

    Dim myArray()
    Dim someValue
    someValue = getSomeValueSomeWay() 'THIS IS DYNAMICALLY GETTING A NUMBER, WE WILL SAY IT IS 145
    ReDim myArray(someValue) 'DID WE JUST MAKE A COPY OF THE ARRAY WHICH WAS CREATED 3 LINES ABOVE?

    Wednesday, June 3, 2015 12:09 PM

Answers

All replies

  • It might be helpful to read the documentation for the VBScript ReDim statement.

    https://msdn.microsoft.com/en-us/library/c850dt17.aspx

    (Note the Preserve keyword.)


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 3, 2015 2:09 PM
    Moderator
  • ReDim is terribly inefficient and is only useful for small arrays and simple applications.

    If you want  robust solution then use PowerShell.


    \_(ツ)_/

    Wednesday, June 3, 2015 3:31 PM
  • @Bill, Thanks, I did review this page already. However it is very brief and does not answer the question I was asking. that is why I came into this forum to find out about the possibility of the original array being held in memory after the ReDim (even if access to it is lost). if you can please re-read my question and see if you can answer what I am asking.

    • Edited by php_guy Wednesday, June 3, 2015 6:37 PM
    Wednesday, June 3, 2015 6:27 PM
  • @jrv, ReDim seems to work fine. But I would like to see more about what you are speaking of. Can you just show a quick example to prove that this could be used instead? Please look at my example code to see how I create an array with no set size, then define the size after, and then if you could just show a value added and then resized and another value added. If that is possible then maybe your idea is better. BUT...... I still would need to know if when using powershell to create an unsized array and then setting the size would have any memory which would be still in place and not used, which was what my original question was about and I still did not get an answer on.
    • Edited by php_guy Wednesday, June 3, 2015 6:36 PM
    Wednesday, June 3, 2015 6:34 PM
  • It would be more helpful if you could provide an example use case that illustrates what's provoking the question.

    As jrv mentioned, ReDim Preserve is known to be inefficient. It is generally not a good idea to resize an array but rather use some other data structure or generate the array only once.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 3, 2015 6:47 PM
    Moderator
  • Powershell array

    $a=@()
    $a+=1
    $a+='hello'
    $a+=99
    $b=$a.Clone()
    $a.Clear()

    Very easy an very efficient.

    If you spend some time learning the basics of VBScript you will learn how memory is used.  We should not have to explain it to you.  You can also search for answers.

    https://www.google.com/#newwindow=1&q=vbscript+memory+management

    Memory management is done automatically.  You cannot do much to change that.


    \_(ツ)_/

    Wednesday, June 3, 2015 6:48 PM
  • @Bill, I did provide an example. Please re-read my original question. I create an array which has no size, and then dynamically ReDim its size. My question had nothing to do with the ethics of creating arrays, but rather with a specific type of creating and use and if it may create a memory issue with the possible fact that ReDim creates a copy of the array. My question is, what happens to that very first array? (the one that ReDim created a copy of). So if I create an unsized array and ReDim it 1000 times, are there 999 still held in memory which I do not have access to?
    Thursday, June 4, 2015 10:32 AM
  • Thanks JRV but that is what these forums are for, asking questions. I simply am asking about something specific that is not explained in the ReDim documentation. if you do not know the answer that is fine and possibly it is best not to make comments in here. Your example is great but does not answer my original question. And I would only ask you if this example creates the same problem as my original question with the possible fact that the very first array created, which has no size, could possibly remain in memory. Again thank you for trying to help, and trust that I have read all the documentation I could find on Redim, but no where does it specifically answer my question. So that is why I reached out to the professionals in this forum to ask if they may know. if you happen to know the answer to my original question please enlighten us.
    Thursday, June 4, 2015 10:37 AM
  • Apparently neither Bill, I or anyone else here wants to write a page and a half of description of how memory works in a scripted system.  It works very much the same in all scripted systems and in all programs under al programming languages.  It is something all techs should have learned in school.

    http://www.tldp.org/HOWTO/Unix-and-Internet-Fundamentals-HOWTO/memory-management.html

    The answer is not simple.  Memory management is in play.  Process memory restrictions are in force. 

    The answer is still that Rediming an array is horribly inefficient so it depends.  If your script works then don't worry about it.  If it is too slow then don't use arrays.  A loop of 1000 is not likely too run out of memory.  If you run low on memory you will get an error.  The error will tell you why.  This is all basic computing 101.

    If you need more information then I recommend searching foor memory management and scripting/vbscript and read the blog post and discussions.

    My take is don't use arrays for anything other than small datasets.  Use sortedlist or a database.  They are much faster and don't slow the program down or consume memory.


    \_(ツ)_/

    Thursday, June 4, 2015 11:24 AM
  • I agree with jrv here. How it works "under the hood" is, from a practical sense, irrelevant because you can't change it anyway. This is why we're asking what's provoking the question.


    -- Bill Stewart [Bill_Stewart]

    Thursday, June 4, 2015 12:23 PM
    Moderator
  • I am not asking how it works. I am simply asking if ReDim actually makes a copy of the original array. And if it does, will the original array remain in memory. This is all being asked because I saw in another forum this topic was discussed and said to be true. So I figured to come to the best place to ask this question (The Microsoft forum).

    My question should result in a yes or no answer. Not a detailed essay or even a paragraph. 

    Does ReDim make a copy of the original array, Yes or No?

    If yes, then does the original array remain in memory, Yes or No?

    Seems I asked something that no one really knows the answer to

    Thursday, June 4, 2015 1:33 PM
  • Why do you need to know? What difference does it make?

    -- Bill Stewart [Bill_Stewart]

    Thursday, June 4, 2015 1:54 PM
    Moderator
  • You already know that ReDim makes a new copy.

    \_(ツ)_/

    Thursday, June 4, 2015 2:04 PM
  • I need to know so I can answer my question. And the difference it would make is that ReDim would be making a copy of the original array rather than assigning a new block of memory with more allocated to accommodate the increase in size, or less allocated to accommodate the decrease in size, and then delete the original array or let the garbage collector reallocate it. 
    I was really expecting to get a simple yes or no answer from this forum but am surprised to only find negative users speculating on ethics and proper code creation. I think if you both feel so strongly against ReDim you should speak with Microsoft about why they would be so stupid to create such a thing. If they created it, why would it be wrong to use it? I do see how it could be inappropriate to use in certain cases but those cases do not apply to this question or to the example I am referencing.
    Thursday, June 4, 2015 2:06 PM
  • VBScript's memory management is an implementation detail.

    This means that you have no control over how the VBScript interpreter allocates and deallocates memory dynamically, or whether it overwrites memory it already used, or how it does this.

    In other words: Your question has no practical relevance, unless you can demonstrate otherwise.

    This is why we are asking: What's provoking the question?


    -- Bill Stewart [Bill_Stewart]

    Thursday, June 4, 2015 2:09 PM
    Moderator
  • I have read that when you ReDim an array there is a copy made of the original array and it is actually the copy that used used from that point forward.

    So My question is, if that is true, is the original array still in memory or is it deleted?
    The reason for my question is that I wonder if I have some code that creates some arrays and then ReDims reach array can that lead to running out of memory?

    You stated that a copy is made.
    You asked if that was true: YES
    You also asked if you can run out of memory: YES

    The I made point above is that you should not use arrays like this.  I noted performance issues that will happen.  The chances are you will give up on the process before you run out of memory.  The references posted were to help you too understand the answer.

    Bill is right.  Without knowing why you are asking the uesiton it is impossible to give a definitive answer.


    \_(ツ)_/

    Thursday, June 4, 2015 2:19 PM
  • Thanks, those two answers finally answered the original question. I wonder why Microsoft invented ReDim if it should not be used. I doubt the example I gave would ever run out of memory and that had nothing to do with my question. I was simply clarifying something that is not addressed in the documentation for ReDim. 
    Thank you again for your answer, it was what I was originally looking for.
    Thursday, June 4, 2015 2:23 PM
  • You originally stated that a copy was being made.

    Redim has been part of the Basic language since before Microsoft.  I believe it was part of the original Dartmouth Basic.

    If you study computer engineering or software engineering this would be something you would know.  The issues with array performance have been around since the beginning.  Good programmers have always avoided arrays for all but static operations.

    Memory an agent is an issue in all languages.  In VBScript you have no control over this.  Just be smart about how you use strings and arrays.

    By reading the documentation on VBScript you will learn how it uses variables and how the exist and are removed.

    In the end you should not be using VBScript.  If you are going to learn to script you should be learning PowerShell.


    \_(ツ)_/

    Thursday, June 4, 2015 2:31 PM
  • I am very shocked that you have always known everything about computer science and never started at the beginning where you needed to learn. Congrats!
    Thursday, June 4, 2015 2:53 PM
  • I am saying that you need to study first so you can ask better questions. 

    Learning how to ask the correct question is more valuable than all of the answers.


    \_(ツ)_/


    Thursday, June 4, 2015 2:58 PM
  • I stated in my original post I had researched and did not find the answer. I think you have stereotyped me improperly due to your past experiences with others. You should be ashamed of your behavior as you are supposed to be a key figure of help and education in this forum.
    The internet is a big place and finding the answers can sometimes be difficult. That is why we have forums. I agree some people do not try very hard to look and use forums as a crutch. However I did look, and I had to resort to a forum to ask if anyone else knew the answer. Looks like I chose the wrong forum
    Thursday, June 4, 2015 3:07 PM
  • Maybe you need to learn how to search or use a different search engine.  I find answers in almost no time.

    One search: https://www.google.com/?gws_rd=ssl#newwindow=1&q=redim+preserve+memory+usage

    hundreds of answers.


    \_(ツ)_/

    Thursday, June 4, 2015 3:12 PM
  • Day late and dollar short. I did not find the answer to my original question when researching. I was not googling "preserve memory usage". if you read my original post you will see what I was looking for. And the answer to my question was difficult to find. Also remember that you are the smartest person in the world so finding an answer is easier for you than someone who is just learning. I think you need to find a new profession or learn how to teach.

    We are done here.

    • Edited by php_guy Thursday, June 4, 2015 3:18 PM
    Thursday, June 4, 2015 3:18 PM
  • You still haven't explained what's provoking the question.

    VBScript's memory management is an implementation detail that is handled automatically by the script interpreter.

    How it manages the memory is therefore an irrelevant question, unless you can demonstrate that you have a good reason for asking. A generic theoretical question is useless if you can't do anything with the information.

    If you don't know what you're going to do with the answer to a question, then there's not much point in making others work hard to answer it

    Also:

    When you are looking for more information, it helps to say what you need the information for

    (A vague "I just want to know" doesn't count, because you can't do anything about it, anyway.)


    -- Bill Stewart [Bill_Stewart]

    Thursday, June 4, 2015 3:23 PM
    Moderator
  • Day late and dollar short. I did not find the answer to my original question when researching. I was not googling "preserve memory usage". if you read my original post you will see what I was looking for. And the answer to my question was difficult to find. Also remember that you are the smartest person in the world so finding an answer is easier for you than someone who is just learning. I think you need to find a new profession or learn how to teach.

    We are done here.

    I searched using the exact text of your post and found hundreds oof answers;

    https://www.google.com/?gws_rd=ssl#newwindow=1&q=ReDim+question+about+memory+used

    You never did any research.  I am smart enough to not believe that you did. 

    Asking a good question here or in Bing or Google is not difficult fro anyone but the lazy.


    \_(ツ)_/

    Thursday, June 4, 2015 3:32 PM