Array data size limit?

Hello guys!

I have two dimensional array with fixed size (15x100). Empty slots are filled in order with strings containing 16-17 chars. In same stage when storing values the app crashes. So far I have not been able to reproduce this in simulator. 

i’m just wondering what is the data size limit of an array? Might that be the issue? 

is there any way to debug in watch?

Thanks!

  • We can get the exception class name in a debug build pretty easily. In a release build that requires access to debug.xml and post-processing as is done with the ERA service.

    I'll file a ticket to add the exception class id to the log and some lookup mechanism on the backend for the ERA service.

  • Thank you, this would be extremely helpful!

  • Is there a better way or work around for this with an array with the shape like

    A = array<numbers (int) size 6>

    array< multiples of A >

  • sure:

    instead of:

    A[y][x], if you pack all the items in 1 array:

    B = array<Number>, where you index them:
    B[6 * y + x]

  • Sorry I don't get it. What are y and x in your example?

    To give a bit more context.

    The array has arrays of hr time in zone values of historical activities (added when an activity ends)

    The number values are:Date_Time_value,time_z1,time_z2,time_z3,time_z4,time_z5

    And I need to drop a sub array of the Date_Time_value older than 14 days.

  • What flocsy meant was that in general, a 2D array (array of arrays) can be flattened into a 1D array, as long as the size of the 2nd dimension is fixed.

    e.g. (note, this isn't the best code to implement either of these concepts, it's just for illustrative purposes)

    2D array (array of arrays):

    class Array2D {
        var _array;
    
        function initialize(var aSize as Number, var bSize as Number) {
            _array = new [aSize];
            for (var i = 0; i < aSize; i++) {
                _array[i] = new [bSize];
            }
        }
    
        function get(var aIndex as Number, var bIndex as Number) {
            return _array[aIndex][bIndex];
        }
    
        function set(var aIndex as Number, var bIndex as Number, var data) as Void {
            _array[aIndex][bIndex] = data;
        }
    }
    
    function test() {
        var array = new Array2D(5, 10); // 5 x 10 array
        array.set(2, 3, "hello world");
        //...
        var x = a.get(2, 3);
    }

    Flattened "2D array" (internally implemented as 1D array, which saves memory)

    class ArrayFlat {
        var _array;
        var _aSize;
        var _bSize;
        function initialize(var aSize as Number, var bSize as Number) {
            _aSize = aSize;
            _bSize = bSize;
            _array = new [_aSize * _bSize];
        }
    
        function get(var aIndex as Number, var bIndex as Number) {
            return _array[aIndex * _bSize + bIndex];
        }
    
        function set(var aIndex as Number, var bIndex as Number, var data) as Void {
            _array[aIndex * _bSize + bIndex]; = data;
        }
    }

  • Ahh oké flattering the array is something I know. But does this save mutch? When adding you will still duplicate the array +1 and in that instance you dubbel the memory need. Is there something that can be done about that?

    Maybe make big empty array and change an item instead of add and delete. Or does changing also result in making a new array copying the values?

    And also does unpacking the flattened array not result in more or less them same amount of memory usage?

  • Disregarding the impact of adding array elements dynamically, there is significant memory savings to be found by flattening 2D arrays, because each array has a 15 byte overhead.

    e.g.

    var arrayFlat = new [100 * 7]; // 3515 bytes [*]
    var array2D = new [100]; // 5515 bytes after calling test [*]

    function test() {
      for (var i = 0; i < array2D.size(); i++) {
        array2D[i] = new [7];
      }
    }

    [*] Based on the simulator's memory viewer. Both these numbers will hold true if you only use your arrays to store Number, Float, Boolean, and/or Null (because these types only use up 4 bytes for their values)

    Here's how to break down the memory usage in each case:

    - An array has a 15 byte overhead. The size of the array is 15 bytes plus the number of elements multiplied by the element size

    - An array element for "simple types" (Number, Float, Boolean, Null) takes 5 bytes: 1 byte for the type, 4 bytes for the value

    - An array element for all other types takes 5 bytes plus the size of the object: 1 byte for the type, plus 4 bytes for the pointer to the object, plus the size of the object itself

    - arrayFlat memory usage:

    array overhead + number of elements * element size

    = 15 + 700 * 5 = 3515

    - array2D memory usage:

    array overhead + number of elements * element size 

    = array overhead + number of elements * (5 + array overhead + number of elements * element size)

    = 15 + 100 * (5 + 15 + 7 * 5)

    = 5515

    In this example, converting a 2D array to a flattened array results in a 36% memory savings (3515 / 5515 ~= 64%).

    And also does unpacking the flattened array not result in more or less them same amount of memory usage?

    This is only useful if you don't completely unpack the flattened array but you only access elements one at a time.

    Or does changing also result in making a new array copying the values?

    No, but obviously you have the disadvantage of having to preallocate memory you may not need

  • Thank you so much for the long explanation. I'll change my code to a flattened array and hopefully solve the out of memory error for some people.