VBScript - Memory crash - How to optimize memory allocation?

Hi everyone,

I have in VBscript a code that imports data from TXT file and stores a dataTree in an array (dataTree size : 1000 branches x 1000 points/branch x 3 coordinate/point). The problem is, when I want to increase the data set to a tree of 1500 x 4000 x 3, I get a memory error :

1/ Is there a way to increase this memory limit ?
2/ If not, well I could reduce the size of my tree, because only a few of my 1500 branches actually contain 4000 points (about 70% of these branches contain less than 500 points), but this implies to allocate memory using Redim Preserve arrBranch(i)(n+1) to allow for more points and at first sight this does not seem to function. Should I persevere with Redim Preserve ?


I’m no expert on how this works, but -

A point coordinate triplet probably takes about 24 bytes of memory - maybe 8 bytes per number…?
1500 x 4000 items in your array each with 24 bytes will be 144,000,000 bytes, i.e 144,000 Kb or 144 Mb?

So, while significant, probably not out of the question. However, IIRC, with VBscript (haven’t used it in years, and dealing with arrays is one reason why) you can only redim dynamically the last branch in the multidimensional dynamic array, not the rest.

Thank you for the response. I see two possible solutions:

1/ try and use

Redim Preserve myPointsTree(iBranchToExtend)(nPointExisting +nNewPoints)

, as in a nested array the concept of “last dimension” maybe does not apply (to be tested and confirm)

2/ specify the type of my variables ? I suppose that all of the 2000x4000x3 elements of my tree are VARIANT type by default ? I don’t know how to declare something like

Public myTree(2000)(4000)(3) as Double


yes, I should probably move to Python or C# if dealing with large arrays becomes too tricky, but it needs some adaptation time I don’t really have for the moment…

The problem there is that is not the last level - as a point in Rhinoscript VBS is actually also an array… So you have a 3 dimension array - the whole tree (all the branches), each branch (all its points), each point (a fixed array of 3 numbers)…

Instead of a single multidimensional array, I would just use a nested (jagged) array - i.e. an array of arrays. That way, each array/level can be dynamically redimmed.

Tree: Array(branches)
Branch: Array(All points in branch)
Point: Array(X,Y,Z)

Thank you, this is what I meant in my comment of preceding point 1 : nested Array is exactly what I use here. So that point coordinates are accesible via:


Well thank you for the response, I should therefore go on with “ReDim Preserve…”. I actually have a dozen of such trees to handle in my little program, so I am going to stick on trying to optimise this memory space.

Last for preceding point 2/ : is it possible to specify that this tree contains ultimately Double (and no Variant) ? Maybe it could help too for memory

One other way to go about this is to dim the array once at the beginning larger than you think you will ever need (that is the tricky part), then use some sort of counter to keep track of up to what index you actually filled the array. Then at the end, Redim Preserve to that number to shrink the array. That way you only Redim twice, not every time you add a point.

As far as I know with VBS, no.

Another thing you might consider is actual Visual Basic. It’s similar enough to VBS that it shouldn’t be difficult to adapt your script to it. It’s one of the .Net languages along with C# so it provides more capability handling arrays, lists and dictionaries. It should be noticeably faster for what you are doing with the quantity of data you are doing it to that it would be worth the time investment.
It does involve getting a copy of Visual Studio, though, which ain’t free but it’s a pretty great programming environment.

You should be able to use a free version of Visual Studio.

VB.NET is syntactically similar, but we don’t have a library that maps close to what RhinoScript provides. The only language where we have the transitional library support is python with rhinoscriptsyntax.

Another potential quicker option is to directly use .NET classes from RhinoScript if you find a need for one.

the overhead of VBScript is huge. I remember it’s like tens of bytes per variable, especially for Variant.

1 Like

Yes ! This is exactly what I have been doing so far. But I use too much memory doing so, when the dataset I need to import is a bit big. Well now I know what to do. I will allocate dynamically the size of my branches (not 1 by 1 point but rather 500 points by 500 points, since anyway I store elsewhere the number of valid points in each branch)…

Thanks !

Yes I have been thiking to switching language for a while now… But it is time consuming, and I don’t have that much time. We are thinking in my company to go on with me doing prototypes in VBscript of what we want and then get it programmed professionally by a real dev in C# probably… To be followed