Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 內存優化:ArrayMap 深入解析

編輯:關於 Android 程式設計

我們在儲存 key-value 資料時,往往下意識就寫出 HashMap。資料量小的時候沒問題,即便放在 PC 上也無傷大雅;可一旦換成手機這類行動裝置,就必須慎重了。手機內存彌足珍貴,不像 PC 那樣可以肆無忌憚地消耗,使用不當很容易觸發 OOM。為此,Android 團隊為我們準備了 HashMap 的替代品——ArrayMap。

官方對 ArrayMap 的說明是:它並不適合大資料量,相比傳統 HashMap 速度較慢,因為查找採用二分法;而且當你增刪資料時,會對空間重新調整,在大量資料場景下效率並不明顯,甚至可能低於 HashMap 的 50%。

換句話說,ArrayMap 本質上是「犧牲時間換取空間」。寫手機 App 時,恰當使用 ArrayMap,能為內存佔用帶來可觀的改善。

那 HashMap 與 ArrayMap 究竟差在哪?個人總結有以下幾點:

1、儲存方式不同。

HashMap 內部有一個 HashMapEntry<k, v>[] 陣列,每一組鍵值對都存放在這個 Entry 對象裡;使用 put 添加時就會 new 一個新的 HashMapEntry。

 @Override public V put(K key, V value) { if (key == null) { return putValueForNullKey(value); } int hash = secondaryHash(key); HashMapEntry[] tab = table; int index = hash & (tab.length - 1); //先查找有沒有對應的key值,如果有,就改寫value,並返回改寫前的value值:oldValue for (HashMapEntry e = tab[index]; e != null; e = e.next) { if (e.hash == hash && key.equals(e.key)) { preModify(e); V oldValue = e.value; e.value = value; return oldValue; } } // No entry for (non-null) key is present; create one modCount++; if (size++ > threshold) { //擴容,雙倍 tab = doubleCapacity(); index = hash & (tab.length - 1); } addNewEntry(key, value, hash, index); return null; } //創建對象存儲鍵值對 void addNewEntry(K key, V value, int hash, int index) { table[index] = new HashMapEntry(key, value, hash, table[index]); }

ArrayMap 則完全沒有 Entry 這東西,它由兩個陣列協同維護:

 int[] mHashes; Object[] mArray;

其中 mHashes 保存每一項的 hashCode 值,mArray 則存放鍵值對——每兩個元素代表一組,前者是 key,後者是 value。看下面這段程式碼:

 arraymap = new HashMap(); a.put(a, a_value); a.put(b, b_value);

執行後,ArrayMap 的儲存結構如下圖:

這樣就能清楚看到 ArrayMap 的儲存方式了,對應的 put 邏輯是:

 mHashes[index] = hash; mArray[index<<1] = key; mArray[(index<<1)+1] = value;

2、擴容時的處理方式不同。

先看 HashMap:

 if (size++ > threshold) { tab = doubleCapacity(); index = hash & (tab.length - 1); }

doubleCapacity 進行雙倍擴容,其中關鍵的一句話是:

 HashMapEntry[] newTable = makeTable(newCapacity);

最終這個 newTable 會作為擴容後的新對象返回。而 makeTable 做了什麼?如下:

 private HashMapEntry[] makeTable(int newCapacity) { @SuppressWarnings(unchecked) HashMapEntry[] newTable = (HashMapEntry[]) new HashMapEntry[newCapacity]; table = newTable; threshold = (newCapacity >> 1) + (newCapacity >> 2); // 3/4 capacity return newTable; }

可以清楚看到,這裡進行了 new 操作,重新建立對象,開銷不小。

那麼 ArrayMap 呢?看看它的做法:

 //如果容量不夠 if (mSize >= mHashes.length) { final int n = mSize >= (BASE_SIZE*2) ? (mSize+(mSize>>1)) : (mSize >= BASE_SIZE ? (BASE_SIZE*2) : BASE_SIZE); if (DEBUG) Log.d(TAG, put: grow from + mHashes.length + to + n); final int[] ohashes = mHashes; final Object[] oarray = mArray; //分配數組 allocArrays(n); if (mHashes.length > 0) { if (DEBUG) Log.d(TAG, put: copy 0- + mSize + to 0); //特別注意這,是copy,而不是new,效率提升 System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length); System.arraycopy(oarray, 0, mArray, 0, oarray.length); } //釋放無用空間,收縮數組 freeArrays(ohashes, oarray, mSize); }

ArrayMap 用的是 copy 而非 new,因此效率相對更高。

3、ArrayMap 提供數組收縮功能。在 clear 或 remove 之後,它會重新收縮數組,節省空間。

4、ArrayMap 採用二分法查找。具體可參考 android.support.v4.util.ContainerHelpers 中的 binarySearch 方法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved