1
我試圖將從相機接收到的幀保存到散列表中,以備將來在Android應用程序中使用。刪除散列表元素不釋放內存
1. Receive Frame (data of type byte [])
2. I just have a counter variable increased each frame received.
3. Using counter variable i keep in the Hash Table along with 'data'.
4. If i reach the counter > 100, then i start deleting from the beginning.
所以,這是一種保持固定大小的哈希表。
我試着用下面的代碼。
Callback -> Received 'data'
counter++;
myHashTable.put(counter, data.clone());
if (counter > 100) {
byte [] b = (byte[]) myHashTable.get(counter-100);
//use 'b' for some other purpose.
myHashTable.remove(counter-100);
}
我的觀察
我檢查應用程序的內存使用,它不斷成長,並在一段時間接收OOM異常後。我究竟做錯了什麼 ?我檢查了Android Studio內存使用情況監視。
下面是錯誤:
01-04 22:54:55.232 7966-7999/sample.app.com E/art: Throwing OutOfMemoryError "Failed to allocate a 460812 byte allocation with 216448 free bytes and 211KB until OOM"
當你說「爲其他目的使用'b'」時,你使用'b'是什麼?如果有什麼東西保留了對'b'的引用,它就不能被垃圾回收。 –
注意'byte [] b =(byte [])myHashTable.remove(counter - 100);'比單獨獲取和移除要容易。 –
爲什麼不簡單地重置您的櫃檯,當你達到100,所以你覆蓋舊的內容? – SkyDream