« Whether to order upda… | Home | Evaluate JavaScript f… »

Array size allocation in Xojo

When you allocate an array in Xojo, you may wonder how many bytes are allocated and when.

We measured a bit and it looks like allocating an empty array will just allocate the array structure, but not allocate data for the array values. Then when you append the first value, the memory is allocated for 16 values, byte size depending on byte size of elements. Then whenever the appended values fills the allocated space, the allocation is resized to make room for more values. The number of values added is doubled each time as you see in our table:

From UboundSize in BytesNumber of values
-100
01281616
163844832
4889611264
1121920240128
2403968496256
49680641008512
10081625620321024
20323264040802048
40806540881764096
8176130944163688192
163682620163275216384
327525241606552032768

When you do a redim on the array, you set the allocated size to the array to an explicit value. So unless redim has already the value of ubound, a new memory block will be allocated and data is copied. There is no optimization to just change internal ubound and keep array allocation if e.g. size is just shrinking by one or two. We made a feedback request to ask for optimization: Feedback 58755.

What to do? Whenever you fill an array and you know the final ubound in advance, you can use redim beforehand and then set values. Otherwise you use append or insert methods which dynamically allocate new memory when you hit the current allocation limit.

04 01 20 - 12:07