Data structures SparseArray Android

Android memory requirements more stringent

android Why SparseArray instead of HashMap? ? ?

  • When expansion HashMap, HashMap space will be in 2-fold increase in regularity. If we have hundreds of thousands, millions of pieces of data, then the HashMap to store complete data will continue to be expansion, and in the process also need to continue to do the hash calculation, which will cause great our memory consumption and waste.
  • SparseArray save more memory than the HashMap, better performance under certain conditions, mainly because it avoids the automatic packing of the key (int into Integer type), which is an internal data storage performed by two arrays, a storage key, a further memory value, in order to optimize performance, it is also taken inside the data compression method to represent sparse array data, thus saving memory space
  • Overall, SparseArray to save more memory

SparseArray not suitable for large volumes of data happens because? ? ?

  • As shown in the following code, when the put operation is performed SparseArray used to System.arrayCopy, this operation in the case where a large amount of data is very time consuming.
public void put(int key, E value) {
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

        if (i >= 0) {
            mValues[i] = value;
        } else {
            i = ~i;

            if (i < mSize && mValues[i] == DELETED) {
                mKeys[i] = key;
                mValues[i] = value;
                return;
            }

            if (mGarbage && mSize >= mKeys.length) {
                gc();

                // Search again because indices may have changed.
                i = ~ContainerHelpers.binarySearch(mKeys, mSize, key);
            }

            mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
            mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
            mSize++;
        }
    }
 public static <T> T[] insert(T[] array, int currentSize, int index, T element) {
        assert currentSize <= array.length;

        if (currentSize + 1 <= array.length) {
            System.arraycopy(array, index, array, index + 1, currentSize - index);
            array[index] = element;
            return array;
        }

        @SuppressWarnings("unchecked")
        T[] newArray = ArrayUtils.newUnpaddedArray((Class<T>)array.getClass().getComponentType(),
                growSize(currentSize));
        System.arraycopy(array, 0, newArray, 0, index);
        newArray[index] = element;
        System.arraycopy(array, index, newArray, index + 1, array.length - index);
        return newArray;
    }
  • to sum up
    • Whether data is large or small, SparseArray to be smaller than the percentage of memory HashMap
    • SparseArray when you add, get data have to use binary search, for the case of a large amount of data, efficiency is lower than HashMap.
    • SparseArray sacrifice efficiency for memory

SparseArray application scenarios

  • The amount of data, in less than one thousand
  • The key must be, in this case HashMap may be replaced with an int SparseArray

Guess you like

Origin blog.csdn.net/reuxfhc/article/details/80786257